在运行时更改Angular中的语言环境? [英] Changing locale in Angular at runtime?

查看:233
本文介绍了在运行时更改Angular中的语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行一些有关在运行时更改Angular语言环境的研究,并发现了以下线程:

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?

推荐答案

我敢打赌,您正在搜索它,因为今天您要将@angular/material升级到beta.10,而DatePicker现在使用LOCALE_ID,不是吗? ?我也在寻找替代品,没有运气.

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方法基于编译器,为不同的语言生成不同的包,因此该体系结构似乎不支持runtime解决方案. Angular 6编译器正在完成一些工作,以支持runtime翻译更改.

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

目前,我喜欢ngx-translate方法,他们似乎订阅了lang更改并更新了在该指令中注册的DOM节点,并且我计划根据他们的 changelog中阅读了一些内容),因此我们可能需要深入研究要翻译的组件.

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 components 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 components we want to translate.

如果采用ngx-translate方法,让我们在github上共享内容以构建新管道;)

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

这篇关于在运行时更改Angular中的语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆