我们正在使用多个角度MFE项目(使用nx),在成功加载主机和远程模块并显示页面(通过路由)之后,下一步是翻译。
我有一个正在加载angular-i18next的共享翻译模块

import { CommonModule } from '@angular/common';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { I18NextModule } from 'angular-i18next';
import { I18N_PROVIDERS } from './i18next.config';
@NgModule({
imports: [CommonModule, I18NextModule.forRoot()],
providers: [I18N_PROVIDERS],
})
export class UtilTranslationsModule {
// I setup the module providers for the root application.
static forRoot(): ModuleWithProviders<UtilTranslationsModule> {
return ({
ngModule: UtilTranslationsModule,
providers: [I18N_PROVIDERS],
});
}使用i18Next选项:
import { APP_INITIALIZER, LOCALE_ID } from '@angular/core';
import { defaultInterpolationFormat, I18NextModule, I18NEXT_SERVICE, ITranslationService } from 'angular-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';
import { Languages } from './languages.constant';
/*
* Platform and Environment providers/directives/pipes
*/
const i18nextOptions = {
whitelist: Object.values(Languages),
fallbackLng: 'en',
debug: false, // true, // set debug?
returnEmptyString: false,
ns: ['common', 'error', 'auth'],
defaultNS: 'common',
interpolation: {
format: I18NextModule.interpolationFormat(defaultInterpolationFormat),
},
// backend plugin options
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json',
addPath: 'locales/add/{{lng}}/{{ns}}',
crossDomain: true,
},
// lang detection plugin options
detection: {
// order and from where user language should be detected
order: ['localStorage', 'cookie'],
// keys or params to lookup language from
lookupCookie: 'lang',
// cache user language on
caches: ['localStorage', 'cookie'],
// optional expire and domain for set cookie
cookieMinutes: 10080, // 7 days
cookieDomain: 'I18NEXT_LANG_COOKIE_DOMAIN',
},
};该模块在webpack sharedMappings.register(tsConfigPath, ['@tgc/translations'], workspaceRootPath);中共享。
文件通过i18next-http-backend库加载。主机正确加载翻译,但remote模块没有显示任何翻译,( 'auth:login.title' | i18nextCap)
我发现了一些类似的问题,但它只包含资产问题,比如angular mfe assets issue。翻译文件也作为project.json中的资产导出。
希望问题是清楚的,如果不是,我总是可以在这里添加一个示例代码。

发布于 2022-01-14 16:51:04
我刚发现我必须让某个文件可以共享:
'angular-i18next': { singleton: true, strictVersion: true, requiredVersion: 'auto' },

https://stackoverflow.com/questions/70712020
复制相似问题