1

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 Answer 1

5

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){
Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful answer helped me solve the issue I was facing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.