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?

Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
  • 1
    You can use JSON. See the following answer: http://stackoverflow.com/a/5619038/290172 – GExGE Mar 29 '13 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](http://php.net/manual/en/function.gettext.php) 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')` – thaJeztah Mar 29 '13 at 12:56

2 Answers2

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>
thejh
  • 44,854
  • 16
  • 96
  • 107
  • +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](http://jqueryui.com/resources/demos/datepicker/jquery.ui.datepicker-fr.js). – thaJeztah Mar 29 '13 at 12:54
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);
hsz
  • 148,279
  • 62
  • 259
  • 315