与gettext的动态消息(AngularJS) [英] Dynamic messages with gettext (AngularJS)

查看:171
本文介绍了与gettext的动态消息(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django的后端和AngularJS前端应用一个。
我用href=\"https://github.com/rubenv/angular-gettext/\" rel=\"nofollow\">角gettext的插件与步兵一起处理翻译的

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.

问题是,我有时会通过API接收的动态字符串从我的后端。例如有关外键约束的MySQL错误或重复键条目。
我怎么能这个字符串添加到一般.POT文件或不哈日codeD字符串?

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);
            }
        };
    }]);

})();

一个解决方案,我能想到的将是每一个动态字符串写入一个JSON对象。发送JSON服务器,并从那里,写一个包含这些字符串的静态文件,以便gettext的可以提取它们。

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.

你有什么建议?

推荐答案

我也用角gettext和有需要翻译从服务器返回的字符串。我们不喜欢有这些邮件单独翻译系统的想法,所以我们比他们在像正常的默认语言发送。

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.

这名单是通过code正确格式化成HTML与翻译标签。该文件未部署,它只是存在,使提取的任务,找到字符串。

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.

其次,我们创建了一个过滤器上做插值翻译,所以不是翻译 {{富}} 将翻译的单词酒吧如果这是是foo的值。我们称之为的postTranslate ,这是一个简单的:

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

这篇关于与gettext的动态消息(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆