最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing locale in Angular at runtime? - Stack Overflow

programmeradmin1浏览0评论

I was doing some research on changing Angular locale at runtime and found this thread: How to set locale in DatePipe in Angular 2? .
Is it still the case that there is no way to change locale at runtime? I have an application that features several pipes to format number, date and language depending on the locale, but implementing many custom pipes for each locale -- rather than having the ability to change it during runtime--seems ridiculous.
What solutions would you purpose?

I was doing some research on changing Angular locale at runtime and found this thread: How to set locale in DatePipe in Angular 2? .
Is it still the case that there is no way to change locale at runtime? I have an application that features several pipes to format number, date and language depending on the locale, but implementing many custom pipes for each locale -- rather than having the ability to change it during runtime--seems ridiculous.
What solutions would you purpose?

Share Improve this question asked Aug 30, 2017 at 14:19 MadCatm2MadCatm2 1,0015 gold badges26 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I bet you are searching for this because today you're upgrading @angular/material to beta.10 and the DatePicker now uses LOCALE_ID, isn't it? I'm looking for an alternative too and had no luck.

Angular 4 i18n approach is based on the piler, generating different bundles for different languages, so a runtime solution seems not supported by that architecture. There's work being done in Angular 6 piler to support runtime translation changes.

For the moment, I like ngx-translate approach, they seem to subscribe to lang changes and update the DOM nodes registered with the Directive, and I'm planning to build my own DateFormat pipe based on their TranslateDirective, but about services and ponents using the LOCALE_ID, I think there's no chance to change their behavior at runtime. Probably there are new exported resources for extension and customization (I did read something in the changelog), so we may need to go deep into the ponents we want to translate.

If you take the ngx-translate approach, let's share stuff at github to build new pipes ;)

发布评论

评论列表(0)

  1. 暂无评论