Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix title, translate with slot #855

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
richardtreier marked this conversation as resolved.
Show resolved Hide resolved
return defaultValue;
} else {
return JSON.parse(storedItem);
}
} catch (e) {
console.warn('Error parsing local storage value', key, storedItem);
return defaultValue;
Expand Down
10 changes: 9 additions & 1 deletion 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,
illfixit marked this conversation as resolved.
Show resolved Hide resolved
);

constructor(
@Inject(APP_CONFIG) public config: AppConfig,
public titleService: Title,
Expand All @@ -34,7 +42,7 @@ export class ConnectorUiComponent implements OnInit {
) {
this.navItemGroups = this.navItemsBuilder.buildNavItemGroups();
this.translateService.setDefaultLang('en');
this.translateService.use(localStorage.getItem('selectedLanguage') || 'en');
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ export class TranslateWithSlotComponent implements OnChanges, OnDestroy {
textAfter = '';

constructor(private translationService: TranslateService) {
this.splitText();
}

splitText() {
this.key$
.pipe(
switchMap(() => this.translationService.get(this.key)),
switchMap(() => this.translationService.stream(this.key)),
takeUntil(this.ngOnDestroy$),
)
.subscribe((text) => {
Expand Down
Loading