我正在使用jquery-localize.js加载本地翻译文件,效果很好。
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Localize Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="libs/jquery.localize.js" type="text/javascript" charset="utf-8"></script>
<script src="app.js"></script>
</head>
<body>
<h1>Test localization...</h1>
<p data-localize="test.nested">puts 2 + 2</p>
<input data-localize="test.input" type="text" value="fail" />
<select>
<optgroup data-localize="test.optgroup" label="Fail">
<option data-localize="test.option" value="1">Fail</option>
</optgroup>
</select>
<p>
<img src="ruby_square.gif" alt="a square ruby" data-localize="test.ruby_image" />
Ruby image should be round.
</p>
<p data-localize="basic">It failed :(</p>
<script type="text/javascript" charset="utf-8">$(function(){
var opts = { language: "sv", pathPrefix: "/test/lang", skipLanguage: "en-US" };
$("[data-localize]").localize("test", opts)
})</script>
</body>
</html>
test-sv.json文件
{
"test": {
"nested": "nested success2",
"input": "input success2",
"input_as_obj": {
"value": "input_as_obj value success2",
"title": "input_as_obj title success2"
},
"optgroup": "optgroup success2",
"option": "option success2",
"ruby_image": {
"src": "ruby_round.gif",
"alt": "a round ruby2",
"title": "A Round Ruby2"
},
"link": {
"text": "success2",
"href": "http://success2"
}
},
"basic": "basic success2",
"with_title": {
"text": "with_title text success2",
"title": "with_title title success2"
}
}
但是我想对特定单词进行特定翻译,例如:
var opts = { language: "sv", pathPrefix: "/test/lang", skipLanguage: "en-US" };
var translation = $.localize("test.input", opts)
不确定如何实现。有帮助吗?