Skip to content

Commit

Permalink
refacto: change language without reload + external language setter
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgreiner committed Nov 13, 2024
1 parent b1729b0 commit 5e48450
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
{{ 'app.user-manual' | translate }}
</a>
<sbb-select
(selectionChange)="changeLanguage($event.value)"
[value]="language"
(selectionChange)="language = $event.value"
[value]="localStorageLanguage"
class="language-selector"
>
<sbb-option value="en">🇬🇧 English</sbb-option>
Expand All @@ -35,8 +35,8 @@
</sbb-usermenu>
<sbb-menu #menu="sbbMenu">
<sbb-select
(selectionChange)="changeLanguage($event.value)"
[value]="language"
(selectionChange)="language = $event.value"
[value]="localStorageLanguage"
class="language-selector language-selector-menu"
(click)="$event.stopPropagation()"
(keydown)="$event.stopPropagation()"
Expand Down
19 changes: 12 additions & 7 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {NetzgrafikDto} from "./data-structures/business.data.structures";
import {Operation} from "./models/operation.model";
import {LabelService} from "./services/data/label.serivce";
import {NodeService} from "./services/data/node.service";
import {I18nService} from "./core/i18n/i18n.service";

@Component({
selector: "sbb-root",
Expand All @@ -22,6 +23,7 @@ export class AppComponent {
readonly version = packageJson.version;
readonly environmentLabel = environment.label;
readonly authenticated: Promise<unknown>;
protected localStorageLanguage: string;

projectInMenu: Observable<ProjectDto | null>;

Expand All @@ -45,8 +47,10 @@ export class AppComponent {
private trainrunSectionService: TrainrunSectionService,
private nodeService: NodeService,
private labelService: LabelService,
private i18nService: I18nService,
) {
this.language = localStorage.getItem("i18nLng");
this.i18nService.setLanguage();
this.localStorageLanguage = localStorage.getItem("i18nLng");
if (!this.disableBackend) {
this.authenticated = authService.initialized;
}
Expand All @@ -58,14 +62,15 @@ export class AppComponent {
}
}

changeLanguage(language: string) {
localStorage.setItem("i18nLng", language);
this.language = language;
location.reload();
@Input()
set language(value: string) {
if (value !== this.localStorageLanguage) {
localStorage.setItem("i18nLng", value);
this.i18nService.setLanguage();
this.localStorageLanguage = localStorage.getItem("i18nLng");
}
}

@Input() language: string;

@Input()
get netzgrafikDto() {
return this.dataService.getNetzgrafikDto();
Expand Down

0 comments on commit 5e48450

Please sign in to comment.