javascript - 怎么使用i18next的angular插件
天蓬老师
天蓬老师 2017-04-10 16:52:56
0
1
395

正在用i18next的angular插件https://github.com/i18next/ng-i18next,可以正确读取json文件,如果使用JS DOM的话可以正确显示出内容。但是不知道怎么使用插件的angular指令翻译内容。以下是我的测试代码

json部分:

{
    "home":{
        "title": "test",
        "content": "this is a test"
    }
}

html部分

<p ng-app="myApp" ng-controller="myCtrl" class="home">
<h1 ng-i18next="home.title"></h1>
<p ng-i18next="home.content"></p>

js部分

angular.module('jm.i18next')
.config(['$i18nextProvider',function($i18nextProvider){
  window.i18next.use(window.i18nextXHRBackend);

  $i18nextProvider.options = {
    debug: true,
    lng: 'en',
    fallbackLng: 'en',
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json'
    }
  };
}]);

angular.module('myApp', ['jm.i18next']).controller('myCtrl',['$scope', '$i18next',function($scope, $i18next){
}])

以上代码是想在页面上显示翻译出来的内容"test"和"this is a test", 但是显示的是“home.title"和”home.content".

以下是console的记录,最后一个报错不知道什么引起的:
i18next::backendConnector: loaded namespace translation for language en Object {home: Object}
i18next.min.js:2 i18next: languageChanged en
i18next.min.js:2 i18next: initialized Object {debug: true, ns: Array[1], defaultNS: Array[1], fallbackLng: Array[1], fallbackNS: false…}
ng-i18next.min.js:1 Uncaught TypeError: i.lng is not a function

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
阿神

已经解决了,angular插件不兼容i18next 2.x,目前暂时的解决方法是加入compatibility flags
http://i18next.com/docs/migration/

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!