angular i18next :: backendConnector正确加载,但转换器在控制器中不可用

问题描述 投票:0回答:1

我初始i18next:

if (window.i18next) {
window.i18next.use(window.i18nextXHRBackend) ;
window.i18next.use(window.i18nextSprintfPostProcessor);
window.i18next.init({
        debug: true,
        fallbackLng: 'en',
        lng: 'en',
        resGetPath: '/locales/{{lng}}/camps.json',
        backend: {
            loadPath: '/locales/{{lng}}/camps.json'
        },
    }, function (err, t) {
        return i18next ;
        console.log('resources loaded',t);
        var translate = i18next.t("nav");
        console.log("translate variable = " + translate);
    });
};

app = angular.module("ngCamps", ['ngSanitize', 'jm.i18next' , 'ngAnimate', 'ui.select']) ;

然后在我的控制器中:

app.controller("myController", ($scope, $http, $filter, $q, $i18next ) =>     {
    const resources = i18next.getResourceBundle('en','camps') ;
    console.log("resources",resources) ;
    $scope.hello = i18next.t("nav");
    console.log($scope.hello) ;

.....当页面加载时,翻译json不可用。但在页面加载后,我得到一个控制台,告诉我资源已加载

控制台看起来像这样:

i18next::translator: missingKey undefined translation nav nav

...然后i18next :: backendConnector:加载语言en的命名空间转换

如果我进入控制台 - 我得到正确的翻译:

i18next.t('nav');
"My Camp"

因此,在转换在控制器中可用之前,页面似乎已加载。

我怎么能改变它?谢谢你的帮助。

angularjs i18next
1个回答
0
投票

$i18next.t事件被触发时,您可以使用i18nextLanguageChange,如下所示:

app.controller('myController', ($rootScope, $scope, $http, $filter, $q, $i18next) => {

  $scope.hello = '';

  $rootScope.$on('i18nextLanguageChange', function () {
    $scope.hello = $i18next.t('nav');
    console.log($scope.hello);
  });

示例:https://github.com/i18next/ng-i18next/blob/master/examples/provider/provider1.js

© www.soinside.com 2019 - 2024. All rights reserved.