robisim74 / angular-l10n

Angular library to translate texts, dates and numbers
MIT License
380 stars 59 forks source link

Upgrade from 8 to 9 : json file not loading #290

Closed Lempkin closed 4 years ago

Lempkin commented 4 years ago

Hi,

I upgraded lib version from 8.1.2 to 9.1.0, I've adjusted configuration, but translations are not working. When I debug, I can see that when : this.translationLoader.get(language, provider)

this fails : provider.asset[language]

and this is thrown : throwError(l10nError(L10nDefaultTranslationLoader_1, 'Asset not found'));

At this time, "providers" variable still contains {name: "app", asset: "./assets/app"} Don't know if this is normal, I assume that providers.asset should have been replaced with json files ?

Here my config in app.module.ts :

export const l10nConfig: L10nConfig = {
  format: 'language-region',
  providers: [{ name: 'app', asset: './assets/app' }],
  fallback: false,
  cache: false,
  keySeparator: '.',
  defaultLocale: { language: 'fr-FR', currency: 'EUR' },
  schema: [
    { locale: { language: 'en-US', currency: 'USD' }, dir: 'ltr', text: 'United States' },
    { locale: { language: 'fr-FR', currency: 'EUR' }, dir: 'ltr', text: 'France' },
  ],
  defaultRouting: true,
};

export function initL10n(l10nLoader: L10nLoader): () => Promise<void> {
  return () => l10nLoader.init();
}

@NgModule({
imports: [
    L10nTranslationModule.forRoot(l10nConfig),
    L10nIntlModule
    ...
 ],
providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: initL10n,
      deps: [L10nLoader],
      multi: true,
    },
    ...

json files are in : src/assets/app-en-US.json src/assets/app-fr-FR.json

On home page template for instance I use translation like : <h2 l10nTranslate>dashboard.introduction.news.title</h2>

And so obviously when I'm on the website I can see only keys, nothing is translated.

When I try with this provider from your example :

providers: [{ name: 'app', asset: i18nAsset }],
...

 const i18nAsset = {
  'en-US': {
    greeting: 'Hello world!',
    whoIAm: 'I am {{name}}',
  },
  'it-IT': {
    greeting: 'Ciao mondo!',
    whoIAm: 'Sono {{name}}',
  },
};

This is working fine.

Can you help to find what's going wrong? When I was with version 8.1.2 everything worked well. Thx

Lempkin commented 4 years ago

Ok I got it, I missed the translationLoader part.