【发布时间】:2019-02-19 08:14:24
【问题描述】:
我正在使用 Angular 的 i18n 设置构建一个小型 Angular 应用程序。一切正常,除了 url 路径和 slug 的翻译。我尝试了一种可能的解决方案,为每种语言提供一个路由模块(如此处所述),但这不起作用。
我以为我可以做类似以下的事情,但我不知道在哪里注入LOCALE_ID:
app-routing.module.ts
import { LOCALE_ID, NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MainComponent } from './main/main.component';
const i18nRoutes = {
de: {
main: 'inhalte',
// ...
},
fr: {
main: 'contenu',
// ...
}
}
const currentLanguage = i18nRoutes[ LOCALE_ID ]; // <-- Apparently not working, since I have to inject LOCALE_ID. But where?
const routes: Routes = [
{
path: '',
redirectTo: currentLanguage.main,
pathMatch: 'full'
},
{
path: currentLanguage.main + '/:key',
component: MainComponent
}
// ...
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
更新说明
在angular.json 中,我为每种语言的构建过程设置了配置(取自here,查看2018 年的修改)
angular.json
{
// ...
"projects": {
"my-app": {
// ...
"architect": {
"build": {
// ...
"configurations": {
// ...
"de": {
"i18nFile": "src/i18n/de.xliff",
"outputPath": "dist/de",
"i18nFormat": "xlf",
"i18nLocale": "de",
"baseHref": "/de/"
// ...
},
"fr": {
"i18nFile": "src/i18n/fr.xliff",
"outputPath": "dist/fr",
"i18nFormat": "xlf",
"i18nLocale": "fr",
"baseHref": "/fr/",
// ...
}
}
}
}
}
}
为了一次构建所有应用程序,我输入npm run buildall,它在package.json 中执行以下操作:
package.json
{
"name": "my-app",
// ...
"scripts": {
// ...
"buildall": "for lang in de fr;do ng build --configuration=$lang; done"
}
}
在dist文件夹的子目录中生成所有应用程序就好了。
所以,回到我原来的问题:提供的answer by Exterminator 不符合我的需求,因为
- 我无法在引导时设置固定的语言环境
- 在构造函数中注入
LOCALE_ID为时已晚,因为我需要app-routing.module.ts中的值
我希望我已经解释得够多了。 但也许我完全误解了一些东西。无论如何,提前感谢您的帮助。我还在学习,我必须承认有一些概念对我来说仍然是模糊的。
【问题讨论】:
标签: angular routing internationalization