Dynamic messages with gettext (AngularJS)

纵然是瞬间 提交于 2019-12-10 17:28:25

问题


I have a application with a Django backend and an AngularJS front-end. I use the angular-gettext plugin along with Grunt to handle translations.

The thing is, I sometimes received dynamic strings from my backend through the API. For instance a MySQL error about a foreign key constraint or duplicate key entry. How can I add this strings to the .pot file or non harcoded string in general ?

I've tried to following but of course it cannot work :

 angular.module('app').factory('HttpInterceptor', ['$q', '$injector', '$rootScope', '$cookieStore', 'gettext', function ($q, $injector, $rootScope, $cookieStore, gettext) {

            responseError: function (rejection) {
                    gettext('static string'); //it works
                    gettext(rejection.data.error); //does not work
                    $rootScope.$emit('errorModal', rejection.data);
                }

                // Return the promise rejection.
                return $q.reject(rejection);
            }
        };
    }]);

})();

One solution I could think of would be to write every dynamic strings into a JSON object. Send this json to server and from there, write a static file containing these strings so gettext can extract them.

What do you suggest ?


回答1:


I also use angular-gettext and have strings returned from the server that need to be translated. We did not like the idea of having a separate translation system for those messages so we send them over in the default language like normal.

To allow this to work we did two things. We created a function in our backend which we can call to retrieve all the possible strings to translate. In our case it's mainly static data that only changes once in a while. Ideally this would be automated but it's fine for now.

That list is formatted properly through code into html with the translate tag. This file is not deployed, it is just there to allow the extraction task to find the strings.

Secondly we created a filter to do the translation on the interpolated value, so instead of translating {{foo}} it will translate the word bar if that's was the value of foo. We called this postTranslate and it's a simple:

angular
.module('app')
.filter('postTranslate', ['gettextCatalog', function (gettextCatalog) {
    return function (s) {
        return gettextCatalog.getString(s);
    };
}]);

As for things that are not in the database we have another file for those where we manually put them in. So your error messages may go here.

If errors are all you are worried about though, you may rather consider not showing all the error messages directly and instead determine what user friendly error message to show. That user friendly error message is in the front end and therefore circumvents all of this other headache :)



来源:https://stackoverflow.com/questions/25045947/dynamic-messages-with-gettext-angularjs

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