首页 > 怎么使用i18next的angular插件

怎么使用i18next的angular插件

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

json部分:

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

html部分

<div 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


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

【热门文章】
【热门文章】