斯巴达克斯如何进行动态翻译?

发布于 2025-01-11 08:37:01 字数 2270 浏览 0 评论 0原文

我正在使用斯巴达克斯 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2025-01-18 08:37:01

看起来这里已经讨论了动态加载翻译的类似情况:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文