Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization: Php and Javascript

I'm thinking of the best way to localize my website. 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 which could be editable through Poedit and could used for both javascript and php, but I can't find a solution for that. Is it even possible without having to create a whole new code library?

The solution I thought of until now is far from ideal:

  • Use a php localization class for php strings
  • Use a javascript localization library for javascript
  • Store the language in a cookie which could be read by both ends.

The idea of keeping two separate language files (one for php and one for javascript) does not seem like an elegant solution.

I saw some javascript libraries for laravel, but they require you to compile your library into a javascript array file every time you update something, which would make it not very dynamic and also redundant, since most php strings won't be used in javascript and vice-versa.

Any thoughts on this?

like image 202
sigmaxf Avatar asked Feb 28 '26 00:02

sigmaxf


1 Answers

You could always have your Javascript files be PHP generated per request, which would mean you could still use the PHP localization library for both.

I would imagine that this might not play very nice for caching the javascript files in browser, but you could always have a PHP script that generates the javascript files, creating a different file per language, then any time you need to update them, just run the PHP script again to overwrite them

I have never worked on localization, but I imagine it involves a file which matches words/phrases for each language? If so, you could set up a cron job that checks the last time this file was updated, which then runs the PHP script to generate the Javascript files? Then you won't have to worry about running it manually each time

like image 110
Adi Bradfield Avatar answered Mar 02 '26 14:03

Adi Bradfield



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!