Romanchuk / angular-i18next

angular v10+ integration with i18next v19.4+
MIT License
130 stars 33 forks source link

I18NextService.language is an empty string #41

Closed scottwalter-nice closed 4 years ago

scottwalter-nice commented 4 years ago

If I have an initializer setup like this


const options = {
    whitelist: ['en', 'ru'],
    fallbackLng: 'en',
    lng: 'en',
    debug: true, // set debug?
    returnEmptyString: false,
    ns: [
    'translation',
    'validation',
    'error',

    // 'feature.rich_form'
    ]
}

export function AppInitializerFactory(i18next:  I18NextService) {
    return () => {
        const i18NextPromise: Promise<I18NextLoadResult> = i18next
        .init(options);

        return i18NextPromise;
    }
};

  export const I18N_PROVIDERS = [
    { provide: APP_INITIALIZER, useFactory: AppInitializerFactory, deps: [I18NEXT_SERVICE], multi: true },
    { provide: LOCALE_ID, deps: [I18NEXT_SERVICE], useFactory: localeIdFactory }
  ];

I get a value for I18NextService.language. However if I move the logic into a class then I get an empty string for I18NextService.language.

export class AppInitializer {
    constructor(
        private i18next: I18NextService) {}

    load()  {
            const i18NextPromise: Promise<I18NextLoadResult> = this.i18next
            .init(options);

            return i18NextPromise;
        }
}

export function AppInitializerFactory(initializer:  AppInitializer) {
    return () => initializer.load();
};

export const I18N_PROVIDERS = [
    AppInitializer,
    { provide: APP_INITIALIZER, useFactory: AppInitializerFactory, deps: [AppInitializer], multi: true },
    { provide: LOCALE_ID, deps: [I18NEXT_SERVICE], useFactory: localeIdFactory }
];

Any ideas why the language is an empty string when I move it into a class?

scottwalter-nice commented 4 years ago

Oh I think I found the issue. I needed to change my provider from:

AppInitializer to { provide: AppInitializer, useClass: AppInitializer, deps: [I18NEXT_SERVICE]},

Romanchuk commented 4 years ago

@scottwalter-nice may be =)

scottwalter-nice commented 4 years ago

@Romanchuk Really liking your wrapper btw.