2016-04-25 230 views
1

我創建了一個包裝角度翻譯的指令,也可以變成輸入字段,以便管理員用戶輕鬆翻譯。角度翻譯更新翻譯表

當我更新單個翻譯時,我並不想在更新數據庫中的1行後從我的數據庫中加載整個翻譯表,因爲它看起來效率非常低。

我的問題是,我似乎無法找到角度翻譯API中的任何內容,這將允許我訪問前端緩存。我想直接修改翻譯地圖,而不必在成功更新1行後徹底打亂翻譯的整個映射的數據庫。

我試過的東西:$ translateCache,$ translateLocalStorage,$ translateCookieStorage。

請問有人能夠賜教。

此外,作爲獎勵,我想知道是否有人找出他們可以在角度翻譯中揭示翻譯映射的位置。 請注意,我不想從控制器中的$ translate翻譯的值,因爲它已經插入。

回答

3

進入源碼的簡短視圖並沒有提供簡單的方法。我解決它使用自己的供應商終於緩存從$ translateProvider.translations參考:

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方法使提供者可以訪問「翻譯」。

+0

在配置中你做了 translationHelperProvider.translations = $ translateProvider.translations(); 它通過延遲加載更新轉換時是否更新我們的translationHelper.translations? –