Localization: Php and Javascript

I am thinking about the best way to localize my site. I am using Laravel which already has a php localization class, but I will also need to translate some javascript strings

Ideally , I would use a library or translation file that could be edited through Poedit and used for both javascript and php, but I cannot find a solution for this. Is this even possible without creating a whole new code library?

The solution I have been thinking about so far is far from ideal:

  • Use php localization class for php strings
  • Use javascript localization library for javascript
  • Save the language in a cookie that can be read from both ends.

The idea of ​​keeping two separate language files (one for php and one for javascript) doesn't seem like an elegant solution.

I've seen several javascript libraries for laravel, but they require you to compile your library into a javascript array file every time you update something, which will make it not very dynamic and redundant since most php lines will not be used in javascript and vice versa.

Any thoughts on this?

+3


source to share


3 answers


You can always have your Javascript files generated by PHP for each request, which means you can still use the PHP localization library for both.

I would suggest that this might not play well for caching javascript files in the browser, but you can always have a PHP script that generates javascript files, creating a different file for each language, and then refreshing them every time you need to. just run PHP script to overwrite them



I've never worked on localization, but I'm guessing it includes a file that matches words / phrases for each language? If so, can you set up a cron job that checks the last time this file was updated and then runs a PHP script to generate the Javascript files? Then you don't have to worry about manually running it every time.

+1


source


We have faced this problem for a while. If you are looking on Github you will find some projects there, but either one will solve the problem and as you said, maintaining two files is not a good option. So, the ways to solve this problem:



  • On each page, declare a javascript variable that you set using php, so it will be available on the JS side as well. This is not yet elegant, but it solves the problem. So it would be something like this:

    /**
     * i18n for JS
     */
    
    var i18n = (function() {
      return {
        stringOne: "{{trans('yourStringOneKeyFromPhp')}}",
        stringTwo: "{{trans('yourStringTwoKeyFromPhp')}}"
      }
    }());
    
          

  • On the other hand, a better solution would be, as Adi pointed out, creating fake views that actually serve as javascript files in which you will resolve the i18n variables on the controller. This is a more elegant solution, but it goes against caching, so I suppose it is a matter of tradeoff you choose to implement. Perhaps a good way would be to use this plugin and update the json files as explained earlier.

+1


source


For sites that are small up to a certain size, I would create a translation table such as

table: translations
en_gb, de, fr
--------------------------------
Text, Text (in de), Text (in fr)

      

Then you identify the current locale by your url and only get data for the current locale, for example.

if locale == de, get en_gb and de fields.

      

Then create an array:

$translations = ['en_gb' => 'de'];

      

Once you get the array, you can translate any static content via php.

Now you can pass your array to javascript

var translations = json_encode($translations);

      

You will now also have a javascript object that you can access: translations['English text']

will return a de-translation.

You will only query the database once on page load / refresh, and tables will make it easier to maintain.

0


source







All Articles