language autodetection functionnal

This commit is contained in:
Nicolas Constant 2023-08-12 01:33:07 -04:00
parent 14287b476c
commit 0b93ed7307
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 82 additions and 1 deletions

View File

@ -68,6 +68,8 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
this.detectAutosuggestion(value);
this._status = value;
this.languageService.autoDetectLang(value);
setTimeout(() => {
this.autoGrow();
}, 0);

View File

@ -1,10 +1,30 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class MyElectronService {
detectedLangSubject = new Subject<DetectedLang[]>();
constructor() {
try {
if ((<any>window).api) {
(<any>window).api.receive("detectedLang", (data) => {
const result = [];
for (const l of data) {
let newLang = new DetectedLang(l[0], l[1]);
result.push(newLang);
}
this.detectedLangSubject.next(result);
});
}
}
catch (err) {
console.error(err);
}
this.detectLang("ceci est une phrase");
}
setLang(lang: string) {
@ -17,4 +37,22 @@ export class MyElectronService {
console.error(err);
}
}
detectLang(text: string) {
try {
if ((<any>window).api) {
(<any>window).api.send("detectLang", text);
}
}
catch (err) {
console.error(err);
}
}
}
export class DetectedLang {
constructor(
public lang: string,
public score: number
) {}
}

View File

@ -3,7 +3,7 @@ import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { ILanguage } from '../states/settings.state';
import { MyElectronService } from './electron.service';
import { DetectedLang, MyElectronService } from './electron.service';
import { SettingsService } from './settings.service';
@Injectable({
@ -19,6 +19,47 @@ export class LanguageService {
) {
this.configuredLanguagesChanged.next(this.getConfiguredLanguages());
this.selectedLanguageChanged.next(this.getSelectedLanguage());
this.electronService.detectedLangSubject.subscribe(l => {
this.detectedLanguage(l);
});
}
private detectedLanguage(lang: DetectedLang[]) {
if(!lang) return;
console.warn(lang);
if(lang.length >= 1){
const languages = this.getConfiguredLanguages();
let firstLang = lang[0].lang;
let firstLocalLang = languages.find(x => x.iso639 == firstLang);
if(firstLocalLang){
this.setSelectedLanguage(firstLocalLang);
return;
}
if(lang.length > 1){
firstLang = lang[1].lang;
firstLocalLang = languages.find(x => x.iso639 == firstLang);
if(firstLocalLang){
this.setSelectedLanguage(firstLocalLang);
return;
}
}
}
}
autoDetectLang(text: string): void {
if(!text || text.length < 6) return;
console.warn('autodetect');
console.warn(text);
//TODO: add settings to disable
this.electronService.detectLang(text);
}
getSelectedLanguage(): ILanguage {