Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgreiner committed Jul 25, 2024
1 parent a880244 commit d87b449
Show file tree
Hide file tree
Showing 79 changed files with 2,988 additions and 2,084 deletions.
10 changes: 5 additions & 5 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<sbb-header-lean
label="Netzgrafik-Editor"
[subtitle]="tVersion"
[subtitle]="'app.version' | translate:{ 'version': version }"
class="sbb-header-fixed-columns noprint"
>
<sbb-app-chooser-section [label]="tUserGuide" class="noprint">
<sbb-app-chooser-section [label]="'app.user-guide' | translate" class="noprint">
<a href="https://github.com/SchweizerischeBundesbahnen/netzgrafik-editor-frontend/blob/main/documentation/USERMANUAL.md"
class="nav-element"
target="_blank"
>
{{ tUserManual }}
{{ 'app.user-manual' | translate }}
</a>
<select (change)="changeLocale($event.target.value)" [value]="locale" class="language-selector">
<option value="en">🇬🇧 English</option>
Expand All @@ -32,7 +32,7 @@
<sbb-menu #menu="sbbMenu">
<button sbb-menu-item (click)="logout()">
<sbb-icon svgIcon="exit-small" class="sbb-icon-fit"></sbb-icon>
<span>{{ tLogout }}</span>
<span>{{ 'app.logout' | translate }}</span>
</button>
</sbb-menu>
</sbb-header-lean>
Expand All @@ -43,7 +43,7 @@
<ng-template #loading>
<ng-container *ngIf="!disableBackend">
<sbb-loading-indicator mode="big"></sbb-loading-indicator>
<div class="login-message">{{ tLoginMessage }}</div>
<div class="login-message">{{ 'app.login' | translate }}</div>
</ng-container>
<ng-container *ngIf="disableBackend">
<sbb-netzgrafik-editor></sbb-netzgrafik-editor>
Expand Down
15 changes: 4 additions & 11 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ import {Operation} from "./models/operation.model";
})
export class AppComponent {
readonly disableBackend = environment.disableBackend;

readonly tVersion = $localize`:@@app.version:Version` + " " + packageJson.version;
readonly tUserGuide = $localize`:@@app.user-guide:User guide`;
readonly tUserManual = $localize`:@@app.user-manual:User manual`;
readonly tLogout = $localize`:@@app.logout:Logout`;
readonly tLoginMessage = $localize`:@@app.login:You are getting logged in...`;

locale = localStorage.getItem("locale");

environmentLabel = environment.label;
authenticated: Promise<unknown>;
readonly version = packageJson.version;
readonly locale = localStorage.getItem("locale");
readonly environmentLabel = environment.label;
readonly authenticated: Promise<unknown>;

projectInMenu: Observable<ProjectDto | null>;

Expand Down
5 changes: 2 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NgModule, Injector, ApplicationRef, DoBootstrap} from "@angular/core";
import {NgModule, Injector, DoBootstrap} from "@angular/core";
import {NgxEditorModule} from "ngx-editor";
import {BrowserModule} from "@angular/platform-browser";
import {createCustomElement} from "@angular/elements";
Expand Down Expand Up @@ -231,13 +231,12 @@ import {I18nModule} from "./core/i18n/i18n.module";
SbbTooltipModule,
SbbBreadcrumbModule,
SbbAutocompleteModule,
I18nModule,
],
bootstrap: environment.customElement ? [] : [AppComponent],
providers: [
... environment.backendUrl ? [{provide: BASE_PATH, useValue: environment.backendUrl}] : [],
{provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true},
I18nModule.setLocale(),
I18nModule.setLocaleId()
],
})

Expand Down
86 changes: 24 additions & 62 deletions src/app/core/i18n/i18n.module.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,25 @@
import {registerLocaleData} from "@angular/common";
import {APP_INITIALIZER, Injectable, LOCALE_ID} from "@angular/core";
import {loadTranslations} from "@angular/localize";

@Injectable({
providedIn: "root",
})
class I18n {
locale = "en";
readonly allowedLocales = ["en", "fr", "de", "it"];

async setLocale() {
const userLocale = localStorage.getItem("locale");

// If the user has a preferred language stored in localStorage, use it.
if (userLocale && this.allowedLocales.includes(userLocale)) {
this.locale = userLocale;
} else {
localStorage.setItem("locale", this.locale);
import {NgModule, APP_INITIALIZER, LOCALE_ID} from "@angular/core";
import {CommonModule} from "@angular/common";
import {TranslatePipe} from "./translate.pipe";
import {I18n} from "./i18n.service";

@NgModule({
declarations: [TranslatePipe], // Declare the pipe
imports: [CommonModule],
providers: [
I18n,
{ // Load locale data at app start-up
provide: APP_INITIALIZER,
useFactory: (i18n: I18n) => () => i18n.setLocale(),
deps: [I18n],
multi: true,
},
{ // Set the runtime locale for the app
provide: LOCALE_ID,
useFactory: (i18n: I18n) => i18n.locale,
deps: [I18n],
}

// Use web pack magic string to only include required locale data
const localeModule = await import(
/* webpackInclude: /(en|de|fr|it)\.mjs$/ */
`/node_modules/@angular/common/locales/${this.locale}.mjs`
);

// Set locale for built in pipes, etc.
registerLocaleData(localeModule.default);

// Load translation file
const localeTranslationsModule = await import(
`src/assets/i18n/${this.locale}.json`
);

// Load translations for the current locale at run-time
loadTranslations(localeTranslationsModule.default);
}
}

// Load locale data at app start-up
function setLocale() {
return {
provide: APP_INITIALIZER,
useFactory: (i18n: I18n) => () => i18n.setLocale(),
deps: [I18n],
multi: true,
};
}

// Set the runtime locale for the app
function setLocaleId() {
return {
provide: LOCALE_ID,
useFactory: (i18n: I18n) => i18n.locale,
deps: [I18n],
};
}

export const I18nModule = {
setLocale: setLocale,
setLocaleId: setLocaleId,
};
],
exports: [TranslatePipe] // Export the pipe
})
export class I18nModule { }
89 changes: 89 additions & 0 deletions src/app/core/i18n/i18n.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {Injectable} from "@angular/core";
import {registerLocaleData} from "@angular/common";
import {loadTranslations} from "@angular/localize";

@Injectable({
providedIn: "root",
})
export class I18n {
locale = "en";
readonly allowedLocales = ["en", "fr", "de", "it"];
translations: any = {};

async setLocale() {
const userLocale = localStorage.getItem("locale");

// If the user has a preferred language stored in LocalStorage, use it.
if (userLocale && this.allowedLocales.includes(userLocale)) {
this.locale = userLocale;
} else {
localStorage.setItem("locale", this.locale);
}

// Use webpack magic string to only include required locale data
const localeModule = await import(
/* webpackInclude: /(en|de|fr|it)\.mjs$/ */
`/node_modules/@angular/common/locales/${this.locale}.mjs`
);
registerLocaleData(localeModule.default);

// Load translation file initially
await this.loadTranslations();
}

async loadTranslations() {
const localeTranslationsModule = await import(
`src/assets/i18n/${this.locale}.json`
);

// Ensure translations are flattened if necessary
this.translations = this.flattenTranslations(localeTranslationsModule.default);

// Load translations for the current locale at runtime
loadTranslations(this.translations);
}

// Helper function to flatten nested translations
// nested JSON :
// {
// "app": {
// "login": "Login",
// "models": {...}
// }
// }
// flattened JSON :
// {
// "app.login": "Login",
// "app.models...": ...,
// "app.models...": ...
// }
private flattenTranslations(translations: any): any {
const flattenedTranslations = {};

function flatten(obj, prefix = "") {
for (const key in obj) {
if (typeof obj[key] === "string") {
flattenedTranslations[prefix + key] = obj[key];
} else if (typeof obj[key] === "object") {
flatten(obj[key], prefix + key + ".");
}
}
}

flatten(translations);
return flattenedTranslations;
}

// Used for the pipe and allowing parameters
translate(key: string, params?: any): string {
let translation = this.translations[key] || key;

if (params) {
Object.keys(params).forEach(param => {
translation = translation.replace(`{$${param}}`, params[param]);
});
}

return translation;
}
}
14 changes: 14 additions & 0 deletions src/app/core/i18n/translate.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Pipe, PipeTransform} from "@angular/core";
import {I18n} from "./i18n.service";

@Pipe({
name: "translate",
pure: false
})
export class TranslatePipe implements PipeTransform {
constructor(private i18n: I18n) {}

transform(key: string, params?: any): string {
return this.i18n.translate(key, params);
}
}
2 changes: 1 addition & 1 deletion src/app/models/node.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class Node {
labelIds,
}: NodeDto = {
id: Node.incrementId(),
betriebspunktName: $localize`:@@app.models.node.betriebspunkt-name-default:NEW`,
betriebspunktName: $localize`:@@app.models.node.shortNameDefault:NEW`,
fullName: $localize`:@@app.models.node.full-name-default:New node`,
positionX: 0,
positionY: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!--<a sbbIconSidebarItem label="Varianten" (click)="onVariantenClicked()" class="sbb-active">-->
<a *ngIf="!disableBackend"
sbbIconSidebarItem
[label]="tVariants"
[label]="'app.netzgrafik-application.variants' | translate"
[class]="getVariantsActivatedTag()"
(click)="onVariantenClicked()"
>
Expand All @@ -19,37 +19,37 @@
</a>
<a
sbbIconSidebarItem
[label]="tFilter"
[label]="'app.netzgrafik-application.filter' | translate"
[class]="getFilterActivatedTag()"
[style]="getFilterStyle()"
(click)="onFilterClicked()"
>
<sbb-icon svgIcon="filter-small"></sbb-icon>
</a>
<!--
<a sbbIconSidebarItem [label]="tAnalytics" (click)="onAnalyticsClicked()">
<a sbbIconSidebarItem [label]="'app.netzgrafik-application.analytics' | translate" (click)="onAnalyticsClicked()">
<sbb-icon svgIcon="bulb-on-small"></sbb-icon>
</a>
-->
<a
sbbIconSidebarItem
[label]="tEdit"
[label]="'app.netzgrafik-application.edit' | translate"
[class]="getEditActivatedTag()"
(click)="onEditToolClicked()"
>
<sbb-icon svgIcon="pen-small"></sbb-icon>
</a>
<a
sbbIconSidebarItem
[label]="tMoreFunctions"
[label]="'app.netzgrafik-application.more-functions' | translate"
[class]="getMoreFunctionActivatedTag()"
(click)="onToolsClicked()"
>
<sbb-icon svgIcon="nine-squares-small"></sbb-icon>
</a>
<a
sbbIconSidebarItem
[label]="tProperties"
[label]="'app.netzgrafik-application.settings' | translate"
[class]="getPropertiesActivatedTag()"
(click)="onPropertiesClicked()"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ export class NetzgrafikApplicationComponent {

readonly disableBackend = environment.disableBackend;

readonly tVariants = $localize`:@@app.netzgrafik-application.variants:Variants`;
readonly tFilter = $localize`:@@app.netzgrafik-application.filter:Filter`;
readonly tAnalytics = $localize`:@@app.netzgrafik-application.analytics:Analytics`;
readonly tEdit = $localize`:@@app.netzgrafik-application.edit:Edit`;
readonly tMoreFunctions = $localize`:@@app.netzgrafik-application.more-functions:More functions`;
readonly tProperties = $localize`:@@app.netzgrafik-application.properties:Properties`;

private readonly destroyed = new Subject<void>();

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class PerlenketteNodeComponent implements OnInit {
if (amountOfWarningConnections === 1) {
return $localize`:@@app.perlenkette.perlenkette-node.1-warning-connection:1 incorrect connection`;
}
return $localize`:@@app.perlenkette.perlenkette-node.n-warning-connections:${amountOfWarningConnections} incorrect connections`;
return $localize`:@@app.perlenkette.perlenkette-node.n-warning-connections:${amountOfWarningConnections}:number: incorrect connections`;
}

getFittingConnections(): string {
Expand All @@ -72,7 +72,7 @@ export class PerlenketteNodeComponent implements OnInit {
if (amountOfFittingConnections === 1) {
return $localize`:@@app.perlenkette.perlenkette-node.1-fitting-connection:1 fitting connection`;
}
return $localize`:@@app.perlenkette.perlenkette-node.n-fitting-connections:${amountOfFittingConnections} fitting connections`;
return $localize`:@@app.perlenkette.perlenkette-node.n-fitting-connections:${amountOfFittingConnections}:number: fitting connections`;
}

expandConnections() {
Expand Down
4 changes: 2 additions & 2 deletions src/app/perlenkette/perlenkette.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<div>
<div [attr.style]="'pointer-events: none; margin: 0px; position: absolute; padding-left: '+(contentWidth-55)+'px; padding-top: '+(contentHeight-40)+'px;'">
<ng-container *ngIf="!getShowAllLockStates()">
<sbb-icon class="ToggleAllEyesButton" svgIcon="eye-disabled-small" style="pointer-events:auto;" (mouseup)="toggleShowAllLockStates()" [title]="tShowOnlyClosedLocks"></sbb-icon>
<sbb-icon class="ToggleAllEyesButton" svgIcon="eye-disabled-small" style="pointer-events:auto;" (mouseup)="toggleShowAllLockStates()" [title]="'app.perlenkette.show-only-closed-locks' | translate"></sbb-icon>
</ng-container>
<ng-container *ngIf="getShowAllLockStates()">
<sbb-icon class="ToggleAllEyesButton" svgIcon="eye-small" style="pointer-events:auto;" (mouseup)="toggleShowAllLockStates()" [title]="tShowAllLocks"></sbb-icon>
<sbb-icon class="ToggleAllEyesButton" svgIcon="eye-small" style="pointer-events:auto;" (mouseup)="toggleShowAllLockStates()" [title]="'app.perlenkette.show-all-locks' | translate"></sbb-icon>
</ng-container>
</div>
</div>
Expand Down
3 changes: 0 additions & 3 deletions src/app/perlenkette/perlenkette.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ export class PerlenketteComponent implements AfterContentChecked, OnDestroy {

private showAllLockStates = false;

readonly tShowOnlyClosedLocks = $localize`:@@app.perlenkette.show-only-closed-locks:Show only closed locks`;
readonly tShowAllLocks = $localize`:@@app.perlenkette.show-all-locks:Show all locks`;

constructor(
private readonly loadPerlenketteService: LoadPerlenketteService,
readonly filterService: FilterService,
Expand Down
Loading

0 comments on commit d87b449

Please sign in to comment.