Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to localize my webapp. Since localization through javascript only is not recommended I thought using php would be an alternative.

So with php I read a messages.json file that stores all localization data.

$json = file_get_contents("_locales/en/messages.json");

In the header of my webapp I generate some javascript with php according to the user's browser language.

echo "var localeObj = " . $json . ";";

So this is just a var that holds all data from the messages.json file that looks like that

{
    "extTitle": {
        "message": "Test1"
    },
    "extName":{
        "message": "Test2"
    }
}

Now I want to be able to access each item from the json like

var title = getItem("extTitle");

and it returns Test1. Any idea how to do that?

I am not very familar with json but if I just alert the localeObj it gives me just [object Object].

share|improve this question

4 Answers

up vote 3 down vote accepted
var getItem(item) {
   return localObj[item].message;
}

You could always encapsulate your i18n strings too...

(function() {

   var localObj = { ... };

   window.getItem(item) {
       return localObj[item].message;
   }

})();

This way, no other variables can possibly clobber your localObj.

share|improve this answer
 
I get an missing ; before statement window.getItem(item) { } –  artworkad シ Feb 24 '11 at 9:30
 
should be localObj a string? Because it's coming as a string from the php method –  artworkad シ Feb 24 '11 at 9:37
 
@ArtWorkAD No, it shouldn't be. If it is, you'll need to parse it to JSON. –  alex Feb 24 '11 at 10:30
 
@ArtWorkAD Did you copy the code as is? JavaScript has ASI, so it shouldn't be that (unless you have an IDE complaining). Are you running this code in PHP? –  alex Feb 24 '11 at 13:37
 
works fine now, thanks! –  artworkad シ Feb 24 '11 at 13:37

You use array syntax [], or dot syntax ., to access javascript object properties.

Example:

localeObj["extTitle"];
localeObj.extTitle;

I would recommend reading something like this to get more familier with JSON.

share|improve this answer

You can initialize javascript variable like this.

var json = eval(); alert(json.extTitle.message+ ' '+json.extName.message);
share|improve this answer
 
Thanks a lot for this last submission cause it allow me to place my markers on a Google map. very efficient. –  Fred FLECHE Oct 17 '12 at 20:07

Inside messages.php:

<?php
header('Content-type:application/javascript');
$messages = array(
 "yes"=>"hai",
 "no"=>"iie"
);
$messages = json_encode($messages);
echo "window.messages = $messages";
?>

Inside index.html:

<html>
 <body>
 <script type="text/javascript" src="messages.php"></script>
 <script type="text/javascript">
  console.log(window.messages)
 </script>
 </body>
</html>

As long as you tell the browser to interpret the php file as a javascript file, you can echo anything you want.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.