Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ mat-form-field.translatable-form-field {
margin-top: 4px;
}
}

.translatable-input-hints {
display: flex;
}

.translation-suggestions {
margin-left: 8px;
text-decoration: underline;
color: blue;
}

.translation-suggestions:hover {
cursor: pointer;
}

.translation-tools {
padding: 8px 0;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Input, Signal, Output, computed, Directive } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Subject, Subscription, debounceTime } from 'rxjs';
import { Language } from '../../../../../app/domain/language';
import { TeacherProjectTranslationService } from '../../../services/teacherProjectTranslationService';
import { TeacherProjectService } from '../../../services/teacherProjectService';
import { generateRandomKey } from '../../../common/string/string';
import { toObservable } from '@angular/core/rxjs-interop';
import { Translations } from '../../../../../app/domain/translations';
import { TranslationSuggestionsDialogComponent } from '../translation-suggestions-dialog/translation-suggestions-dialog.component';
import { copy } from '../../../common/object/object';

@Directive()
Expand All @@ -27,6 +29,7 @@ export abstract class AbstractTranslatableFieldComponent {
protected translationText: string;
protected translationTextChanged: Subject<string> = new Subject<string>();
constructor(
protected dialog: MatDialog,
protected projectService: TeacherProjectService,
protected projectTranslationService: TeacherProjectTranslationService
) {}
Expand Down Expand Up @@ -75,4 +78,41 @@ export abstract class AbstractTranslatableFieldComponent {
currentTranslations[this.i18nId] = { value: text, modified: new Date().getTime() };
this.projectTranslationService.saveCurrentTranslations(currentTranslations).subscribe();
}

protected async translateText(): Promise<void> {
if (this.translationText) {
this.openDialog();
} else {
this.projectTranslationService
.getTranslationSuggestion(
this.defaultLanguage.language,
this.currentLanguage().language,
this.content[this.key]
)
.subscribe((suggestedTranslation: string) => {
this.saveTranslationText(suggestedTranslation);
});
}
}

private openDialog(): void {
const dialogRef = this.createDialogRef();
dialogRef.afterClosed().subscribe((result: string) => {
if (result || result === '') {
this.saveTranslationText(result);
}
});
}

private createDialogRef(): MatDialogRef<TranslationSuggestionsDialogComponent> {
return this.dialog.open(TranslationSuggestionsDialogComponent, {
width: '40%',
data: {
defaultLanguage: this.defaultLanguage.language,
currentLanguage: this.currentLanguage().language,
defaultLanguageContent: this.content[this.key],
currentLanguageContent: this.translationText
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class TranslatableAssetChooserComponent extends AbstractTranslatableField
};

constructor(
private dialog: MatDialog,
protected dialog: MatDialog,
protected projectService: TeacherProjectService,
protected projectTranslationService: TeacherProjectTranslationService
) {
super(projectService, projectTranslationService);
super(dialog, projectService, projectTranslationService);
}

protected chooseAsset(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@
@if (hint) {
<mat-hint>{{ hint }}</mat-hint>
}
<mat-hint class="source-language selected-bg-bg">
<strong><mat-icon>translate</mat-icon> {{ defaultLanguage.language }}:</strong>
{{ content[key] }}
<mat-hint class="translatable-input-hints">
<div class="source-language selected-bg-bg">
<strong><mat-icon>translate</mat-icon> {{ defaultLanguage.language }}:</strong>
{{ content[key] }}
</div>
@if (content[key]) {
<div class="source-language selected-bg-bg translation-suggestions">
<span (click)="translateText()"><mat-icon>auto_awesome</mat-icon> Translate with AI</span>
</div>
}
</mat-hint>
</mat-form-field>
} @else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
</ng-template>
</mat-tab>
</mat-tab-group>
@if (content[key]) {
<div class="translatable translatable-input-hints">
<mat-hint class="source-language selected-bg-bg translation-suggestions">
<span (click)="translateText()"><mat-icon>auto_awesome</mat-icon> Translate with AI</span>
</mat-hint>
</div>
}
} @else {
<wise-authoring-tinymce-editor [(model)]="html" (modelChange)="saveDefaultLanguageText()" />
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import { insertWiseLinks, replaceWiseLinks } from '../../../common/wise-link/wis
import { ConfigService } from '../../../services/configService';
import { TeacherProjectTranslationService } from '../../../services/teacherProjectTranslationService';
import { TeacherProjectService } from '../../../services/teacherProjectService';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';

@Component({
imports: [MatButtonModule, MatTabsModule, WiseAuthoringTinymceEditorComponent],
imports: [
MatButtonModule,
MatDialogModule,
MatIconModule,
MatTabsModule,
WiseAuthoringTinymceEditorComponent
],
selector: 'translatable-rich-text-editor',
styles: ['.translation-tools { padding: 8px 0; }'],
styleUrl: '../abstract-translatable-field/abstract-translatable-field.component.scss',
templateUrl: './translatable-rich-text-editor.component.html'
})
export class TranslatableRichTextEditorComponent extends AbstractTranslatableFieldComponent {
Expand All @@ -20,10 +28,11 @@ export class TranslatableRichTextEditorComponent extends AbstractTranslatableFie

constructor(
private configService: ConfigService,
protected dialog: MatDialog,
protected projectService: TeacherProjectService,
protected projectTranslationService: TeacherProjectTranslationService
) {
super(projectService, projectTranslationService);
super(dialog, projectService, projectTranslationService);
}

ngOnChanges(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
@if (hint) {
<mat-hint>{{ hint }}</mat-hint>
}
<mat-hint class="source-language selected-bg-bg">
<strong><mat-icon>translate</mat-icon> {{ defaultLanguage.language }}:</strong>
{{ content[key] }}
<mat-hint class="translatable-input-hints">
<div class="source-language selected-bg-bg">
<strong><mat-icon>translate</mat-icon> {{ defaultLanguage.language }}:</strong>
{{ content[key] }}
</div>
@if (content[key]) {
<div class="source-language selected-bg-bg translation-suggestions">
<span (click)="translateText()"><mat-icon>auto_awesome</mat-icon> Translate with AI</span>
</div>
}
</mat-hint>
</mat-form-field>
} @else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<mat-dialog-content>
<p>
<b>{{ this.data.defaultLanguage }} text</b>
</p>
<p class="text-content">{{ this.data.defaultLanguageContent }}</p>
<p>
<b>{{ this.data.currentLanguage }} text (current translation)</b>
</p>
<p class="text-content">{{ this.data.currentLanguageContent }}</p>
<p>
<b>{{ this.data.currentLanguage }} text (AI suggested translation)</b>
</p>
<p class="text-content">{{ this.translation }}</p>
<p class="replace-translation">
Do you want to replace the current translation with the AI suggested translation?
</p>
</mat-dialog-content>
<mat-dialog-actions align="start">
<button mat-raised-button (click)="onClose(true)">Replace</button>
<button mat-raised-button (click)="onClose(false)">Cancel</button>
</mat-dialog-actions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.text-content {
margin-left: 15px;
}

.replace-translation {
margin-top: 20px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TranslationSuggestionsDialogComponent } from './translation-suggestions-dialog.component';

describe('TranslationSuggestionsDialogComponent', () => {
let component: TranslationSuggestionsDialogComponent;
let fixture: ComponentFixture<TranslationSuggestionsDialogComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TranslationSuggestionsDialogComponent]
})
.compileComponents();

fixture = TestBed.createComponent(TranslationSuggestionsDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import {
MAT_DIALOG_DATA,
MatDialogActions,
MatDialogContent,
MatDialogRef
} from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { TeacherProjectTranslationService } from '../../../services/teacherProjectTranslationService';

interface TranslationSuggestionsDialogData {
defaultLanguage: string;
currentLanguage: string;
defaultLanguageContent: string;
currentLanguageContent?: string;
}

@Component({
selector: 'translation-suggestions-dialog',
imports: [
MatFormFieldModule,
MatInputModule,
FormsModule,
MatButtonModule,
MatDialogContent,
MatDialogActions
],
templateUrl: './translation-suggestions-dialog.component.html',
styleUrl: './translation-suggestions-dialog.component.scss'
})
export class TranslationSuggestionsDialogComponent {
readonly dialogRef = inject(MatDialogRef<TranslationSuggestionsDialogComponent>);
readonly data = inject<TranslationSuggestionsDialogData>(MAT_DIALOG_DATA);
protected translation;

constructor(protected projectTranslationService: TeacherProjectTranslationService) {
this.generateTranslationSuggestion();
}

private generateTranslationSuggestion(): void {
this.projectTranslationService
.getTranslationSuggestion(
this.data.defaultLanguage,
this.data.currentLanguage,
this.data.defaultLanguageContent
)
.subscribe((suggestedTranslation: string) => {
this.translation = suggestedTranslation;
});
}

protected onClose(saveTranslation: boolean): void {
this.dialogRef.close(saveTranslation && this.translation);
}
}
31 changes: 31 additions & 0 deletions src/assets/wise5/services/teacherProjectTranslationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,35 @@ export class TeacherProjectTranslationService extends ProjectTranslationService
})
);
}

getTranslationSuggestion(
defaultLanguage: string,
currentLanguage: string,
defaultLanguageText: string
): Observable<string> {
return this.http
.post(
`/api/author/project/translate/translationSuggestions`,
{
srcLang: defaultLanguage,
targetLang: currentLanguage,
srcText: this.addDoNotTranslateTags(defaultLanguageText)
},
{ responseType: 'text' }
)
.pipe(map(this.removeDoNotTranslateTags));
}

private addDoNotTranslateTags(textToTranslate: string): string {
return textToTranslate.replaceAll(
/<.*?>/g,
(match) => '<span translate="no">' + match + '</span>'
);
}

private removeDoNotTranslateTags(translatedText: string): string {
return translatedText.replaceAll(/<span translate="no"><.*?><\/span>/g, (match) =>
match.slice(21, -7)
);
}
}
Loading