Skip to content

Commit

Permalink
chore: refactor selectedLanguage
Browse files Browse the repository at this point in the history
  • Loading branch information
illfixit committed Oct 2, 2024
1 parent 6e0f1e2 commit c298c50
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/app/core/utils/i18n-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface AvailableLanguage {
code: string;
name: string;
}

export const supportedLanguages: AvailableLanguage[] = [
{code: 'en', name: 'English'},
{code: 'de', name: 'Deutsch'},
];

export const isLanguageSupported = (value: unknown): value is string =>
supportedLanguages.map((it) => it.code).includes(value as string);
7 changes: 6 additions & 1 deletion src/app/core/utils/local-storage-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export class LocalStorageUtils {
const storedItem = localStorage.getItem(key);

try {
return storedItem == null ? defaultValue : JSON.parse(storedItem);
if (storedItem == null) {
this.saveData(key, defaultValue);
return defaultValue;
} else {
return JSON.parse(storedItem);
}
} catch (e) {
console.warn('Error parsing local storage value', key, storedItem);
return defaultValue;
Expand Down
15 changes: 9 additions & 6 deletions src/app/routes/connector-ui/connector-ui.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {map, shareReplay} from 'rxjs/operators';
import {TranslateService} from '@ngx-translate/core';
import {NavItemGroup} from 'src/app/core/services/models/nav-item-group';
import {NavItemsBuilder} from 'src/app/core/services/nav-items-builder';
import {isLanguageSupported} from 'src/app/core/utils/i18n-utils';
import {LocalStoredValue} from 'src/app/core/utils/local-stored-value';
import {APP_CONFIG, AppConfig} from '../../core/config/app-config';
import {LoginPollingService} from '../../core/services/login-polling.service';

Expand All @@ -24,6 +26,12 @@ export class ConnectorUiComponent implements OnInit {

navItemGroups: NavItemGroup[] = [];

selectedLanguage = new LocalStoredValue<string>(
'en',
'selectedLanguage',
isLanguageSupported,
);

constructor(
@Inject(APP_CONFIG) public config: AppConfig,
public titleService: Title,
Expand All @@ -34,12 +42,7 @@ export class ConnectorUiComponent implements OnInit {
) {
this.navItemGroups = this.navItemsBuilder.buildNavItemGroups();
this.translateService.setDefaultLang('en');
let selectedLanguage = localStorage.getItem('selectedLanguage');
if (!selectedLanguage) {
selectedLanguage = 'en';
localStorage.setItem('selectedLanguage', selectedLanguage);
}
this.translateService.use(JSON.parse(selectedLanguage));
this.translateService.use(this.selectedLanguage.value);
}

ngOnInit() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import {Component, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {
AvailableLanguage,
isLanguageSupported,
supportedLanguages,
} from 'src/app/core/utils/i18n-utils';
import {LocalStoredValue} from '../../../core/utils/local-stored-value';

interface AvailableLanguage {
code: string;
name: string;
}

@Component({
selector: 'app-language-selector',
templateUrl: './language-selector.component.html',
})
export class LanguageSelectorComponent implements OnInit {
supportedLanguages: AvailableLanguage[] = [
{code: 'en', name: 'English'},
{code: 'de', name: 'Deutsch'},
];

selectedLanguage = new LocalStoredValue<string>(
'en',
'selectedLanguage',
(value): value is string =>
this.supportedLanguages.map((it) => it.code).includes(value as string),
isLanguageSupported,
);

supportedLanguages = supportedLanguages;

constructor(private translateService: TranslateService) {}

ngOnInit(): void {
Expand Down

0 comments on commit c298c50

Please sign in to comment.