AngularJS: a dynamic language

I am using Angular Dynamic Language and Angular -Translate for Internationalization and Localization (i18n). And it works well.

I like the idea of Angular-translate , which allows you to change the language without refreshing the page.

  • Can you do the same with Angular's Dynamic Locale ? If possible, how can I get it?

All words from angular-translate changed automatically, but not words from angular_locale (datapicker, etc.) that users need to refresh the page.

Thank!

+3


source to share


1 answer


Just in case you don't absolutely need to use the Angular Dynamic locale, you can create your own LocaleFactory like this:

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

      

You can add other locals in the same way



Calling setLocale to change the locale

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

      

When you change your language, you can call setLocale by providing the locale key as an argument. It will instantly change your language

+8


source







All Articles