角度翻译更新翻译表 [英] angular translate update translation table

查看:82
本文介绍了角度翻译更新翻译表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一条指令,该指令包装了angular-translate,还可以将其转换为输入字段,以方便管理员用户进行翻译.

I have created a directive which wraps angular-translate and can also turns into input fields for easy Translating for admin users.

当我更新单个翻译时,我真的不想在更新数据库中的1行后从数据库中加载整个翻译表,因为这似乎效率很低.

When I update a single translation I don't really want to load an entire translation table from my DB after updating 1 row in the DB, because it seems incredibly inefficient.

我的问题是,我似乎在angular-translate API中找不到任何东西可以让我访问前端缓存.我想直接修改翻译地图,而不必在成功更新1行后麻烦数据库来翻译整个翻译.

My problem is that I can't seem to find anything in the angular-translate API that will allow me to have access to the front-end Cache. I want to modify the translation map directly without having to bother the DB for an entire mapping of my translations after I updated 1 row successfully.

我尝试过的东西:$ translationCache,$ translateLocalStorage,$ translateCookieStorage.

Things that I have tried: $translationCache, $translateLocalStorage, $translateCookieStorage.

有人可以启发我吗?

此外,作为奖励,我想知道是否有人想出可以在角度平移中显示平移贴图的位置.注意,我不希望$ translate的转换值在控制器中,因为已经插入了.

Also, as a bonus, I wonder if anyone figured out where they can expose the translation mapping in angular translate. Note, I don't want the translated values from $translate in the controller because that's already interpolated.

推荐答案

对源代码进行简短浏览不能提供简单的方法.我终于解决了它,使用自己的提供程序从$ translateProvider.translations缓存了一个引用:

A short view into the source offers no simple way for this. I solved it finally caching a reference from $translateProvider.translations using a own provider:

app.provider('translationHelper', function () {
    this.translations = {};

    this.$get = function () {
        return {
            translations: this.translations
        }
    };
});

在您的应用程序中

app.config([
    '$translateProvider',
    'translationHelperProvider',
function (
    $translateProvider,
    translationHelperProvider
) {
    translationHelperProvider.translations = $translateProvider.translations();
}]);

,稍后再使用它

app.component('myComponent', {
    templateUrl: 'views/components/myComponent.html',
    controller: [
        'translationHelper',
    function (
        translationHelper
    ) {
        // query translation
        var translation = translationHelper.translations['de']["Your.Key.Here"];
        // modify translation
        translationHelper.translations['de']["Your.Key.Here"] = 'A new value';
    }]
});

或者,您可以修改角度平移源,并通过其$ get方法访问提供者的平移".

Alternatively, you can modify the angular translate source and made 'translations' from provider accessible through its $get method.

这篇关于角度翻译更新翻译表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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