feat: nitroplus translate. Needs to fix
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Routes } from "@angular/router";
|
||||
import { MainPageComponent } from './pages/main/main.component';
|
||||
import { SecondComponent } from './pages/second/second.component';
|
||||
import { NitroplusComponent } from './pages/nitroplus-translator/nitroplus-translator.component';
|
||||
|
||||
export const routes: Routes = [
|
||||
{
|
||||
@@ -11,8 +11,8 @@ export const routes: Routes = [
|
||||
component: MainPageComponent
|
||||
},
|
||||
{
|
||||
path: 'second',
|
||||
component: SecondComponent
|
||||
path: 'translator',
|
||||
component: NitroplusComponent
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -13,4 +13,5 @@ export class DesktopIconComponent {
|
||||
@Input() image: string = '';
|
||||
@Input() alt: string = '';
|
||||
@Input() name: string = '';
|
||||
@Input({required: false}) click: ()=>void = ()=>{};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<desktop-icon alt='test' name='test' image='../../../assets/svg/logo-gitea.svg'></desktop-icon>
|
||||
<a routerLink='/second'><desktop-icon alt='test' name='test' image='../../../assets/svg/logo-gitea.svg'></desktop-icon></a>
|
||||
<app-modal></app-modal>
|
||||
<app-dock></app-dock>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
import { RouterLink } from '@angular/router';
|
||||
import { DesktopIconComponent } from '../../modules/desktop-icon/desktop-icon.component';
|
||||
import { DockComponent } from '../../modules/dock/dock.component';
|
||||
import { ModalComponent } from '../../modules/modal/modal.component';
|
||||
@@ -8,7 +9,7 @@ import { ModalComponent } from '../../modules/modal/modal.component';
|
||||
selector: 'app-main-page',
|
||||
templateUrl: './main.component.html',
|
||||
styleUrls: ['./main.component.less'],
|
||||
imports: [DockComponent, ModalComponent, CommonModule, DesktopIconComponent],
|
||||
imports: [DockComponent, ModalComponent, CommonModule, DesktopIconComponent, RouterLink],
|
||||
standalone: true
|
||||
})
|
||||
export class MainPageComponent {}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<h1 *ngIf="elements_data.length">Всего: {{ elements_data.length }}</h1>
|
||||
<div id="elements">
|
||||
@for(item of elements_data; track $index) {
|
||||
<app-translate-block #translateBlock [index]="$index" [item]="item"></app-translate-block>
|
||||
}
|
||||
</div>
|
||||
@@ -0,0 +1,19 @@
|
||||
#elements {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3rem;
|
||||
align-items: center;
|
||||
margin-inline: 2rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #efdee0;
|
||||
text-align: center;
|
||||
margin: 1rem 2rem;
|
||||
}
|
||||
app-translate-block {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component, Input, QueryList, ViewChildren } from '@angular/core';
|
||||
import { TranslateData } from '../../dto/translate_data.dto';
|
||||
import { TranslateBlockComponent } from '../translate_block/translate_block.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-text-list',
|
||||
templateUrl: './text_list.component.html',
|
||||
styleUrls: ['./text_list.component.scss'],
|
||||
standalone: true,
|
||||
imports: [CommonModule, TranslateBlockComponent],
|
||||
})
|
||||
export class TextListComponent {
|
||||
@ViewChildren('translateBlock') translate_blocks: QueryList<TranslateBlockComponent> | null = null;
|
||||
elements_data: TranslateData[] = [];
|
||||
@Input() set elements(el: TranslateData[]) {
|
||||
this.elements_data = el;
|
||||
localStorage.setItem('translations', JSON.stringify(this.elements_data));
|
||||
}
|
||||
|
||||
get elements() {
|
||||
return this.elements_data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<div class="element">
|
||||
<h2>{{ index + 1 }}</h2>
|
||||
<div class="fields">
|
||||
<nwui-textarea [contenteditable]="false">{{ item.english_text }}</nwui-textarea>
|
||||
<nwui-textarea
|
||||
#translatedText
|
||||
[contenteditable]="isEditing"
|
||||
(leave)="saveTranslate($event)"
|
||||
[disabled]="!isEditing"
|
||||
>{{ item.translated_text }}</nwui-textarea
|
||||
>
|
||||
</div>
|
||||
<div class="btns">
|
||||
<nwui-button (click)="sendToGoogleTranslate()" [disabled]="translateLoading">Translate</nwui-button>
|
||||
<nwui-button (click)="isEditing = !isEditing" [disabled]="isEditing">Edit</nwui-button>
|
||||
<nwui-button (click)="clear()" [disabled]="!item.translated_text.length">Clear</nwui-button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,39 @@
|
||||
.element {
|
||||
display: flex;
|
||||
background-color: #413738;
|
||||
padding: 3rem 5rem;
|
||||
border-radius: 30px;
|
||||
flex-direction: row;
|
||||
width: 100%;
|
||||
max-width: 1200px;
|
||||
align-items: center;
|
||||
gap: 2rem;
|
||||
justify-content: space-between;
|
||||
h2 {
|
||||
color: #efdee0;
|
||||
}
|
||||
.fields {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
max-width: 1200px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.element {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
|
||||
.btns {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
align-items: center;
|
||||
padding-inline: 1rem;
|
||||
nwui-button {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component, Input, OnInit, ViewChild } from '@angular/core';
|
||||
import { NWUIButtonComponent, NWUITextAreaComponent } from '@nwaifu-ui';
|
||||
import { LocalStorageKeys } from '../../consts';
|
||||
import { TranslateData } from '../../dto/translate_data.dto';
|
||||
import { ETranslateService } from '../../services/translate.enums';
|
||||
import { TranslateService } from '../../services/translate.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-translate-block',
|
||||
templateUrl: './translate_block.component.html',
|
||||
styleUrls: ['./translate_block.component.scss'],
|
||||
standalone: true,
|
||||
imports: [CommonModule, NWUIButtonComponent, NWUITextAreaComponent],
|
||||
providers: [TranslateService],
|
||||
})
|
||||
export class TranslateBlockComponent implements OnInit {
|
||||
@ViewChild('translatedText') translatedText: NWUITextAreaComponent | null = null;
|
||||
@Input({ required: true }) item: TranslateData = { english_text: '', translated_text: '' };
|
||||
@Input({ required: true }) index = 0;
|
||||
translateLoading = false;
|
||||
isEditing = false;
|
||||
|
||||
saveChanges() {
|
||||
const data: TranslateData[] = JSON.parse(localStorage.getItem(LocalStorageKeys.TRANSLATIONS) ?? '[]');
|
||||
if (!data.length) {
|
||||
alert('No data');
|
||||
return;
|
||||
}
|
||||
data[this.index] = this.item;
|
||||
localStorage.setItem('translations', JSON.stringify(data));
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.isEditing = this.item.translated_text === '';
|
||||
}
|
||||
|
||||
constructor(private translateService: TranslateService) {}
|
||||
|
||||
private sendToTranslate(service: ETranslateService = ETranslateService.GOOGLE) {
|
||||
this.translateLoading = true;
|
||||
this.translateService.translate(this.item.english_text, service).subscribe((text) => {
|
||||
if (this.translatedText) if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = text;
|
||||
this.item.translated_text = text;
|
||||
this.isEditing = false;
|
||||
this.translateLoading = false;
|
||||
this.saveChanges();
|
||||
});
|
||||
}
|
||||
|
||||
sendToGoogleTranslate() {
|
||||
this.sendToTranslate(ETranslateService.GOOGLE);
|
||||
}
|
||||
|
||||
sendToDeeplTranslate() {
|
||||
this.sendToTranslate(ETranslateService.DEEPL);
|
||||
}
|
||||
|
||||
sendToPromptTranslate() {
|
||||
this.sendToTranslate(ETranslateService.PROMPT);
|
||||
}
|
||||
|
||||
saveTranslate(text: string) {
|
||||
this.isEditing = false;
|
||||
if (this.translatedText) if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = '';
|
||||
this.item.translated_text = text;
|
||||
this.saveChanges();
|
||||
}
|
||||
clear() {
|
||||
this.item.translated_text = '';
|
||||
this.isEditing = true;
|
||||
if (this.translatedText) if (this.translatedText.ref) this.translatedText.ref.nativeElement.textContent = '';
|
||||
this.saveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum LocalStorageKeys {
|
||||
TRANSLATIONS = 'translations',
|
||||
ORIGINAL_FILE = 'original_file',
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export interface TranslateData {
|
||||
english_text: string;
|
||||
translated_text: string;
|
||||
}
|
||||
export interface NpsFile {
|
||||
file_name: string;
|
||||
original_text: string;
|
||||
translated_text?: string;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { LocalStorageKeys } from '../consts';
|
||||
import { NpsFile } from '../dto/translate_data.dto';
|
||||
|
||||
export function saveOriginalFile(file: NpsFile) {
|
||||
localStorage.setItem(LocalStorageKeys.ORIGINAL_FILE, JSON.stringify(file));
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { TranslateData } from '../dto/translate_data.dto';
|
||||
|
||||
export function parse(text: string): TranslateData[] {
|
||||
const replaced_text = text
|
||||
.replace('/<[kK]{1}>/gm', '\n')
|
||||
.replace(/(<[^>]*>|\/\/.*)/gm, '')
|
||||
.replace(/\s*\n\s*/gm, '\n');
|
||||
const result = replaced_text
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.map((line) => ({ english_text: line, translated_text: '' }));
|
||||
console.log(result);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<div class="btns">
|
||||
<nwui-button (click)="fileInput.click()">
|
||||
<span><i class="lni lni-upload"></i> Upload</span>
|
||||
<input type="file" (change)="submitFile($event)" accept=".nps" #fileInput style="display: none" />
|
||||
</nwui-button>
|
||||
<nwui-button (click)="onSaveClicked()">
|
||||
<span><i class="lni lni-save"></i> Save</span>
|
||||
</nwui-button>
|
||||
@if (this.elements.length){
|
||||
<nwui-button (click)="clearAllTranslations()" [disabled]="!has_translations">
|
||||
<span><i class="lni lni-trash-can"></i> Clear translations</span>
|
||||
</nwui-button>
|
||||
<nwui-button (click)="getAllTranslations()">
|
||||
<span><i class="lni lni-google"></i> Translate all</span>
|
||||
</nwui-button>
|
||||
<nwui-button (click)="onClearClicked()">
|
||||
<span><i class="lni lni-trash-can"></i> Clear</span>
|
||||
</nwui-button>
|
||||
}
|
||||
</div>
|
||||
<app-text-list [elements]="elements"></app-text-list>
|
||||
@@ -0,0 +1,9 @@
|
||||
.btns {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: flex-start;
|
||||
align-items: center;
|
||||
nwui-button {
|
||||
margin: 2rem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component, OnInit, ViewChild } from '@angular/core';
|
||||
import { NWUIButtonComponent } from '@nwaifu-ui';
|
||||
import { TextListComponent } from './components/text_list/text_list.component';
|
||||
import { LocalStorageKeys } from './consts';
|
||||
import { NpsFile, TranslateData } from './dto/translate_data.dto';
|
||||
import { saveOriginalFile } from './lib/file_tools';
|
||||
import { parse } from './lib/parser';
|
||||
@Component({
|
||||
standalone: true,
|
||||
imports: [TextListComponent, CommonModule, NWUIButtonComponent],
|
||||
selector: 'app-nitroplus',
|
||||
templateUrl: './nitroplus-translator.component.html',
|
||||
styleUrl: './nitroplus-translator.component.less',
|
||||
})
|
||||
export class NitroplusComponent implements OnInit {
|
||||
title = 'NitroPlusTranslator';
|
||||
elements: TranslateData[] = [];
|
||||
@ViewChild('fileInput') fileInput: HTMLInputElement | null = null;
|
||||
@ViewChild(TextListComponent) text_list: TextListComponent | null = null;
|
||||
|
||||
ngOnInit(): void {
|
||||
const data = localStorage.getItem(LocalStorageKeys.TRANSLATIONS);
|
||||
if (data) {
|
||||
try {
|
||||
this.elements = JSON.parse(data);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
alert('Error while loading');
|
||||
localStorage.removeItem(LocalStorageKeys.TRANSLATIONS);
|
||||
this.elements = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
submitFile($event: Event) {
|
||||
const target = $event.target as HTMLInputElement;
|
||||
if (target.files) {
|
||||
const file = target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
if (reader.result) {
|
||||
this.onFileLoaded(reader.result.toString());
|
||||
const original_file: NpsFile = {
|
||||
file_name: file.name,
|
||||
original_text: reader.result.toString(),
|
||||
};
|
||||
saveOriginalFile(original_file);
|
||||
target.value = '';
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onFileLoaded(text: string) {
|
||||
const parsed = parse(text);
|
||||
this.elements = parsed;
|
||||
}
|
||||
|
||||
onClearClicked() {
|
||||
this.elements = [];
|
||||
localStorage.removeItem(LocalStorageKeys.TRANSLATIONS);
|
||||
}
|
||||
|
||||
onSaveClicked() {
|
||||
const original_file: NpsFile = JSON.parse(
|
||||
localStorage.getItem(LocalStorageKeys.ORIGINAL_FILE) ?? '{"file_name":"", "original_text":""}',
|
||||
);
|
||||
if (original_file.file_name && original_file.original_text) {
|
||||
const data: TranslateData[] = JSON.parse(localStorage.getItem(LocalStorageKeys.TRANSLATIONS) ?? '[]');
|
||||
if (!data.length) {
|
||||
alert('No data');
|
||||
return;
|
||||
}
|
||||
original_file.translated_text = original_file.original_text;
|
||||
data.forEach((el) => {
|
||||
original_file.translated_text = original_file.translated_text?.replace(el.english_text, el.translated_text);
|
||||
});
|
||||
|
||||
const element = document.createElement('a');
|
||||
const file = new Blob([original_file.translated_text], { type: 'text/plain' });
|
||||
element.href = URL.createObjectURL(file);
|
||||
element.download = original_file.file_name;
|
||||
element.click();
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Dialog windows for clear op
|
||||
clearAllTranslations() {
|
||||
if (this.text_list)
|
||||
if (this.text_list.translate_blocks) {
|
||||
this.text_list.translate_blocks.filter((item) => item.item.translated_text).forEach((item) => item.clear());
|
||||
}
|
||||
}
|
||||
|
||||
getAllTranslations() {
|
||||
if (this.text_list)
|
||||
if (this.text_list.translate_blocks)
|
||||
this.text_list.translate_blocks.forEach((item) => item.sendToGoogleTranslate());
|
||||
}
|
||||
|
||||
get has_translations(): boolean {
|
||||
return this.elements.some((i) => i.translated_text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export type GoogleTranslateResponse = Array<Array<string>>;
|
||||
@@ -0,0 +1,5 @@
|
||||
export enum ETranslateService {
|
||||
GOOGLE = 'google',
|
||||
DEEPL = 'deepl',
|
||||
PROMPT = 'prompt',
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, map } from 'rxjs';
|
||||
import { GoogleTranslateResponse } from './translate.dto';
|
||||
import { ETranslateService } from './translate.enums';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class TranslateService {
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
private googleTranslate(text: string): Observable<string> {
|
||||
return this.http
|
||||
.get<GoogleTranslateResponse>(
|
||||
'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=ru&dt=t&q=' + encodeURIComponent(text),
|
||||
)
|
||||
.pipe(
|
||||
map((response) => {
|
||||
let result = '';
|
||||
response[0].forEach((el) => {
|
||||
result += el[0] + ' ';
|
||||
});
|
||||
return result;
|
||||
}),
|
||||
);
|
||||
}
|
||||
translate(text: string, service: ETranslateService = ETranslateService.GOOGLE): Observable<string> {
|
||||
switch (service) {
|
||||
case ETranslateService.GOOGLE:
|
||||
return this.googleTranslate(text);
|
||||
default:
|
||||
return this.googleTranslate(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
<h1>Test</h1>
|
||||
@@ -1,12 +0,0 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-second',
|
||||
templateUrl: './second.component.html',
|
||||
styleUrls: ['./second.component.less'],
|
||||
standalone: true,
|
||||
imports: [CommonModule]
|
||||
})
|
||||
export class SecondComponent {
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../dist/out-tsc",
|
||||
"types": []
|
||||
|
||||
33
apps/NwaifuWeb/tsconfig.json
Normal file
33
apps/NwaifuWeb/tsconfig.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"useDefineForClassFields": false,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"files": [],
|
||||
"include": [],
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true,
|
||||
"strictTemplates": true
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.app.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.spec.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
17
apps/NwaifuWeb/tsconfig.lib.json
Normal file
17
apps/NwaifuWeb/tsconfig.lib.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../dist/out-tsc",
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"inlineSources": true,
|
||||
"types": []
|
||||
},
|
||||
"exclude": [
|
||||
"src/**/*.spec.ts",
|
||||
"src/**/*.test.ts"
|
||||
],
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user