--- /dev/null
+<ng-template #dialogContent>
+ <div class="modal-header bg-info">
+ <h4 class="modal-title" i18n>
+ Translating field "{{idlClassDef.field_map[field].label}}"
+ of "{{idlClassDef.label}}".
+ </h4>
+ <button type="button" class="close"
+ i18n-aria-label aria-label="Close"
+ (click)="dismiss('cross_click')">
+ <span aria-hidden="true">×</span>
+ </button>
+ </div>
+ <div class="modal-body form-common form-validated" *ngIf="idlObj">
+ <div class="form-group row">
+ <label class="col-lg-4 text-right font-weight-bold"
+ i18n>Current Value</label>
+ <input
+ type="text"
+ [disabled]="true"
+ class="form-control col-lg-7"
+ value="{{idlObj[field]()}}">
+ </div>
+ <div class="form-group row">
+ <label class="col-lg-4 text-right font-weight-bold"
+ i18n>Select Locale</label>
+ <select class="form-control col-lg-7"
+ (change)="localeChanged($event)"
+ [(ngModel)]="selectedLocale">
+ <option value="{{locale.code()}}" *ngFor="let locale of locales">
+ {{locale.name()}}
+ </option>
+ </select>
+ </div>
+ <div class="form-group row">
+ <label class="col-lg-4 text-right font-weight-bold" i18n>Translation</label>
+ <input
+ id='translation-input'
+ type="text"
+ class="form-control col-lg-7"
+ required
+ i18n-placeholder
+ (keyup.enter)="translate()"
+ placeholder="Translation..."
+ [(ngModel)]="translatedValue"/>
+ </div>
+ </div>
+ <div class="modal-footer">
+ <button (click)="translate()" class="btn btn-info" i18n>Apply</button>
+ <button (click)="dismiss('canceled')" class="btn btn-warning ml-2" i18n>Cancel</button>
+ </div>
+</ng-template>
--- /dev/null
+import {Component, OnInit, Input, Renderer2} from '@angular/core';
+import {IdlService, IdlObject} from '@eg/core/idl.service';
+import {ToastService} from '@eg/share/toast/toast.service';
+import {LocaleService} from '@eg/core/locale.service';
+import {AuthService} from '@eg/core/auth.service';
+import {PcrudService} from '@eg/core/pcrud.service';
+import {DialogComponent} from '@eg/share/dialog/dialog.component';
+import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
+
+@Component({
+ selector: 'eg-translate',
+ templateUrl: 'translate.component.html'
+})
+
+export class TranslateComponent
+ extends DialogComponent implements OnInit {
+
+ idlClassDef: any;
+ locales: IdlObject[];
+ selectedLocale: string;
+ translatedValue: string;
+ existingTranslation: IdlObject;
+
+ idlObj: IdlObject;
+ @Input() set idlObject(o: IdlObject) {
+ if (o) {
+ this.idlObj = o;
+ this.idlClassDef = this.idl.classes[o.classname];
+ this.fetchTranslation();
+ }
+ }
+
+ field: string;
+ @Input() set fieldName(n: string) {
+ this.field = n;
+ }
+
+ constructor(
+ private modal: NgbModal, // required for passing to parent
+ private renderer: Renderer2,
+ private idl: IdlService,
+ private toast: ToastService,
+ private locale: LocaleService,
+ private pcrud: PcrudService,
+ private auth: AuthService) {
+ super(modal);
+ }
+
+ ngOnInit() {
+ // Default to the login locale
+ this.selectedLocale = this.locale.currentLocaleCode();
+ this.locales = [];
+ this.locale.supportedLocales().subscribe(l => this.locales.push(l));
+
+ this.onOpen$.subscribe(() => {
+ const elm = this.renderer.selectRootElement('#translation-input');
+ if (elm) {
+ elm.focus();
+ elm.select();
+ }
+ });
+ }
+
+ localeChanged(code: string) {
+ this.fetchTranslation();
+ }
+
+ fetchTranslation() {
+ const exist = this.existingTranslation;
+
+ if (exist
+ && exist.fq_field() === this.fqField()
+ && exist.identity_value() === this.identValue()) {
+ // Already have the current translation object.
+ return;
+ }
+
+ this.translatedValue = '';
+ this.existingTranslation = null;
+
+ this.pcrud.search('i18n', {
+ translation: this.selectedLocale,
+ fq_field : this.fqField(),
+ identity_value: this.identValue()
+ }).subscribe(tr => {
+ this.existingTranslation = tr;
+ this.translatedValue = tr.string();
+ console.debug('found existing translation ', tr);
+ });
+ }
+
+ fqField(): string {
+ return this.idlClassDef.classname + '.' + this.field;
+ }
+
+ identValue(): string {
+ return this.idlObj[this.idlClassDef.pkey || 'id']();
+ }
+
+ translate() {
+ if (!this.translatedValue) return;
+
+ if (this.existingTranslation) {
+ const entry = this.existingTranslation;
+ entry.string(this.translatedValue);
+
+ this.pcrud.update(entry).toPromise().then(
+ ok => this.close('Translation updated'),
+ err => console.error(err)
+ );
+
+ return;
+ }
+
+ const entry = this.idl.create('i18n');
+ entry.fq_field(this.fqField());
+ entry.identity_value(this.identValue());
+ entry.translation(this.selectedLocale);
+ entry.string(this.translatedValue);
+
+ this.pcrud.create(entry).toPromise().then(
+ ok => this.close('Translation created'),
+ err => console.error('Translation creation failed')
+ );
+ }
+}
+
+