Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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){

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...