7

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...)?

brabertaser19
  • 5,678
  • 16
  • 78
  • 184
  • try out this plunkr from a similar post http://plnkr.co/edit/AFpj79M1C6vOewSWLX8J – Jared Dec 22 '15 at 21:33
  • @Jared `will be great, if this is doneable without momentum and other plugins`. second: i haven't any ui calendar... – brabertaser19 Dec 22 '15 at 21:36
  • They are using i18n and ngLocale, I dont think you need momentum. Look at the ng-fr-ca.js file. – Jared Dec 22 '15 at 21:38
  • I think your question is a duplicate of http://stackoverflow.com/questions/29937780/angularjs-translate-format-dynamic-dates – Jared Dec 22 '15 at 21:43
  • 1
    Have you loaded the locale for the languages that you are using? These contain the translations that you are seeking. https://github.com/angular/angular.js/tree/master/src/ngLocale – Jared Dec 23 '15 at 15:55
  • @Jared i've loaded all this locations. But then it translate my month names depending on browser language. And this is wrong. It must translate due to angular-translate language settings. – brabertaser19 Dec 23 '15 at 17:45
  • Sounds like you need to use angular-dynamic-locale so that you can set the locale at run time. You cant set it at run time using ngLocale. – Jared Dec 23 '15 at 17:52
  • 3
    FWIW angular-translate does NOT translate any date or time settings, you must use ngLocale or angular-dynamic-locale to do that. In your case you MUST use the latter, angular-dynamic-locale, to achieve your goal. To my knowledge, there is no other way. Link: https://github.com/lgalfaso/angular-dynamic-locale – Jared Dec 23 '15 at 17:56
  • @Jared, ok, thank's, i'll try... – brabertaser19 Dec 23 '15 at 20:17
  • Is this http://stackoverflow.com/questions/29937780/angularjs-translate-format-dynamic-dates what you are looking for? – Tome Pejoski Dec 29 '15 at 09:32
  • @tomepejo no! it's not – brabertaser19 Dec 30 '15 at 11:31
  • You must take a look at this GitHub issue: https://github.com/angular/angular.js/tree/master/src/ngLocale – Avijit Gupta Jan 01 '16 at 17:40

2 Answers2

6

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.

Poyraz Yilmaz
  • 5,847
  • 1
  • 26
  • 28
1

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?

Community
  • 1
  • 1
pdem
  • 3,880
  • 1
  • 24
  • 38
  • no... it's a bad idea in my case (because i need to see month name without dot, from capital letter etc...) – brabertaser19 Jan 11 '16 at 08:08
  • If it is a dot from the format, you can provide your own: from the documentation: "'longDate': equivalent to 'MMMM d, y' for en_US locale (e.g. September 3, 2010)" so use 'MMMM' to hav full month name. Maybe i don't understand your need and it is not enough, in that case, you may create your own translation inspired from the source code of the angular libraries: use $provide.value("$locale", { "DATETIME_FORMATS": { "AMPMS": [ "AM", "PM" ], "DAY": [ "dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi" ... – pdem Jan 11 '16 at 08:35