AngularJS实现了模板的本地化 [英] AngularJS Implementing a localization for templates
问题描述
我想实施意见的本地化(应包括身体,太)。
我已经通过加载一个JSON文件和比迭代低谷键这样做过。
键是类名。比我干脆分配的关键价值
元素与类。
i want to implement a localization for views (should include the body, too). I've done this before by loading a JSON File and than iterate trough the keys. The keys are class names. Than i simply assign the value of the key to the element with the class.
语言文件(JSON)搜索
".Header-Title" : "My Title",
".Header-Text" : "Lorem ipsum vehicula interdum."
code示例搜索
$.load("./Content/Text/main-en.json", function(data) {
for (key in data) {
$(key).html(data[key]);
}
});
我喜欢这一点,因为文字是从HTML和脚本分离。想知道我可以在AngularJS做到这一点。我的想法是通过将路径一个额外的参数来扩展路线提供商
到JSON语言文件。加载视图后的函数应该被称为是的分配
值喜欢在code例子。刚开始用AngularJS。任何意见或帮助?
I liked this because the text is separated from html and script. Like to know can i accomplish this in AngularJS. My thoughts are to extend the route provider by a additional parameter that takes the path to the JSON Language File. After loading a view a function should be called that's assign the values like in the code example. Just have started with AngularJS. Any ideas or help ?
推荐答案
1)装载的服务,包括您的i18n文件。例如,借此
// /i18/en/dictionary_en.js
[
{
"key":"_More_",
"value":"More",
"description":"More button"
}
]
// In the localize service
$http({ method:"GET", url:url, cache:false }).success(localize.successCallback).error(function () {
// the request failed set the url to the default resource file
var url = '/i18n/en/dictionary_en.js';
localize.language = 'en';
// request the default resource file
$http({ method:"GET", url:url, cache:false }).success(localize.successCallback);
});
2)在你的意见用I18过滤器或通过 NG-绑定
2) In your views use the i18 filter or through ng-bind
<button data-i18n="_More_">
这篇关于AngularJS实现了模板的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!