Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization file for JS and PHP

I did some search about localization files, see that we can use .po file or gettext but, is there any tutorial or sample of a unique solution that will work both in javascript and in php.

I want to only maintain one localization file per language that will work with both JS and PHP languages.

Would appreciates if someone can point me to some links or samples...

like image 278
Steve Avatar asked Oct 26 '25 09:10

Steve


2 Answers

I found that it is typically a sign of a questionable design when translatable text is coded inside JS functions. JS is meant to implement logic, not content. The content should be provided by PHP (typically by using a templating engine) and should be used by JS. That way you only need a localization solution for PHP.

If (exceptions always occur) you really need to translate a phrase inside a JS routine you use an ajax call to fetch the translation. This also simplifies the access to the dictionary holding the translation tokens since it is again done by PHP. The dictionary can be kept in a single place.

like image 195
arkascha Avatar answered Oct 29 '25 00:10

arkascha


Yep, there is. I've successfully used gettext.js a while ago, which is operating on .json or .po files. This way, you only have to maintain one translation source. The webpage I've used this for is located here so you can check out how I've did it. Good luck!

like image 40
David Müller Avatar answered Oct 28 '25 22:10

David Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!