language autodetection functionnal
This commit is contained in:
parent
14287b476c
commit
0b93ed7307
|
@ -68,6 +68,8 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
|
||||||
this.detectAutosuggestion(value);
|
this.detectAutosuggestion(value);
|
||||||
this._status = value;
|
this._status = value;
|
||||||
|
|
||||||
|
this.languageService.autoDetectLang(value);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.autoGrow();
|
this.autoGrow();
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
|
@ -1,10 +1,30 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Subject } from 'rxjs';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class MyElectronService {
|
export class MyElectronService {
|
||||||
|
detectedLangSubject = new Subject<DetectedLang[]>();
|
||||||
|
|
||||||
constructor() {
|
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) {
|
setLang(lang: string) {
|
||||||
|
@ -17,4 +37,22 @@ export class MyElectronService {
|
||||||
console.error(err);
|
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
|
||||||
|
) {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Injectable } from '@angular/core';
|
||||||
import { BehaviorSubject } from 'rxjs';
|
import { BehaviorSubject } from 'rxjs';
|
||||||
|
|
||||||
import { ILanguage } from '../states/settings.state';
|
import { ILanguage } from '../states/settings.state';
|
||||||
import { MyElectronService } from './electron.service';
|
import { DetectedLang, MyElectronService } from './electron.service';
|
||||||
import { SettingsService } from './settings.service';
|
import { SettingsService } from './settings.service';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
|
@ -19,6 +19,47 @@ export class LanguageService {
|
||||||
) {
|
) {
|
||||||
this.configuredLanguagesChanged.next(this.getConfiguredLanguages());
|
this.configuredLanguagesChanged.next(this.getConfiguredLanguages());
|
||||||
this.selectedLanguageChanged.next(this.getSelectedLanguage());
|
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 {
|
getSelectedLanguage(): ILanguage {
|
||||||
|
|
Loading…
Reference in New Issue