如何通过ot编译在Angular中提供备用的i18n语言链接? [英] How to provide an alternate i18n language link in Angular with aot compiling?
问题描述
我目前正在开发Angular应用程序的i18n.我在在这里中所述,通过xlf文件使用AOT编译来创建预编译的应用程序一个>.在构建中,我提供了特定于语言的基本href(使用--base-href
参数).例如,这意味着,我最终使用以下网址路径获得了两个特定于语言的应用程序:
I am currently working on i18n of my Angular app. I use AOT compiling with xlf files to create pre-compiled apps as described here. In the build, I provide a language specific base href (using the --base-href
parm). For example, this means, I end up with two language specific apps using the following url paths:
- /app_path/en/...
- /app_path/de/...
现在,我想通过在活动网址中将en
替换为de
来提供应用程序中相应替代语言的链接.我使用了路由器注入,因此可以访问其url属性,但是此属性并没有给我完整的URL ,包括base-href.
Now I want to provide a link to the respective alternate language within my app by replacing for example en
by de
in the active url. I used the Router injection, so I can access its url property, but this property does not give me the full url including the base-href.
如何从Angular应用程序中找到基本网址?
How can I find out the base url from within an Angular app?
或者,有什么方法可以找出应用程序的构建语言吗?我可以通过某种方式从xliff文件访问target-language属性吗?
Alternatively, is there a way to I find out language the app was built for? Can I access the target-language property from the xliff files somehow?
推荐答案
在为各种语言编译应用程序时,您需要在ng构建过程中设置语言环境:
When you compile your app for the various languages you set the locale in the ng build process:
./node_modules/.bin/ngc --i18nFile=./locale/messages.es.xlf --locale=es --i18nFormat=xlf
您可以通过将其注入您的组件来访问此语言环境:
You can get access to this locale by injecting it to your component:
import { Inject, LOCALE_ID } from '@angular/core';
...
constructor(@Inject(LOCALE_ID) locale: string, ...
然后,您可以使用语言环境字符串来确定当前语言.在上面的示例中,语言环境设置为"es"
Then you can use the locale string to determine the current language. In the example above the locale was set to "es"
希望这会有所帮助
这篇关于如何通过ot编译在Angular中提供备用的i18n语言链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!