internationalization - Spartacus Storefront 多站点 I18n 与后端
问题描述
在进行 I18n 时,我们的 MultiSite Spartacus 设置遇到了一些问题。
我们希望为每个站点提供不同的翻译,因此我们将它们放在一个 API 上,该 API 可以返回依赖于 baseSite 的消息,例如:
backend.org/baseSiteX/messages?group=common
但是 Spartacus 设置不允许我们通过 baseSite?我们可以通过{{lng}}
and{{ns}}
,但不能通过 baseSite。
请参阅https://sap.github.io/spartacus-docs/i18n/#lazy-loading 我们可以通过覆盖i18nextInit
来做到这一点,但我不确定如何实现这一点。在文档中,它说您可以
crossOrigin: true
在配置中使用,但这似乎不起作用。类型检查说它不受支持,它仍然显示 uw CORS 问题
有人对这些问题有想法吗?
解决方案
目前仅支持语言 {{lng}} 和块名称 {{ns}} 作为i18n.backend.loadPath
配置中的动态参数。
为了实现您的目标,您可以实现自定义 Spartacus以根据以下值CONFIG_INITIALIZER
填充您的配置:i18n.backend.loadPath
BaseSiteService.getActive()
@Injectable({ providedIn: 'root' })
export class I18nBackendPathConfigInitializer implements ConfigInitializer {
readonly scopes = ['i18n.backend.loadPath']; // declare config key that you will resolve
readonly configFactory = () => this.resolveConfig().toPromise();
constructor(protected baseSiteService: BaseSiteService) {}
protected resolveConfig(): Observable<I18nConfig> {
return this.baseSiteService.getActive().pipe(
take(1),
map((baseSite) => ({
i18n: {
backend: {
// initialize your i18n backend path using the basesite value:
loadPath: `https://backend.org/${baseSite}/messages?lang={{lng}}&group={{ns}}`,
},
},
}))
);
}
}
并在您的模块中提供它(即在 app.module 中):
@NgModule({
providers: [
{
provide: CONFIG_INITIALIZER,
useExisting: I18nBackendPathConfigInitializer,
multi: true,
},
],
/* ... */
})
注意:上述解决方案假定活动基站仅在应用启动时设置一次(默认情况下在 Spartacus 中就是这种情况)。
推荐阅读
- python - openpyxl 只读模式不能 iter_cols
- python - 熊猫数据透视表输出具有整数和浮点数
- python - 数据库中不存在表“登录”
- python - 为什么re.sub在python中替换后添加空格?如何摆脱空间?
- java - 在 Java 进程中使用 Micrometer 计时器?
- rabbitmq - Yocto 项目 rabbitmq-server 配方构建
- c++ - 为什么在使用 lambda 递归时总是需要 [&]?
- c# - Java 验证签名 C# 等效
- javascript - 在 Console 日志的返回值中返回 undefiend
- laravel - 在laravel中以多种形式验证组中的至少一个复选框