Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript and Translations

I have a PHP application that makes extensive use of Javascript on the client side. I have a simple system on the PHP side for providing translators an easy way to provide new languages. But there are cases where javascript needs to display language elements to the user (maybe an OK or cancel button or "loading" or something).

With PHP, I just have a text file that is cached on the server side which contains phrase codes on one side and their translation on the other. A translator just needs to replace the english with their own language and send me the translated version which I integrate into the application.

I want something similar on the client side. It occurred to me to have a javascript include that is just a set of translated constants but then every page load is downloading a potentially large file most of which is unnecessary.

Has anyone had to deal with this? If so, what was your solution?

EDIT: To be clear, I'm not referring to "on-the-fly" translations here. The translations have already been prepared and are ready to go, I just need them to be made available to the client in an efficient way.

like image 398
Karim Avatar asked Feb 02 '26 23:02

Karim


1 Answers

How about feeding the javascript from php? So instead of heaving:

  <script type='text/javascript' src='jsscript.js'></script>

do

 <script type='text/javascript' src='jsscript.php'></script>

And then in the php file replace all outputted text with their associated constants.

Be sure to output the correct caching headers from within PHP code.

EDIT

These are the headers that I use:

header('Content-type: text/javascript');
header('Cache-Control: public');
header('expires: '. date("r", time() + ( 7 * 24 * 60 * 60 ) ) ); // 1 week
header("Pragma: public");
like image 76
Pim Jager Avatar answered Feb 05 '26 12:02

Pim Jager



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!