Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-translate and date formatter: how to translate month name?

In my app i'm using angular-translate

https://github.com/angular-translate/angular-translate

for translating my content.

And also in my view i have such date formatter:

{{article.CreatedAt | date:'dd MMM yyyy':'UTC'}}

but when i set polish, russian (or any other language, non-english) - my month names are still in english.

How can i translate them (will be great, if this is doneable without momentum and other plugins...)?

like image 720
brabertaser19 Avatar asked Dec 22 '15 21:12

brabertaser19


2 Answers

For changing application locale dynamically you need angular-dynamic-locale and you also need other locale files (english is coming with angular) from ngLocale.

Here is working plunker.

You need to catch translate events as we want to change locale when language is changed. So for this purpose I used $translateChangeSuccess event to set selected language as new locale.

$rootScope.$on('$translateChangeSuccess', function (event, data) {
    tmhDynamicLocale.set(data.language);
});

Here is the list of all $translate events.

like image 168
Poyraz Yilmaz Avatar answered Oct 05 '22 03:10

Poyraz Yilmaz


You don't need an external library, in angular, date and number format are translated using angular-i18n: https://docs.angularjs.org/guide/i18n

For example, to get date in russian, just include the locale file after angular:

<script src="angular.js"></script>
<script src="angular-locale_ru-ru.js"></script>

Of course this will not be dynamic, you could manage it server-side, or find another trick.

to get it from bower, use

$ bower install angular-i18n

see Where are the AngularJS I18n files?

like image 41
pdem Avatar answered Oct 05 '22 03:10

pdem