Added i18n translations

This commit is contained in:
2024-03-10 00:14:00 +03:00
parent a9436a5af7
commit 90582949d5
15 changed files with 191 additions and 20 deletions

View File

@@ -1,5 +1,4 @@
import { Component } from "@angular/core"; import { Component } from "@angular/core";
import { RouterOutlet } from "@angular/router";
import { DockComponent } from "./modules/dock/dock.component"; import { DockComponent } from "./modules/dock/dock.component";
import { ModalComponent } from "./modules/modal/modal.component"; import { ModalComponent } from "./modules/modal/modal.component";
import { PanelComponent } from "./modules/panel/panel.component"; import { PanelComponent } from "./modules/panel/panel.component";
@@ -7,7 +6,7 @@ import { PanelComponent } from "./modules/panel/panel.component";
@Component({ @Component({
selector: "app-root", selector: "app-root",
standalone: true, standalone: true,
imports: [RouterOutlet, PanelComponent, DockComponent, ModalComponent], imports: [PanelComponent, DockComponent, ModalComponent],
templateUrl: "./app.component.html", templateUrl: "./app.component.html",
styleUrl: "./app.component.less", styleUrl: "./app.component.less",
}) })

View File

@@ -1,8 +1,24 @@
import { ApplicationConfig } from '@angular/core'; import { APP_INITIALIZER, ApplicationConfig } from "@angular/core";
import { provideRouter } from '@angular/router'; import { provideRouter } from "@angular/router";
import { routes } from './app.routes'; import { provideHttpClient } from "@angular/common/http";
import { routes } from "./app.routes";
import { TranslateService } from "./services/translate.service";
export function setupTranslateServiceFactory(service: TranslateService) {
return () => service.use("en");
}
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)] providers: [
provideRouter(routes),
provideHttpClient(),
TranslateService,
{
provide: APP_INITIALIZER,
useFactory: setupTranslateServiceFactory,
deps: [TranslateService],
multi: true,
},
],
}; };

View File

@@ -16,19 +16,19 @@ export class DockComponent {
id: 0, id: 0,
svg: "../../../assets/svg/logo-telegram.svg", svg: "../../../assets/svg/logo-telegram.svg",
url: "https://t.me/neur0w0men", url: "https://t.me/neur0w0men",
text: "Telegram channel", text: "TELEGRAM_LABEL",
}, },
{ {
id: 1, id: 1,
svg: "../../../assets/svg/logo-github.svg", svg: "../../../assets/svg/logo-github.svg",
url: "https://github.com/MrSedan", url: "https://github.com/MrSedan",
text: "Admin's GitHub", text: "GITHUB_LABEL",
}, },
{ {
id: 2, id: 2,
svg: "../../../assets/svg/logo-gitea.svg", svg: "../../../assets/svg/logo-gitea.svg",
url: "https://git.nwaifu.su", url: "https://git.nwaifu.su",
text: "Gitea", text: "GITEA_LABEL",
}, },
]; ];
} }

View File

@@ -1,4 +1,4 @@
<a [href]="url"> <a [href]="url">
<img [src]="svg" [alt]="url" [title]="text" /> <img [src]="svg" [alt]="url" [title]="text" />
<span>{{ text }}</span> <span>{{ text | translate }}</span>
</a> </a>

View File

@@ -1,10 +1,11 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Component, Input } from "@angular/core"; import { Component, Input } from "@angular/core";
import { TranslationPipe } from "../../pipes/translation.pipe";
@Component({ @Component({
standalone: true, standalone: true,
selector: "app-link", selector: "app-link",
imports: [CommonModule], imports: [CommonModule, TranslationPipe],
templateUrl: "./link.component.html", templateUrl: "./link.component.html",
styleUrls: ["./link.component.less"], styleUrls: ["./link.component.less"],
}) })

View File

@@ -7,7 +7,14 @@
<span>{{ time }}</span> <span>{{ time }}</span>
</div> </div>
<div class="right-menu"> <div class="right-menu">
<span class="lang">en</span> <span
class="lang cursor-pointer"
(click)="toggleModal()"
(keydown)="toggleModal()"
tabindex="0"
#lang_btn
>{{ getLang() }}</span
>
<a href="https://git.nwaifu.su/neuro_llc/NwaifuWeb"> <a href="https://git.nwaifu.su/neuro_llc/NwaifuWeb">
<fa-icon [icon]="faGithub" class="sourcecode-icon" title="Source code"></fa-icon> <fa-icon [icon]="faGithub" class="sourcecode-icon" title="Source code"></fa-icon>
</a> </a>
@@ -15,3 +22,8 @@
<fa-icon [icon]="faPower"></fa-icon> <fa-icon [icon]="faPower"></fa-icon>
</div> </div>
</div> </div>
<div class="lang-choose" #lang_modal>
<p (click)="useLang('en')" (keypress)="useLang('en')" tabindex="0"><b>en</b> - English</p>
<p (click)="useLang('ru')" (keypress)="useLang('ru')" tabindex="0"><b>ru</b> - Русский</p>
<p (click)="useLang('ja')" (keypress)="useLang('ja')" tabindex="0"><b>ja</b> - 日本語</p>
</div>

View File

@@ -63,3 +63,43 @@
cursor: pointer; cursor: pointer;
} }
} }
.lang-choose {
position: absolute;
display: none;
&.active {
display: flex;
}
top: 0;
flex-direction: column;
justify-content: space-around;
gap: 0;
background-color: var(--black);
margin-block-start: 3rem;
z-index: 9999;
margin-inline-end: 1rem;
color: var(--white);
padding: 1rem 0;
border-radius: 15px;
&::before {
content: "";
position: absolute;
left: 50%;
transform: translateX(-50%) rotate(45deg);
vertical-align: middle;
top: -0.5rem;
width: 0;
height: 0;
border: 10px solid var(--black);
}
p {
line-height: 2rem;
width: 100%;
padding: 0.5rem 1rem;
user-select: none;
-webkit-user-select: none;
cursor: pointer;
&:hover {
background-color: #000;
}
}
}

View File

@@ -1,9 +1,10 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Component } from "@angular/core"; import { Component, ElementRef, HostListener, ViewChild } from "@angular/core";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome"; import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { faGithub } from "@fortawesome/free-brands-svg-icons"; import { faGithub } from "@fortawesome/free-brands-svg-icons";
import { faPowerOff, faVolumeHigh } from "@fortawesome/free-solid-svg-icons"; import { faPowerOff, faVolumeHigh } from "@fortawesome/free-solid-svg-icons";
import { PanelSevice } from "../../services/panel.service"; import { PanelSevice } from "../../services/panel.service";
import { TranslateService } from "../../services/translate.service";
@Component({ @Component({
standalone: true, standalone: true,
@@ -17,7 +18,11 @@ export class PanelComponent {
faGithub = faGithub; faGithub = faGithub;
faVolume = faVolumeHigh; faVolume = faVolumeHigh;
faPower = faPowerOff; faPower = faPowerOff;
constructor(private panelService: PanelSevice) { showLangModalBool = false;
@ViewChild("lang_modal") langModal: ElementRef<HTMLDivElement> | null = null;
@ViewChild("lang_btn") langBtn: ElementRef<HTMLSpanElement> | null = null;
constructor(private panelService: PanelSevice, private translateService: TranslateService) {
this.time = this.getTime(); this.time = this.getTime();
setInterval(() => { setInterval(() => {
this.time = this.getTime(); this.time = this.getTime();
@@ -26,7 +31,7 @@ export class PanelComponent {
private getTime() { private getTime() {
const time = this.panelService.getTime(); const time = this.panelService.getTime();
return time.toLocaleDateString("en-US", { return time.toLocaleDateString(this.translateService.translate("TIME_SCHEMA"), {
month: "short", month: "short",
day: "numeric", day: "numeric",
hour: "numeric", hour: "numeric",
@@ -37,4 +42,31 @@ export class PanelComponent {
goToSource() { goToSource() {
window.open("https://git.nwaifu.su/neuro_llc/NwaifuWeb", "_blank"); window.open("https://git.nwaifu.su/neuro_llc/NwaifuWeb", "_blank");
} }
@HostListener("window:resize")
private moveLangModal() {
if (!this.langModal || !this.langBtn) {
return;
}
const x = this.langBtn.nativeElement.getBoundingClientRect().x;
this.langModal.nativeElement.style.left = `calc(${x}px - 3.5rem)`;
}
toggleModal() {
if (this.langModal) {
this.langModal.nativeElement.classList.toggle("active");
if (this.langModal.nativeElement.classList.contains("active")) {
this.moveLangModal();
}
}
}
useLang(lang: string) {
this.translateService.use(lang);
}
getLang() {
return this.translateService.lang;
}
} }

View File

@@ -1,10 +1,10 @@
<div class="panel"> <div class="panel">
<span>Info</span> <span>{{ modal_title | translate }}</span>
<div class="btns"> <div class="btns">
<fa-icon [icon]="faMenu"></fa-icon> <fa-icon [icon]="faMenu"></fa-icon>
<fa-icon [icon]="faClose"></fa-icon> <fa-icon [icon]="faClose"></fa-icon>
</div> </div>
</div> </div>
<div class="content"> <div class="content">
<h1>Hello</h1> <h1>{{ modal_text | translate }}</h1>
</div> </div>

View File

@@ -1,18 +1,21 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { ChangeDetectionStrategy, Component } from "@angular/core"; import { Component } from "@angular/core";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome"; import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { faBars, faXmark } from "@fortawesome/free-solid-svg-icons"; import { faBars, faXmark } from "@fortawesome/free-solid-svg-icons";
import { DialogRef } from "@ngneat/dialog"; import { DialogRef } from "@ngneat/dialog";
import { TranslationPipe } from "../../pipes/translation.pipe";
@Component({ @Component({
selector: "app-window", selector: "app-window",
standalone: true, standalone: true,
imports: [CommonModule, FontAwesomeModule], imports: [CommonModule, FontAwesomeModule, TranslationPipe],
templateUrl: "./window.component.html", templateUrl: "./window.component.html",
styleUrls: ["./window.component.less"], styleUrls: ["./window.component.less"],
changeDetection: ChangeDetectionStrategy.OnPush, // changeDetection: ChangeDetectionStrategy.OnPush,
}) })
export class WindowComponent { export class WindowComponent {
modal_text = "MODAL_TEXT";
modal_title = "MODAL_TITLE";
faClose = faXmark; faClose = faXmark;
faMenu = faBars; faMenu = faBars;
constructor(public ref: DialogRef) {} constructor(public ref: DialogRef) {}

View File

@@ -0,0 +1,14 @@
import { Pipe, PipeTransform } from "@angular/core";
import { TranslateService } from "../services/translate.service";
@Pipe({
name: "translate",
standalone: true,
pure: false,
})
export class TranslationPipe implements PipeTransform {
constructor(private translateService: TranslateService) {}
transform(key: string) {
return this.translateService.data[key] ?? key;
}
}

View File

@@ -0,0 +1,30 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
@Injectable({ providedIn: "root" })
export class TranslateService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: Record<string, any> = {};
lang = "en";
constructor(private http: HttpClient) {}
use(lang: string): Promise<object> {
this.lang = lang ?? "en";
return new Promise<object>((resolve) => {
const langPath = `assets/i18n/${lang ?? "en"}.json`;
this.http.get(langPath).subscribe({
next: (response) => {
this.data = response ?? {};
resolve(this.data);
},
error: () => {
this.data = {};
resolve(this.data);
},
});
});
}
translate(key: string) {
return this.data[key] ?? key;
}
}

8
src/assets/i18n/en.json Normal file
View File

@@ -0,0 +1,8 @@
{
"MODAL_TITLE": "Info",
"TIME_SCHEMA": "en-US",
"MODAL_TEXT": "Hello, World!",
"TELEGRAM_LABEL": "Telegram channel",
"GITHUB_LABEL": "Admin's Github",
"GITEA_LABEL": "Neuro LLC Gitea"
}

8
src/assets/i18n/ja.json Normal file
View File

@@ -0,0 +1,8 @@
{
"MODAL_TITLE": "情報",
"TIME_SCHEMA": "ja-JP",
"MODAL_TEXT": "こんにちは、世界!",
"TELEGRAM_LABEL": "Telegramチャンネル",
"GITHUB_LABEL": "管理者Github",
"GITEA_LABEL": "Neuro LLC Gitea"
}

8
src/assets/i18n/ru.json Normal file
View File

@@ -0,0 +1,8 @@
{
"MODAL_TITLE": "Информация",
"TIME_SCHEMA": "ru-RU",
"MODAL_TEXT": "Здравствуйте, мир!",
"TELEGRAM_LABEL": "Телеграм канал",
"GITHUB_LABEL": "Github админа",
"GITEA_LABEL": "Neuro LLC Gitea"
}