0

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!

1

2 Answers 2

2

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];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.