最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pass JSON from php to javascript - Stack Overflow

programmeradmin0浏览0评论

I want to localize my webapp. Since localization through javascript only is not remended 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].

I want to localize my webapp. Since localization through javascript only is not remended 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 edited Feb 24, 2011 at 6:34 Clyde Lobo 9,1747 gold badges36 silver badges63 bronze badges asked Feb 24, 2011 at 6:29 DarkLeafyGreenDarkLeafyGreen 70.5k136 gold badges391 silver badges616 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4
var getItem = function(item) {
   return localObj[item].message;
};

You could always encapsulate your i18n strings too...

(function() {

   var localObj = { ... };

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

})();

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

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

Example:

localeObj["extTitle"];
localeObj.extTitle;

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

You can initialize javascript variable like this.

var json = eval(<? echo $json ?>);   
alert(json.extTitle.message+ '  '+json.extName.message);

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.

发布评论

评论列表(0)

  1. 暂无评论