0
我正在使用角動態語言環境(tmhDynamicLocale),但希望在應用程序啓動時將所有語言環境預加載到緩存(tmhDynamicLocaleCache)中。這背後的動機是將我所有的資源捆綁到1個縮小的js文件中,而不是標準的延遲加載。這個地方有沒有完整的例子?如何將所有語言環境預加載到角動態語言環境緩存中(tmhDynamicLocaleCache)
我正在使用角動態語言環境(tmhDynamicLocale),但希望在應用程序啓動時將所有語言環境預加載到緩存(tmhDynamicLocaleCache)中。這背後的動機是將我所有的資源捆綁到1個縮小的js文件中,而不是標準的延遲加載。這個地方有沒有完整的例子?如何將所有語言環境預加載到角動態語言環境緩存中(tmhDynamicLocaleCache)
解決了它。 我所做的就是把每個區域設置成一個模塊,像這樣:
angular.module("myapp.locale.en", [
'tmh.dynamicLocale'
])
.run(['tmhDynamicLocaleCache', function (tmhDynamicLocaleCache) {
var PLURAL_CATEGORY = {ZERO: "zero", ONE: "one", TWO: "two", FEW: "few", MANY: "many", OTHER: "other"};
function getDecimals(n) {
n = n + '';
var i = n.indexOf('.');
return (i == -1) ? 0 : n.length - i - 1;
};
function getVF(n, opt_precision) {
var v = opt_precision;
if (undefined === v) {
v = Math.min(getDecimals(n), 3);
}
var base = Math.pow(10, v);
var f = ((n * base) | 0) % base;
return {v: v, f: f};
};
tmhDynamicLocaleCache.put('en', {
"DATETIME_FORMATS": {
"AMPMS": [
"AM",
"PM"
],
"DAY": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
"FIRSTDAYOFWEEK": 6,
"MONTH": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"SHORTDAY": [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
],
"SHORTMONTH": [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
"WEEKENDRANGE": [
5,
6
],
"fullDate": "EEEE, MMMM d, y",
"longDate": "MMMM d, y",
"medium": "MMM d, y h:mm:ss a",
"mediumDate": "MMM d, y",
"mediumTime": "h:mm:ss a",
"short": "M/d/yy h:mm a",
"shortDate": "M/d/yy",
"shortTime": "h:mm a"
},
"NUMBER_FORMATS": {
"CURRENCY_SYM": "$",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "\u00a4-",
"negSuf": "",
"posPre": "\u00a4",
"posSuf": ""
}
]
},
"id": "en",
"pluralCat": function (n, opt_precision) {
var i = n | 0;
var vf = getVF(n, opt_precision);
if (i == 1 && vf.v === 0) {
return PLURAL_CATEGORY.ONE;
}
return PLURAL_CATEGORY.OTHER;
}
});
}])
;
創建一個模塊,這樣每個區域設置使用,只需更改「恩」爲「PT」或「他」等(3處在上面的代碼中)。
下一頁加載這些模塊到您的app.js ...
angular.module('myapp', [
'myapp.locale.en',
'myapp.locale.pt',
'myapp.locale.de',
'myapp.locale.he'
])
這樣.RUN時()調用這些模塊就會把Locale對象到tmhDynamicLocaleCache的。然後當你以後使用tmhDynamicLocale.set('en')時,它會在緩存中找到locale對象並使用它。
由於我們的代碼在加載到'angular-locale - {{locale}}。js'文件中的異步調用之前加載了英文短語,日期和數字,我們必須將其應用。證明是非常多的解決方案。但是,我們必須讓我們的'i18nLanguageService'模塊將這些新創建的語言環境模塊(在您的情況下爲'myapp.locale.en')視爲依賴關係,這將強制tmhDynamicLocaleCache在使用之前填充。感謝您領導解決方案! –