Skip to content

Commit

Permalink
Use tranlate provider method
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel committed Dec 16, 2024
1 parent e54f078 commit 1ccd39d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,37 @@ export class OpenSlidesMainComponent implements OnInit {
this.loadCustomIcons();
}

private loadTranslation(): void {
private async loadTranslation(): Promise<void> {
// manually add the supported languages
this.translate.addLangs(Object.keys(availableTranslations));
this.translate.setDefaultLang(`en`);
// get the browsers default language
const browserLang = this.translate.getBrowserLang() as string;
let currentLang = `en`;

// get language set in local storage
this.storageService.get(CURRENT_LANGUAGE_STORAGE_KEY).then(lang => {
currentLang = lang as string;
if (lang && this.translate.getLangs().includes(lang as string)) {
this.translate.use(lang as string);
} else {
// try to use the browser language if it is available. If not, uses english.
this.translate.use(this.translate.getLangs().includes(browserLang) ? browserLang : `en`);
}
const lang = (await this.storageService.get(CURRENT_LANGUAGE_STORAGE_KEY)) as string;
currentLang = lang;
if (lang && this.translate.getLangs().includes(lang as string)) {
this.translate.use(lang as string);
} else {
// try to use the browser language if it is available. If not, uses english.
this.translate.use(this.translate.getLangs().includes(browserLang) ? browserLang : `en`);
}

// set date-fns locale
this.updateLocaleByName(
this.translate.currentLang ? this.translate.currentLang : this.translate.defaultLang
);
});
// set date-fns locale
this.updateLocaleByName(this.translate.currentLang ? this.translate.currentLang : this.translate.defaultLang);

// listen for language changes
this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.storageService.set(CURRENT_LANGUAGE_STORAGE_KEY, event.lang);
currentLang = event.lang;

// update date-fns locale
this.updateLocaleByName(event.lang);

currentLang = event.lang;
this.updateLocaleByName(currentLang);
});

this.ctService.customTranslationSubject.subscribe(() => {
this.translate.reloadLang(`en`);
this.translate.reloadLang(currentLang);
});
}
Expand Down
16 changes: 12 additions & 4 deletions client/src/app/openslides-main-module/openslides-main.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { HttpClient, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ServiceWorkerModule } from '@angular/service-worker';
import { provideTranslateService, TranslateLoader } from '@ngx-translate/core';
import { GlobalSpinnerModule } from 'src/app/site/modules/global-spinner';
import { OpenSlidesTranslationModule } from 'src/app/site/modules/translations';
import { environment } from 'src/environments/environment';

import { CustomTranslationService } from '../site/modules/translations/custom-translation.service';
import { PruningTranslationLoader } from '../site/modules/translations/translation-pruning-loader';
import { WaitForActionDialogModule } from '../site/modules/wait-for-action-dialog';
import { WaitForActionDialogService } from '../site/modules/wait-for-action-dialog/services';
import { OpenSlidesMainComponent } from './components/openslides-main/openslides-main.component';
Expand All @@ -33,7 +35,6 @@ const NOT_LAZY_LOADED_MODULES = [MatSnackBarModule, GlobalSpinnerModule, WaitFor
BrowserModule,
OpenSlidesMainRoutingModule,
BrowserAnimationsModule,
OpenSlidesTranslationModule.forRoot(),
...NOT_LAZY_LOADED_MODULES,
ServiceWorkerModule.register(`sw.js`, {
enabled: environment.production,
Expand All @@ -46,7 +47,14 @@ const NOT_LAZY_LOADED_MODULES = [MatSnackBarModule, GlobalSpinnerModule, WaitFor
WaitForActionDialogService,
{ provide: APP_INITIALIZER, useFactory: AppLoaderFactory, deps: [AppLoadService], multi: true },
httpInterceptorProviders,
provideHttpClient(withInterceptorsFromDi())
provideHttpClient(withInterceptorsFromDi()),
provideTranslateService({
loader: {
provide: TranslateLoader,
useClass: PruningTranslationLoader,
deps: [CustomTranslationService, HttpClient]
}
})
]
})
export class OpenSlidesMainModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { PruningTranslationLoader } from './translation-pruning-loader';
export class OpenSlidesTranslationModule {
public static forRoot(): ModuleWithProviders<TranslateModule> {
return TranslateModule.forRoot({
defaultLanguage: `en`,
loader: {
provide: TranslateLoader,
useClass: PruningTranslationLoader,
Expand All @@ -24,7 +23,7 @@ export class OpenSlidesTranslationModule {
// no config store for child.
public static forChild(): ModuleWithProviders<TranslateModule> {
return TranslateModule.forChild({
defaultLanguage: `en`,
extend: true,
loader: {
provide: TranslateLoader,
useClass: PruningTranslationLoader,
Expand Down

0 comments on commit 1ccd39d

Please sign in to comment.