Search code examples
javascriptangularjsinternationalizationangular-translate

How to use i18n file in a service (angular-translate)?


I'm using angular-translate to translate my website and it works. But now, I want to show an error dialog when there is a problem (api request). To do that, I've created a service to display a modal with two parameters title and the error message.

One part of the error message is static, so I can translate it directly with angular-translate.

Here my service (errorModal.service.js) :

(function () {
    'use strict';

    angular
        .module('app.core.project')
        .factory('errorDialog', ['$mdDialog', '$document', '$translatePartialLoader', '$translate', errorDialog]);

        /** @ngInject */
    function errorDialog($mdDialog, $document, $translatePartialLoaderProvider, $translate)
    {
        var service = {
            displayError : displayError
        };

        // path to i18n folder : /app/core-project/services/i18n/{lang}.json but it can't be used...
        $translatePartialLoaderProvider.addPart('/app/core-project/services/');

        return service;

        function displayError(title, errorMessage, clickOutsideToClose)
        {
            // error_modal.message isn't translated
            var message = $translate.instant('error_modal.message') + errorMessage;
            console.log(message); // show "error_modal.message"

            $mdDialog.show(
                $mdDialog.alert()
                    .parent(angular.element($document.body))
                    .clickOutsideToClose(clickOutsideToClose)
                    .title(title)
                    .htmlContent(message)
                    .ariaLabel(title)
                    .ok('OK')
            );
        }

    }

})();

How can use i18n file in a specific path in this service ?

Thank you very much for your help !


Solution

  • I found the solution. I just added translatePartialLoader provider into my config, specified the folder where to find the translations.

    (function ()
    {
        'use strict';
    
        angular
            .module('app.core.project')
            .config(config);
    
        /** @ngInject */
        function config($translatePartialLoaderProvider)
        {
            // Put your custom configurations here
    
            $translatePartialLoaderProvider.addPart('app/core-project/services');
        }
    
    })();
    

    and my errorModal.service.js

    (function () {
        'use strict';
    
        angular
            .module('app.core.project')
            .factory('errorDialog', ['$mdDialog', '$document', '$translate', errorDialog]);
    
            /** @ngInject */
        function errorDialog($mdDialog, $document, $translate)
        {
            var service = {
                displayError : displayError
            };
    
            return service;
    
            function displayError(statusCode, errorMessage)
            {
                var clickOutsideToClose = false;
    
                var title = $translate.instant('core_project.dialog.error.status_code', { statusCode: statusCode });
                var message = $translate.instant('core_project.dialog.error.message', { message: errorMessage });
    
                $mdDialog.show(
                    $mdDialog.alert()
                        .parent(angular.element($document.body))
                        .clickOutsideToClose(clickOutsideToClose)
                        .title(title)
                        .htmlContent(message)
                        .ariaLabel(title)
                        .ok('OK')
                );
            }
    
        }
    
    })();