7 Commits

12 changed files with 114 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
@if (elements_data.length) { @if (elements_data.length) {
<h1>Всего: {{ elements_data.length }}</h1> <h1>{{ "ROW_COUNT_LABEL" | translate }}: {{ elements_data.length }}</h1>
<h2>Файл: {{ fileName }}</h2> <h2>{{ "FILE_NAME_LABEL" | translate }}: {{ fileName }}</h2>
} }
<div id="elements"> <div id="elements">
@for (item of elements_data; track $index) { @for (item of elements_data; track $index) {

View File

@@ -1,5 +1,6 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Component, Input, OnInit, QueryList, ViewChildren } from "@angular/core"; import { AfterViewInit, Component, Input, QueryList, ViewChildren } from "@angular/core";
import { TranslationPipe } from "../../../../pipes/translation.pipe";
import { NpsFile, TranslateData } from "../../dto/translate_data.dto"; import { NpsFile, TranslateData } from "../../dto/translate_data.dto";
import { TranslateBlockComponent } from "../translate_block/translate_block.component"; import { TranslateBlockComponent } from "../translate_block/translate_block.component";
@@ -8,9 +9,9 @@ import { TranslateBlockComponent } from "../translate_block/translate_block.comp
templateUrl: "./text_list.component.html", templateUrl: "./text_list.component.html",
styleUrls: ["./text_list.component.scss"], styleUrls: ["./text_list.component.scss"],
standalone: true, standalone: true,
imports: [CommonModule, TranslateBlockComponent], imports: [CommonModule, TranslateBlockComponent, TranslationPipe],
}) })
export class TextListComponent implements OnInit { export class TextListComponent implements AfterViewInit {
@ViewChildren("translateBlock") translate_blocks: QueryList<TranslateBlockComponent> | null = @ViewChildren("translateBlock") translate_blocks: QueryList<TranslateBlockComponent> | null =
null; null;
fileName = ""; fileName = "";
@@ -18,13 +19,14 @@ export class TextListComponent implements OnInit {
@Input() set elements(el: TranslateData[]) { @Input() set elements(el: TranslateData[]) {
this.elements_data = el; this.elements_data = el;
localStorage.setItem("translations", JSON.stringify(this.elements_data)); localStorage.setItem("translations", JSON.stringify(this.elements_data));
this.ngAfterViewInit();
} }
get elements() { get elements() {
return this.elements_data; return this.elements_data;
} }
ngOnInit(): void { ngAfterViewInit(): void {
const data = localStorage.getItem("original_file"); const data = localStorage.getItem("original_file");
if (data) { if (data) {
const file: NpsFile = JSON.parse(data); const file: NpsFile = JSON.parse(data);

View File

@@ -1,6 +1,7 @@
<div class="element"> <div class="element">
<h2>{{ index + 1 }}</h2> <h2>{{ index + 1 }}</h2>
<div class="fields"> <div class="fields">
<h2>{{ item.name }}</h2>
<nwui-textarea [contenteditable]="false">{{ item.english_text }}</nwui-textarea> <nwui-textarea [contenteditable]="false">{{ item.english_text }}</nwui-textarea>
<nwui-textarea <nwui-textarea
#translatedText #translatedText
@@ -11,8 +12,14 @@
> >
</div> </div>
<div class="btns"> <div class="btns">
<nwui-button (click)="sendToGoogleTranslate()" [disabled]="translateLoading">Translate</nwui-button> <nwui-button (click)="sendToGoogleTranslate()" [disabled]="translateLoading">{{
<nwui-button (click)="isEditing = !isEditing" [disabled]="isEditing">Edit</nwui-button> "TRANSLATE_BTN" | translate
<nwui-button (click)="clear()" [disabled]="!item.translated_text.length">Clear</nwui-button> }}</nwui-button>
<nwui-button (click)="isEditing = !isEditing" [disabled]="isEditing">{{
"EDIT_BTN" | translate
}}</nwui-button>
<nwui-button (click)="clear()" [disabled]="!item.translated_text.length">{{
"CLEAR_ROW_BTN" | translate
}}</nwui-button>
</div> </div>
</div> </div>

View File

@@ -1,38 +1,45 @@
import { CommonModule } from '@angular/common'; import { CommonModule } from "@angular/common";
import { Component, Input, OnInit, ViewChild } from '@angular/core'; import { Component, Input, OnInit, ViewChild } from "@angular/core";
import { NWUIButtonComponent, NWUITextAreaComponent } from '@nwaifu-ui'; import { NWUIButtonComponent, NWUITextAreaComponent } from "@nwaifu-ui";
import { LocalStorageKeys } from '../../consts'; import { TranslationPipe } from "../../../../pipes/translation.pipe";
import { TranslateData } from '../../dto/translate_data.dto'; import { LocalStorageKeys } from "../../consts";
import { ETranslateService } from '../../services/translate.enums'; import { TranslateData } from "../../dto/translate_data.dto";
import { TranslateService } from '../../services/translate.service'; import { ETranslateService } from "../../services/translate.enums";
import { TranslateService } from "../../services/translate.service";
@Component({ @Component({
selector: 'app-translate-block', selector: "app-translate-block",
templateUrl: './translate_block.component.html', templateUrl: "./translate_block.component.html",
styleUrls: ['./translate_block.component.scss'], styleUrls: ["./translate_block.component.scss"],
standalone: true, standalone: true,
imports: [CommonModule, NWUIButtonComponent, NWUITextAreaComponent], imports: [CommonModule, NWUIButtonComponent, NWUITextAreaComponent, TranslationPipe],
providers: [TranslateService], providers: [TranslateService],
}) })
export class TranslateBlockComponent implements OnInit { export class TranslateBlockComponent implements OnInit {
@ViewChild('translatedText') translatedText: NWUITextAreaComponent | null = null; @ViewChild("translatedText") translatedText: NWUITextAreaComponent | null = null;
@Input({ required: true }) item: TranslateData = { english_text: '', translated_text: '' }; @Input({ required: true }) item: TranslateData = {
english_text: "",
translated_text: "",
name: "",
};
@Input({ required: true }) index = 0; @Input({ required: true }) index = 0;
translateLoading = false; translateLoading = false;
isEditing = false; isEditing = false;
saveChanges() { saveChanges() {
const data: TranslateData[] = JSON.parse(localStorage.getItem(LocalStorageKeys.TRANSLATIONS) ?? '[]'); const data: TranslateData[] = JSON.parse(
localStorage.getItem(LocalStorageKeys.TRANSLATIONS) ?? "[]",
);
if (!data.length) { if (!data.length) {
alert('No data'); alert("No data");
return; return;
} }
data[this.index] = this.item; data[this.index] = this.item;
localStorage.setItem('translations', JSON.stringify(data)); localStorage.setItem("translations", JSON.stringify(data));
} }
ngOnInit(): void { ngOnInit(): void {
this.isEditing = this.item.translated_text === ''; this.isEditing = this.item.translated_text === "";
} }
constructor(private translateService: TranslateService) {} constructor(private translateService: TranslateService) {}
@@ -40,7 +47,8 @@ export class TranslateBlockComponent implements OnInit {
private sendToTranslate(service: ETranslateService = ETranslateService.GOOGLE) { private sendToTranslate(service: ETranslateService = ETranslateService.GOOGLE) {
this.translateLoading = true; this.translateLoading = true;
this.translateService.translate(this.item.english_text, service).subscribe((text) => { this.translateService.translate(this.item.english_text, service).subscribe((text) => {
if (this.translatedText) if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = text; if (this.translatedText)
if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = text;
this.item.translated_text = text; this.item.translated_text = text;
this.isEditing = false; this.isEditing = false;
this.translateLoading = false; this.translateLoading = false;
@@ -62,14 +70,16 @@ export class TranslateBlockComponent implements OnInit {
saveTranslate(text: string) { saveTranslate(text: string) {
this.isEditing = false; this.isEditing = false;
if (this.translatedText) if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = ''; if (this.translatedText)
if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = "";
this.item.translated_text = text; this.item.translated_text = text;
this.saveChanges(); this.saveChanges();
} }
clear() { clear() {
this.item.translated_text = ''; this.item.translated_text = "";
this.isEditing = true; this.isEditing = true;
if (this.translatedText) if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = ''; if (this.translatedText)
if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = "";
this.saveChanges(); this.saveChanges();
} }
} }

View File

@@ -1,6 +1,7 @@
export interface TranslateData { export interface TranslateData {
english_text: string; english_text: string;
translated_text: string; translated_text: string;
name: string;
} }
export interface NpsFile { export interface NpsFile {
file_name: string; file_name: string;

View File

@@ -3,20 +3,33 @@ import { TranslateData } from "../dto/translate_data.dto";
export function parse(text: string): TranslateData[] { export function parse(text: string): TranslateData[] {
// Find all TEXT attr data // Find all TEXT attr data
const result: TranslateData[] = []; const result: TranslateData[] = [];
const re = /<[^>]*TEXT="(?<textAttr>[^>"]+)"[^>]*>|<[^>]*>|\/\/.*|\s*\n\s*/gm; const re = /^(?!\/\/)<[^>]*TEXT="(?<textAttr>[^>"]+)"[^>]*>/gim;
for (const match of text.matchAll(re)) { for (const match of text.matchAll(re)) {
console.log(match);
if (match.groups?.["textAttr"]) if (match.groups?.["textAttr"])
result.push({ english_text: match.groups?.["textAttr"], translated_text: "" }); result.push({
english_text: match.groups?.["textAttr"],
translated_text: "",
name: "Choice (without name)",
});
} }
const replaced_text = text.replace(re, "\n").replace(/\s*\n\s*/gm, "\n"); const name_re = /<voice name="(?<name>[^"]+)"[^>]*>(?<text>[\s\S]*?)(?=<voice|$)/gi;
result.push(
...replaced_text for (const match of text.matchAll(name_re)) {
.split("\n") if (match.groups?.["name"] && match.groups?.["text"]) {
.map((line) => line.trim()) const name_text = match.groups?.["text"];
.filter((line) => line.length > 0) const name = match.groups?.["name"];
.map((line) => ({ english_text: line, translated_text: "" })), const re = /<[^>]*>|\/\/.*|\s*\n\s*/gm;
); name_text
.split(re)
.filter((line) => line.length > 0)
.map((line) => line.trim())
.forEach((line) => {
result.push({ english_text: line, translated_text: "", name: name });
});
}
}
console.log(result); console.log(result);
return result; return result;
} }

View File

@@ -1,7 +1,7 @@
<div class="btns"> <div class="btns">
<div id="op_btns"> <div id="op_btns">
<nwui-button (click)="fileInput.click()"> <nwui-button (click)="fileInput.click()">
<span><i class="lni lni-upload"></i> Upload</span> <span><i class="lni lni-upload"></i> {{ "UPLOAD_BTN" | translate }}</span>
<input <input
type="file" type="file"
(change)="submitFile($event)" (change)="submitFile($event)"
@@ -11,17 +11,17 @@
/> />
</nwui-button> </nwui-button>
<nwui-button (click)="onSaveClicked()"> <nwui-button (click)="onSaveClicked()">
<span><i class="lni lni-save"></i> Save</span> <span><i class="lni lni-save"></i> {{ "SAVE_BTN" | translate }}</span>
</nwui-button> </nwui-button>
@if (this.elements.length) { @if (this.elements.length) {
<nwui-button (click)="clearAllTranslations()" [disabled]="!has_translations"> <nwui-button (click)="clearAllTranslations()" [disabled]="!has_translations">
<span><i class="lni lni-trash-can"></i> Clear translations</span> <span><i class="lni lni-trash-can"></i> {{ "CLEAR_TRANSLATIONS_BTN" | translate }}</span>
</nwui-button> </nwui-button>
<nwui-button (click)="getAllTranslations()"> <nwui-button (click)="getAllTranslations()">
<span><i class="lni lni-google"></i> Translate all</span> <span><i class="lni lni-google"></i> {{ "TRANSLATE_ALL_BTN" | translate }}</span>
</nwui-button> </nwui-button>
<nwui-button (click)="onClearClicked()"> <nwui-button (click)="onClearClicked()">
<span><i class="lni lni-trash-can"></i> Clear</span> <span><i class="lni lni-trash-can"></i> {{ "CLEAR_BTN" | translate }}</span>
</nwui-button> </nwui-button>
} }
</div> </div>

View File

@@ -3,6 +3,7 @@ import { AfterViewInit, Component, OnInit, ViewChild } from "@angular/core";
import { Router } from "@angular/router"; import { Router } from "@angular/router";
import { NWUIButtonComponent } from "@nwaifu-ui"; import { NWUIButtonComponent } from "@nwaifu-ui";
import { fromEvent, map } from "rxjs"; import { fromEvent, map } from "rxjs";
import { TranslationPipe } from "../../pipes/translation.pipe";
import { TextListComponent } from "./components/text_list/text_list.component"; import { TextListComponent } from "./components/text_list/text_list.component";
import { LocalStorageKeys } from "./consts"; import { LocalStorageKeys } from "./consts";
import { NpsFile, TranslateData } from "./dto/translate_data.dto"; import { NpsFile, TranslateData } from "./dto/translate_data.dto";
@@ -10,7 +11,7 @@ import { saveOriginalFile } from "./lib/file_tools";
import { parse } from "./lib/parser"; import { parse } from "./lib/parser";
@Component({ @Component({
standalone: true, standalone: true,
imports: [TextListComponent, CommonModule, NWUIButtonComponent], imports: [TextListComponent, CommonModule, NWUIButtonComponent, TranslationPipe],
selector: "app-nitroplus", selector: "app-nitroplus",
templateUrl: "./nitroplus-translator.component.html", templateUrl: "./nitroplus-translator.component.html",
styleUrl: "./nitroplus-translator.component.less", styleUrl: "./nitroplus-translator.component.less",

View File

@@ -5,5 +5,15 @@
"TELEGRAM_LABEL": "Telegram channel", "TELEGRAM_LABEL": "Telegram channel",
"GITHUB_LABEL": "Admin's Github", "GITHUB_LABEL": "Admin's Github",
"GITEA_LABEL": "Neuro LLC Gitea", "GITEA_LABEL": "Neuro LLC Gitea",
"SOURCE_CODE_TITLE": "Source code" "SOURCE_CODE_TITLE": "Source code",
"UPLOAD_BTN": "Upload",
"SAVE_BTN": "Save",
"CLEAR_TRANSLATIONS_BTN": "Clear translations",
"CLEAR_BTN": "Clear",
"ROW_COUNT_LABEL": "Row count",
"FILE_NAME_LABEL": "File name",
"TRANSLATE_BTN": "Translate",
"EDIT_BTN": "Edit",
"CLEAR_ROW_BTN": "Clear",
"TRANSLATE_ALL_BTN": "Translate all"
} }

View File

@@ -5,5 +5,15 @@
"TELEGRAM_LABEL": "Telegramチャンネル", "TELEGRAM_LABEL": "Telegramチャンネル",
"GITHUB_LABEL": "管理者Github", "GITHUB_LABEL": "管理者Github",
"GITEA_LABEL": "Neuro LLC Gitea", "GITEA_LABEL": "Neuro LLC Gitea",
"SOURCE_CODE_TITLE": "ソースコード" "SOURCE_CODE_TITLE": "ソースコード",
"UPLOAD_BTN": "アップロード",
"SAVE_BTN": "保存",
"CLEAR_TRANSLATIONS_BTN": "翻訳をクリア",
"CLEAR_BTN": "クリア",
"ROW_COUNT_LABEL": "行数",
"FILE_NAME_LABEL": "ファイル名",
"TRANSLATE_BTN": "翻訳",
"EDIT_BTN": "編集",
"CLEAR_ROW_BTN": "クリア",
"TRANSLATE_ALL_BTN": "すべて翻訳"
} }

View File

@@ -5,5 +5,15 @@
"TELEGRAM_LABEL": "Телеграм канал", "TELEGRAM_LABEL": "Телеграм канал",
"GITHUB_LABEL": "Github админа", "GITHUB_LABEL": "Github админа",
"GITEA_LABEL": "Neuro LLC Gitea", "GITEA_LABEL": "Neuro LLC Gitea",
"SOURCE_CODE_TITLE": "Исходный код" "SOURCE_CODE_TITLE": "Исходный код",
"UPLOAD_BTN": "Загрузить",
"SAVE_BTN": "Сохранить",
"CLEAR_TRANSLATIONS_BTN": "Очистить переводы",
"CLEAR_BTN": "Очистить",
"ROW_COUNT_LABEL": "Количество строк",
"FILE_NAME_LABEL": "Имя файла",
"TRANSLATE_BTN": "Перевести",
"EDIT_BTN": "Редактировать",
"CLEAR_ROW_BTN": "Очистить",
"TRANSLATE_ALL_BTN": "Перевести все"
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "nwaifu-web", "name": "nwaifu-web",
"version": "0.0.1", "version": "0.2.3",
"scripts": { "scripts": {
"ng": "ng", "ng": "ng",
"start": "nx serve NwaifuWeb", "start": "nx serve NwaifuWeb",