1 import {Component, OnInit, Input, Renderer2} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {ToastService} from '@eg/share/toast/toast.service';
4 import {LocaleService} from '@eg/core/locale.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {DialogComponent} from '@eg/share/dialog/dialog.component';
8 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
11 selector: 'eg-translate',
12 templateUrl: 'translate.component.html'
15 export class TranslateComponent
16 extends DialogComponent implements OnInit {
20 selectedLocale: string;
21 translatedValue: string;
22 existingTranslation: IdlObject;
24 // These actions should update the idlObject and/or fieldName values,
25 // forcing the dialog to load a new string to translate. When set,
26 // applying a translation in the dialog will leave the dialog window open
27 // so the next/prev buttons can be used to fetch the next string.
28 nextString: () => void;
29 prevString: () => void;
32 @Input() set idlObject(o: IdlObject) {
35 this.idlClassDef = this.idl.classes[o.classname];
36 this.fetchTranslation();
41 @Input() set fieldName(n: string) {
46 private modal: NgbModal, // required for passing to parent
47 private renderer: Renderer2,
48 private idl: IdlService,
49 private toast: ToastService,
50 private locale: LocaleService,
51 private pcrud: PcrudService,
52 private auth: AuthService) {
57 // Default to the login locale
58 this.selectedLocale = this.locale.currentLocaleCode();
60 this.locale.supportedLocales().subscribe(l => this.locales.push(l));
62 this.onOpen$.subscribe(() => {
63 const elm = this.renderer.selectRootElement('#translation-input');
71 localeChanged(code: string) {
72 this.fetchTranslation();
76 const exist = this.existingTranslation;
79 && exist.fq_field() === this.fqField()
80 && exist.identity_value() === this.identValue()) {
81 // Already have the current translation object.
85 this.translatedValue = '';
86 this.existingTranslation = null;
88 this.pcrud.search('i18n', {
89 translation: this.selectedLocale,
90 fq_field : this.fqField(),
91 identity_value: this.identValue()
93 this.existingTranslation = tr;
94 this.translatedValue = tr.string();
95 console.debug('found existing translation ', tr);
100 return this.idlClassDef.classname + '.' + this.field;
103 identValue(): string {
104 return this.idlObj[this.idlClassDef.pkey || 'id']();
108 if (!this.translatedValue) { return; }
112 if (this.existingTranslation) {
113 entry = this.existingTranslation;
114 entry.string(this.translatedValue);
116 this.pcrud.update(entry).toPromise().then(
118 if (!this.nextString) {
119 this.close('Translation updated');
122 err => console.error(err)
128 entry = this.idl.create('i18n');
129 entry.fq_field(this.fqField());
130 entry.identity_value(this.identValue());
131 entry.translation(this.selectedLocale);
132 entry.string(this.translatedValue);
134 this.pcrud.create(entry).toPromise().then(
136 if (!this.nextString) {
137 this.close('Translation created');
140 err => console.error('Translation creation failed')