如果您已经在使用像@ngx-translate/core这样的东西,那么您可以创建自己的映射而不是添加更多的外部库。
例如:
function generateI18nRoutes(
elements: Array<{ i18nPaths: string[]; component: any; data: object; canActivate?:
[] }>
): Routes {
return elements.reduce(
(accumulator, currentValue) => [
...accumulator,
...currentValue.i18nPaths.map((path: string) => ({
path,
component: currentValue.component,
data: currentValue.data,
canActivate: currentValue.canActivate
}))
],
[]
);
}
然后像这样使用:
const routes: Routes = [
{
path: "admin",
component: AdminLayoutComponent,
canActivate: [AdminAuthGuard],
children: generateI18nRoutes([{ i18nPaths: [""], component: HomeComponent, data: { title: "ADMIN" } }])
},
{
path: "",
component: GeneralLayoutComponent,
children: generateI18nRoutes([
{ i18nPaths: [""], component: HomeComponent, data: { title: "HOME" } },
{
i18nPaths: ["sign-in", "iniciar-sesión", "iniciar-sessão"],
component: SignInComponent,
data: { title: "SIGN_IN" }
}
])
},
{
path: "**",
component: PageNotFoundComponent,
data: { title: "PAGE_NOT_FOUND" }
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
ngx-translate
,我希望能够在那里看到一些解决方案,而不是再增加一个依赖项。 - Bogacngx-i18nsupport
软件包对于简化流程非常有帮助。更多信息请参见此处。唯一无法帮助的是路由字符串。如果您一直在创建i18n方式的应用程序,则路由变量不是问题,路由变量可以是翻译版本,但路由字符串不行。 - Bogacnpm scripts
来提取需要翻译的字符串,然后构建/服务于选定的语言。由于路由字符串是唯一无法在此过程中进行优化的部分,因此我已经在所有可用语言中创建了所有路由定义,并在构建所需语言之前将那些需要的注释掉,不需要使用的则取消注释。这是丑陋的部分,这是因为我使用了具有子路由的惰性加载模块。如果没有,我可以使用 @estus 推荐的软件包。如果需要更多信息,请联系我。 - Bogac