Caricamento di bundle per un modulo
È possibile caricare i bundle per un modulo per supportare la localizzazione e l'internazionalizzazione.
Procedura
- Impostare
TranslateModuleper utilizzareMultiTranslateHttpLoader.MultiTranslateHttpLoaderutilizzaHttpClientper caricare le traduzioni, quindi importareHttpClientModuleprima diTranslateModule. -
MultiTranslateHttpLoaderutilizza un elenco di configurazioni di file di traduzione. Ogni configurazione ha i seguenti due parametri:prefix: string = "./assets/store-frontend/i18n/<moduleName>"suffix: string = ".json"Note:- Assicurarsi di creare la cartella <moduleName> in BUNDLE_EXTENSIONS_HOME.
- Si consiglia di creare la configurazione richiesta per
MultiTranslateHttpLoadernel seguente formato. Un approccio simile è stato seguito anche nei moduli di funzione forniti dall'applicazione.// Array of configurations required for the module declared in a class and exported. export class UserBundlesModule { static bundles: Array<any> = [ { prefix: './assets/store-frontend/i18n/user/‘, suffix: '.json' } ]; } /* Configure the custom MultiTranslateHttpLoader as an exported function. If you are using any UI componets from SharedModule, you have to load the SharedModule bundles as well. In this case, you need to import SharedBundlesModule from SharedModule and include them in your custom LoaderFactory function as shown in this code snippet. If you are not using any UI components of SharedModule, then you need not include the SharedBundlesModule in the custom LoaderFactory. */ export function UserBundlesModuleHttpLoaderFactory(http: HttpClient) { const bundles = []; Array.prototype.push.apply(bundles, UserBundlesModule.bundles); Array.prototype.push.apply(bundles, SharedBundlesModule.bundles); return new MultiTranslateHttpLoader(http, bundles); } // Initialize TranslateModule to use the custom LoaderFactory define earlier. TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: UserBundlesModuleHttpLoaderFactory, deps: [HttpClient] } }) // Initialize the TranslateService for your custom module. export class UserModule { constructor(private translateService: TranslateService) { // Get language from appInfo or userInfo or resolvedLocale this.translateService.setDefaultLang(AppInfoService.getFallbackLanguage()); this.translateService.use('en'); } }