AngularJS:动态语言环境

AngularJS: Dynamic locale

我正在使用 Angular Dynamic locale and Angular-Translate 进行国际化和本地化 (i18n)。而且效果很好。

我喜欢 angular-translate 的想法,它可以在不刷新页面的情况下更改语言。

所有来自angular-translate的词都自动改变了,但来自angular_locale的词(datapicker等)没有,用户需要刷新这一页。

谢谢!

以防万一您没有绝对必要使用 Angular 动态语言环境,您可以像这样创建自己的 LocaleFactory:

 factory('LocaleFactory', function ( $locale, $translate) {
    var locales = {
        nl: {
            "DATETIME_FORMATS": {
                "AMPMS"     : [
                    "AM",
                    "PM"
                ],
                "DAY"       : [
                    "zondag",
                    "maandag",
                    "dinsdag",
                    "woensdag",
                    "donderdag",
                    "vrijdag",
                    "zaterdag"
                ],
                "MONTH"     : [
                    "januari",
                    "februari",
                    "maart",
                    "april",
                    "mei",
                    "juni",
                    "juli",
                    "augustus",
                    "september",
                    "oktober",
                    "november",
                    "december"
                ],
                "SHORTDAY"  : [
                    "zo",
                    "ma",
                    "di",
                    "wo",
                    "do",
                    "vr",
                    "za"
                ],
                "SHORTMONTH": [
                    "jan.",
                    "feb.",
                    "mrt.",
                    "apr.",
                    "mei",
                    "jun.",
                    "jul.",
                    "aug.",
                    "sep.",
                    "okt.",
                    "nov.",
                    "dec."
                ],

                "fullDate"  : "EEEE d MMMM y",
                "longDate"  : "d MMMM y",
                "medium"    : "d MMM y HH:mm:ss",
                "mediumDate": "d MMM y",
                "mediumTime": "HH:mm:ss",
                "short"     : "dd-MM-yyyy HH:mm",
                "shortDate" : "dd-MM-yyyy",
                "shortTime" : "HH:mm"
            },
            "NUMBER_FORMATS"  : {
                "CURRENCY_SYM": "\u20ac",
                "DECIMAL_SEP" : ",",
                "GROUP_SEP"   : ".",
                "PATTERNS"    : [
                    {
                        "gSize"  : 3,
                        "lgSize" : 3,
                        "macFrac": 0,
                        "maxFrac": 3,
                        "minFrac": 0,
                        "minInt" : 1,
                        "negPre" : "-",
                        "negSuf" : "",
                        "posPre" : "",
                        "posSuf" : ""
                    },
                    {
                        "gSize"  : 3,
                        "lgSize" : 3,
                        "macFrac": 0,
                        "maxFrac": 2,
                        "minFrac": 2,
                        "minInt" : 1,
                        "negPre" : "\u00a4\u00a0",
                        "negSuf" : "-",
                        "posPre" : "\u00a4\u00a0",
                        "posSuf" : ""
                    }
                ]
            }
        }
    };

    return {
        setLocale: function (key) {
            $translate.use(key);
            angular.copy(locales[key], $locale);
        }
    };
});

同样你也可以添加其他本地人

调用 setLocale 更改语言环境

run(function (LocaleFactory) {
    LocaleFactory.setLocale('nl');
});

当您的语言环境发生变化时,您可以通过提供语言环境键作为参数来调用 setLocale。它会立即更改您的语言环境