0

My page is available in 3 languages and I'm including in the header of each file (index, contact, etc.) the selected language file (en, ru, pl).

<?php
session_start();
include('language/'.$_SESSION['lang'].'.php');
?>

en.php

<?php
session_start();
$lang = array();

$lang['hello'] = 'Hello';
$lang['world'] = 'World !';
?>

ru.php

<?php
session_start();
$lang = array();

$lang['hello'] = 'привет';
$lang['world'] = 'мир';
?>

And this is how I'm echoing them:

<p><? echo $lang['hello'].' '.$lang['world'];?></p>

This is working actually fine, but I'm having problem with my .js files. If I want to alert or insert some text somewhere I can't.

For example in one of my .js file I have an alert:

if (something_obj.val() == ''){
   alert('Wrong username given !');
   return false;
}

How can I implement language files in my js files? Am I need to create a news .js language file?

2
  • 1
    You can use JSON. See the following answer: stackoverflow.com/a/5619038/290172 Commented Mar 29, 2013 at 12:28
  • The answer for JavaScript has already been given. However, for translations in PHP; Why re-invent the wheel? Most PHP installations have gettext installed. Gettext is a standardised way to translate strings in your application and uses separate 'translation' files (.po/.mo) that contain translations per language. You can translate a string by setting the current 'locale' and translating the text via _('text-to-translate') Commented Mar 29, 2013 at 12:56

2 Answers 2

3

You could have one js file per language with translations looking like this:

// german.js
var translations = {
  'hello': 'hallo',
  'world': 'welt'
};

In other JS scripts, just do something like this:

alert(translations['hello'] + " " + translations['world'])

Of course, you might want to choose a shorter variable name than translations.

Then, in your php file, include the correct JS script based on the language:

<script src="languages/<?php echo $_SESSION['lang']; ?>.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 For translations in JavaScript, it is common practice to use separate .js files per language for an example, have a look at the jquery-ui date picker; jquery-ui.datepicker-fr.js.
1

You can create new file - for example js.php which will load proper language file and print out all variables into JavaScript array:

js.php

<script type="text/javascript">
var lang = {
<?php
session_start();
include('language/'.$_SESSION['lang'].'.php');

$data = array();
foreach ( $lang as $key => $value ) {
  $data[] = '"' . $key . '" : "' . $value . '"';
}
echo implode(',', $data);
?>
};
</script>

Then include this file on the page which have to use JavaScript translation array and use it with:

alert(lang.key);

Also if there will be " sign in the translation string, you have to escape it with \", for example with:

$value = str_replace('"', '\"', $value);

2 Comments

Not a good idea unless you don't mind that caching won't work, I think.
It's just an idea which can be extended with cache feature, etc.

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.