斯巴达克斯如何进行动态翻译?
我正在使用斯巴达克斯 4.2 。我想使用我自己的自定义端点提供翻译服务。我正在使用 APP_INITIALIZER 令牌进行 api 调用。我的问题是如何在 ProvideConfig() 中设置结果。或者还有其他方法来动态提供翻译服务吗? 当我收到 api 的响应时,我想更新 mytranslationsEN。
斯巴达克斯-configuration.module.ts
import { HttpClient } from '@angular/common/http';
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { translationChunksConfig, translations } from '@spartacus/assets';
import {
FeaturesConfig,
I18nConfig,
OccConfig,
provideConfig,
SiteContextConfig,
TranslationResources,
} from '@spartacus/core';
import {
defaultCmsContentProviders,
layoutConfig,
mediaConfig,
} from '@spartacus/storefront';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { mytranslationsfr } from 'src/assets/translations/fr/index.fr';
export let mytranslationsEN: TranslationResources = {};
function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
return () => httpClient.get('http://localhost:3000/translations')
.pipe(
tap(translations => {
mytranslationsEN= {
product:{...translations}
};
})
);
}
@NgModule({
declarations: [],
imports: [],
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeAppFactory,
deps: [HttpClient],
multi: true,
},
provideConfig(layoutConfig),
provideConfig(mediaConfig),
...defaultCmsContentProviders,
provideConfig(<OccConfig>{
backend: {
occ: {
baseUrl: 'https://localhost:9002',
},
},
}),
provideConfig(<SiteContextConfig>{
context: {
urlParameters: ['baseSite', 'language', 'currency'],
baseSite: ['apparel-uk-spa'],
},
}),
provideConfig(<SiteContextConfig>{
context: {
currency: ['GBP'],
language: ['en'],
},
}),
provideConfig(<I18nConfig>{
i18n: {
resources: translations,
chunks: translationChunksConfig,
fallbackLang: 'en',
},
}),
provideConfig(<I18nConfig>{
i18n: {
resources: {
en: mytranslationsEN ,
fr: mytranslationsfr,
},
fallbackLang: 'en',
},
}),
provideConfig(<FeaturesConfig>{
features: {
level: '4.2',
},
}),
],
})
export class SpartacusConfigurationModule {}
I am using Spartacus 4.2 . I want to serve translations using my own custom endpoint. I am making the api call using APP_INITIALIZER token. My question is how will I set the result in provideConfig(). Or is there any other approach to serve translations dynamically?
I want to update mytranslationsEN when I have response from the api.
spartacus-configuration.module.ts
import { HttpClient } from '@angular/common/http';
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { translationChunksConfig, translations } from '@spartacus/assets';
import {
FeaturesConfig,
I18nConfig,
OccConfig,
provideConfig,
SiteContextConfig,
TranslationResources,
} from '@spartacus/core';
import {
defaultCmsContentProviders,
layoutConfig,
mediaConfig,
} from '@spartacus/storefront';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { mytranslationsfr } from 'src/assets/translations/fr/index.fr';
export let mytranslationsEN: TranslationResources = {};
function initializeAppFactory(httpClient: HttpClient): () => Observable<any> {
return () => httpClient.get('http://localhost:3000/translations')
.pipe(
tap(translations => {
mytranslationsEN= {
product:{...translations}
};
})
);
}
@NgModule({
declarations: [],
imports: [],
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeAppFactory,
deps: [HttpClient],
multi: true,
},
provideConfig(layoutConfig),
provideConfig(mediaConfig),
...defaultCmsContentProviders,
provideConfig(<OccConfig>{
backend: {
occ: {
baseUrl: 'https://localhost:9002',
},
},
}),
provideConfig(<SiteContextConfig>{
context: {
urlParameters: ['baseSite', 'language', 'currency'],
baseSite: ['apparel-uk-spa'],
},
}),
provideConfig(<SiteContextConfig>{
context: {
currency: ['GBP'],
language: ['en'],
},
}),
provideConfig(<I18nConfig>{
i18n: {
resources: translations,
chunks: translationChunksConfig,
fallbackLang: 'en',
},
}),
provideConfig(<I18nConfig>{
i18n: {
resources: {
en: mytranslationsEN ,
fr: mytranslationsfr,
},
fallbackLang: 'en',
},
}),
provideConfig(<FeaturesConfig>{
features: {
level: '4.2',
},
}),
],
})
export class SpartacusConfigurationModule {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这里已经讨论了动态加载翻译的类似情况:
https://stackoverflow.com/a/65224726/12566149
It looks like a similar situation with dynamically loading translations has been discussed here:
https://stackoverflow.com/a/65224726/12566149