如何将所有语言环境预先加载到角度动态语言环境缓存中(tmhDynamicLocaleCache)

时间:2015-06-09 07:35:19

标签: angularjs

我正在使用角度动态区域设置(tmhDynamicLocale),但希望在应用启动时将所有区域设置预先加载到缓存(tmhDynamicLocaleCache)中。这背后的动机是将我的所有资源捆绑到1个缩小的js文件而不是标准的延迟加载。在任何地方都有完整的例子吗?

1 个答案:

答案 0 :(得分:3)

解决了它。 我所做的是将每个语言环境放入一个类似的模块中:

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

为您使用的每个区域设置创建这样的模块,只需更改" en"到" 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')时,它将在缓存中找到区域设置对象并使用它。