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

In my app i'm using angular-translate for translating my content.And also in my view i have such d

In my app i'm using 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...)?

In my app i'm using angular-translate

https://github./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...)?

Share Improve this question asked Dec 22, 2015 at 21:23 byCoderbyCoder 9,18427 gold badges120 silver badges259 bronze badges 12
  • try out this plunkr from a similar post plnkr.co/edit/AFpj79M1C6vOewSWLX8J – Jared Commented Dec 22, 2015 at 21:33
  • @Jared will be great, if this is doneable without momentum and other plugins. second: i haven't any ui calendar... – byCoder Commented Dec 22, 2015 at 21:36
  • They are using i18n and ngLocale, I dont think you need momentum. Look at the ng-fr-ca.js file. – Jared Commented Dec 22, 2015 at 21:38
  • 1 Have you loaded the locale for the languages that you are using? These contain the translations that you are seeking. github./angular/angular.js/tree/master/src/ngLocale – Jared Commented Dec 23, 2015 at 15:55
  • 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: github./lgalfaso/angular-dynamic-locale – Jared Commented Dec 23, 2015 at 17:56
 |  Show 7 more ments

2 Answers 2

Reset to default 6 +25

For changing application locale dynamically you need angular-dynamic-locale and you also need other locale files (english is ing 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.

You don't need an external library, in angular, date and number format are translated using angular-i18n: https://docs.angularjs/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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744066919a4552826.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信