How to load Angular-translate before any UI is displayed with ui-router resolve

那年仲夏 提交于 2019-11-28 06:32:46

问题


I used angular-translate for i18n. I want to use $translatePartialLoader service to modular language key as lazy load. Also I want to use ui-router resolve option for this.

Now How to do this? Is possible add a code sample for me?

Thanks


回答1:


I find solutions and solve my problem.

In config:

$translatePartialLoaderProvider.addPart('index');
        $translateProvider
            .useSanitizeValueStrategy(null)
            .fallbackLanguage('en-us')
            .registerAvailableLanguageKeys(['en-us','pt-br'], {
                'en_*': 'en-us',
                'pt_*': 'pt-br'
            })
            .useLoader('$translatePartialLoader', {
                urlTemplate: '{part}/locale_{lang}.json'
            })
            .useLoaderCache(true)
            .useCookieStorage()
            .determinePreferredLanguage();

In ui-router for index:

.state('index', {
    url: '/index',
    templateUrl: 'index.html',                   
    controller:'IndexCtrl',
    resolve: {
        trans:['RequireTranslations',
            function (RequireTranslations) {
                RequireTranslations('index');
            }],
        dep: ['trans','$ocLazyLoad',
            function(trans,$ocLazyLoad){
                return $ocLazyLoad.load(['plugin']).then(
                    function(){
                        return $ocLazyLoad.load(['IndexCtrl.js']);
                    }
                );
            }]
    }
})
.state('index.users',{
    url: "/users",
    templateUrl: "users.html",
    controller:'UserListCtrl',
    resolve: {
        trans:['RequireTranslations',
            function (RequireTranslations) {
                RequireTranslations('modules/user');
            }],
        dep: ['trans','$ocLazyLoad',
            function(trans,$ocLazyLoad){
                return $ocLazyLoad.load(['UserListCtrl.js'])
            }]

    }
})

and in run:

app.run(function($rootScope,$translate) {

    // translate refresh is necessary to load translate table
    $rootScope.$on('$translatePartialLoaderStructureChanged', function () {
        $translate.refresh();
    });

    $rootScope.$on('$translateChangeEnd', function() {
        // get current language
        $rootScope.currentLanguage = $translate.use();
    });
})

and in RequireTranslations factory:

app.factory('RequireTranslations', function($translatePartialLoader, $translate,$rootScope) {
    return function() {
        angular.forEach(arguments, function(translationKey) {
            $translatePartialLoader.addPart(translationKey);
        });
        return $translate.refresh().then(
            function(){
                return $translate.use($rootScope.currentLanguage);
            }
        );
    };
});

and please note you should add $translatePartialLoader and trans as parameter in all controllers like this:

app.controller('UserListCtrl',function($scope,...,$translatePartialLoader,trans){


来源:https://stackoverflow.com/questions/37247083/how-to-load-angular-translate-before-any-ui-is-displayed-with-ui-router-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!