Reputation: 8697
I've language files like "en.lang.php":
/* english language */
return array(
"random.key.one" => "random value one",
"random.key.two" => "random value two);
Usage (the quick & dirty way):
/* random_template.php */
$language = "en";
$file = $language . ".lang.php";
$text = include($file);
echo $text["random.key.one"]; // "random value one"
Question: How is it possible to use this values in JavaScript?
Idea: Generate en.lang.js with a function that returns the needed value, usage:
alert(get_text("random.key.one"));
Problem: I've to flush the cache everytime the *.lang.php-file was changed. Not that user-friendly.
Thanks in advance!
Upvotes: 0
Views: 257
Reputation: 2126
Try to cache control the page and set it to no cache so it will load from origin.
And if you want to play with PHP and JS you have to keep in mind that PHP is server side and JS is client side. So best thing i can suggest is to use Ajax. Its prety easy to use JS on the page and get JS to query the server that process data with php.
Upvotes: 1
Reputation: 1183
If you're using PHP >= 5.2.1 (.0, technically), the easiest way I can imagine is as follows:
Have a file that generates a JSON-array with
echo 'set_text(' . json_encode(include($file)) . ')';
JavaScript-wise:
var texts = [];
function set_text(languageTexts){
texts = languageTexts;
}
function get_text(key) {
return texts[key];
}
Upvotes: 2