Skip to content

Commit 34b95f8

Browse files
authored
Replace Transloco translate() with i18n.selectTranslate() where possible (#3319)
1 parent 6c1f073 commit 34b95f8

File tree

28 files changed

+148
-125
lines changed

28 files changed

+148
-125
lines changed

src/SIL.XForge.Scripture/ClientApp/src/app/app.component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Component, DestroyRef, Inject, OnDestroy, OnInit } from '@angular/core'
44
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
55
import { NavigationEnd, Router } from '@angular/router';
66
import Bugsnag from '@bugsnag/js';
7-
import { translate } from '@ngneat/transloco';
87
import { cloneDeep } from 'lodash-es';
98
import { CookieService } from 'ngx-cookie-service';
109
import { SystemRole } from 'realtime-server/lib/esm/common/models/system-role';
@@ -356,12 +355,12 @@ export class AppComponent extends DataLoadingComponent implements OnInit, OnDest
356355
if (this.currentUser == null) {
357356
return;
358357
} else if (!this.isAppOnline) {
359-
this.noticeService.show(translate('app.action_not_available_offline'));
358+
this.noticeService.show(this.i18n.translateStatic('app.action_not_available_offline'));
360359
} else {
361360
this.authService
362361
.changePassword(this.currentUser.email)
363362
.then(() => {
364-
this.noticeService.show(translate('app.password_reset_email_sent'));
363+
this.noticeService.show(this.i18n.translateStatic('app.password_reset_email_sent'));
365364
})
366365
.catch(() => {
367366
this.dialogService.message('app.cannot_change_password');
@@ -373,7 +372,7 @@ export class AppComponent extends DataLoadingComponent implements OnInit, OnDest
373372
if (this.isAppOnline) {
374373
this.userService.editDisplayName(false);
375374
} else {
376-
this.noticeService.show(translate('app.action_not_available_offline'));
375+
this.noticeService.show(this.i18n.translateStatic('app.action_not_available_offline'));
377376
}
378377
}
379378

src/SIL.XForge.Scripture/ClientApp/src/app/checking/checking-overview/checking-overview.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, DestroyRef, OnDestroy, OnInit } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
3-
import { translate } from '@ngneat/transloco';
43
import { Canon } from '@sillsdev/scripture';
54
import { saveAs } from 'file-saver';
65
import Papa from 'papaparse';
@@ -270,14 +269,16 @@ export class CheckingOverviewComponent extends DataLoadingComponent implements O
270269
}
271270

272271
questionCountLabel(count: number): string {
273-
return translate('checking_overview.question_count_label', { count: count });
272+
return this.i18n.translateStatic('checking_overview.question_count_label', { count: count });
274273
}
275274

276275
timeArchivedStamp(date: string | undefined): string {
277276
if (date == null) {
278277
return '';
279278
}
280-
return translate('checking_overview.time_archived_stamp', { timeStamp: this.i18n.formatDate(new Date(date)) });
279+
return this.i18n.translateStatic('checking_overview.time_archived_stamp', {
280+
timeStamp: this.i18n.formatDate(new Date(date))
281+
});
281282
}
282283

283284
bookAnswerCount(text: TextInfo): number {
@@ -306,7 +307,9 @@ export class CheckingOverviewComponent extends DataLoadingComponent implements O
306307

307308
answerCountLabel(count?: number): string {
308309
return count != null && count > 0
309-
? translate('checking_overview.answer_count_label', { count: this.l10nNumberPipe.transform(count) })
310+
? this.i18n.translateStatic('checking_overview.answer_count_label', {
311+
count: this.l10nNumberPipe.transform(count)
312+
})
310313
: '';
311314
}
312315

src/SIL.XForge.Scripture/ClientApp/src/app/checking/checking/checking-answers/checking-answers.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
ViewChildren
1111
} from '@angular/core';
1212
import { MatDialogRef } from '@angular/material/dialog';
13-
import { translate } from '@ngneat/transloco';
1413
import { VerseRef } from '@sillsdev/scripture';
1514
import { cloneDeep } from 'lodash-es';
1615
import { Operation } from 'realtime-server/lib/esm/common/models/project-rights';
@@ -454,9 +453,9 @@ export class CheckingAnswersComponent implements OnInit {
454453
answer
455454
});
456455
} else if (likeAnswerResponse === LikeAnswerResponse.DeniedOwnAnswer) {
457-
this.noticeService.show(translate('checking_answers.cannot_like_own_answer'));
456+
this.noticeService.show(this.i18n.translateStatic('checking_answers.cannot_like_own_answer'));
458457
} else if (likeAnswerResponse === LikeAnswerResponse.DeniedNoPermission) {
459-
this.noticeService.show(translate('checking_answers.no_permission_to_like'));
458+
this.noticeService.show(this.i18n.translateStatic('checking_answers.no_permission_to_like'));
460459
}
461460
}
462461

src/SIL.XForge.Scripture/ClientApp/src/app/checking/checking/checking-answers/checking-comments/checking-comments.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
2-
import { translate } from '@ngneat/transloco';
32
import { cloneDeep, sortBy } from 'lodash-es';
43
import { Operation } from 'realtime-server/lib/esm/common/models/project-rights';
54
import { Answer } from 'realtime-server/lib/esm/scriptureforge/models/answer';
@@ -8,6 +7,7 @@ import { SFProjectProfile } from 'realtime-server/lib/esm/scriptureforge/models/
87
import { SF_PROJECT_RIGHTS, SFProjectDomain } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-rights';
98
import { debounceTime } from 'rxjs/operators';
109
import { DialogService } from 'xforge-common/dialog.service';
10+
import { I18nService } from 'xforge-common/i18n.service';
1111
import { SubscriptionDisposable } from 'xforge-common/subscription-disposable';
1212
import { UserService } from 'xforge-common/user.service';
1313
import { QuestionDoc } from '../../../../core/models/question-doc';
@@ -44,7 +44,8 @@ export class CheckingCommentsComponent extends SubscriptionDisposable implements
4444

4545
constructor(
4646
private readonly dialogService: DialogService,
47-
private userService: UserService
47+
private userService: UserService,
48+
private readonly i18n: I18nService
4849
) {
4950
super();
5051
}
@@ -64,9 +65,9 @@ export class CheckingCommentsComponent extends SubscriptionDisposable implements
6465
}
6566

6667
if (unread > 0) {
67-
return translate('checking_comments.show_more_comments_and_unread', { count, unread });
68+
return this.i18n.translateStatic('checking_comments.show_more_comments_and_unread', { count, unread });
6869
} else {
69-
return translate('checking_comments.show_more_comments', { count });
70+
return this.i18n.translateStatic('checking_comments.show_more_comments', { count });
7071
}
7172
}
7273

src/SIL.XForge.Scripture/ClientApp/src/app/checking/checking/checking-answers/checking-question/checking-question.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
SimpleChanges,
1010
ViewChild
1111
} from '@angular/core';
12-
import { translate } from '@ngneat/transloco';
1312
import { VerseRef } from '@sillsdev/scripture';
1413
import { AudioTiming } from 'realtime-server/lib/esm/scriptureforge/models/audio-timing';
1514
import { getTextAudioId, TextAudio } from 'realtime-server/lib/esm/scriptureforge/models/text-audio';
@@ -113,7 +112,7 @@ export class CheckingQuestionComponent extends SubscriptionDisposable implements
113112
return this._questionDoc.data.text
114113
? this._questionDoc.data.text
115114
: this._questionDoc.data.audioUrl != null
116-
? translate('checking_questions.listen_to_question', {
115+
? this.i18n.translateStatic('checking_questions.listen_to_question', {
117116
referenceForDisplay: this.referenceForDisplay
118117
})
119118
: '';

src/SIL.XForge.Scripture/ClientApp/src/app/checking/question-dialog/question-dialog.component.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, DestroyRef, Inject, OnInit, ViewChild } from '@angular/core';
22
import { AbstractControl, FormControl, FormGroup, Validators } from '@angular/forms';
33
import { MAT_DIALOG_DATA, MatDialogConfig, MatDialogRef } from '@angular/material/dialog';
4-
import { translate } from '@ngneat/transloco';
54
import { VerseRef } from '@sillsdev/scripture';
65
import { cloneDeep } from 'lodash-es';
76
import { Question } from 'realtime-server/lib/esm/scriptureforge/models/question';
@@ -44,8 +43,8 @@ export class QuestionDialogComponent implements OnInit {
4443
@ViewChild(TextAndAudioComponent) textAndAudio?: TextAndAudioComponent;
4544
modeLabel =
4645
this.data && this.data.questionDoc != null
47-
? translate('question_dialog.edit_question')
48-
: translate('question_dialog.new_question');
46+
? this.i18n.translateStatic('question_dialog.edit_question')
47+
: this.i18n.translateStatic('question_dialog.new_question');
4948
parentAndStartMatcher = new ParentAndStartErrorStateMatcher();
5049
versesForm: FormGroup = new FormGroup(
5150
{
@@ -91,19 +90,19 @@ export class QuestionDialogComponent implements OnInit {
9190
}
9291

9392
get scriptureInputErrorMessages(): { startError: string; endError: string } {
94-
let start: string = translate('question_dialog.required_with_asterisk');
93+
let start: string = this.i18n.translateStatic('question_dialog.required_with_asterisk');
9594
if (this.scriptureStart.hasError('verseFormat')) {
96-
start = translate('question_dialog.example_verse');
95+
start = this.i18n.translateStatic('question_dialog.example_verse');
9796
} else if (this.scriptureStart.hasError('verseRange')) {
98-
start = translate('question_dialog.must_be_inside_verse_range');
97+
start = this.i18n.translateStatic('question_dialog.must_be_inside_verse_range');
9998
}
10099
let end: string = '';
101100
if (this.scriptureEnd.hasError('verseFormat')) {
102-
end = translate('question_dialog.example_verse');
101+
end = this.i18n.translateStatic('question_dialog.example_verse');
103102
} else if (this.scriptureEnd.hasError('verseRange')) {
104-
end = translate('question_dialog.must_be_inside_verse_range');
103+
end = this.i18n.translateStatic('question_dialog.must_be_inside_verse_range');
105104
} else if (this.versesForm.hasError('verseDifferentBookOrChapter')) {
106-
end = translate('question_dialog.must_be_same_book_and_chapter');
105+
end = this.i18n.translateStatic('question_dialog.must_be_same_book_and_chapter');
107106
}
108107
return { startError: start, endError: end };
109108
}

src/SIL.XForge.Scripture/ClientApp/src/app/core/translation-engine.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SFProjectUserConfig } from 'realtime-server/lib/esm/scriptureforge/mode
88
import { getTextDocId } from 'realtime-server/lib/esm/scriptureforge/models/text-data';
99
import { Observable } from 'rxjs';
1010
import { filter, share } from 'rxjs/operators';
11+
import { I18nService } from 'xforge-common/i18n.service';
1112
import { NoticeService } from 'xforge-common/notice.service';
1213
import { OfflineData, OfflineStore } from 'xforge-common/offline-store';
1314
import { OnlineStatusService } from 'xforge-common/online-status.service';
@@ -16,6 +17,7 @@ import { HttpClient } from '../machine-api/http-client';
1617
import { RemoteTranslationEngine } from '../machine-api/remote-translation-engine';
1718
import { EDITED_SEGMENTS, EditedSegmentData } from './models/edited-segment-data';
1819
import { SFProjectService } from './sf-project.service';
20+
1921
/**
2022
* A service to access features for translation suggestions and training the translation engine
2123
*/
@@ -38,7 +40,8 @@ export class TranslationEngineService {
3840
private readonly machineHttp: HttpClient,
3941
private readonly noticeService: NoticeService,
4042
private readonly router: Router,
41-
private destroyRef: DestroyRef
43+
private destroyRef: DestroyRef,
44+
private readonly i18n: I18nService
4245
) {
4346
this.onlineStatus$ = this.onlineStatusService.onlineStatus$.pipe(
4447
filter(online => online),
@@ -57,7 +60,7 @@ export class TranslationEngineService {
5760
if (!this.translationEngines.has(projectId)) {
5861
this.translationEngines.set(
5962
projectId,
60-
new RemoteTranslationEngine(projectId, this.machineHttp, this.noticeService, this.router)
63+
new RemoteTranslationEngine(projectId, this.machineHttp, this.noticeService, this.router, this.i18n)
6164
);
6265
}
6366
return this.translationEngines.get(projectId)!;

src/SIL.XForge.Scripture/ClientApp/src/app/machine-api/remote-translation-engine.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Router } from '@angular/router';
55
import { TranslationSources, WordGraph } from '@sillsdev/machine';
66
import { of, throwError } from 'rxjs';
77
import { anything, instance, mock, when } from 'ts-mockito';
8+
import { I18nService } from 'xforge-common/i18n.service';
89
import { NoticeService } from 'xforge-common/notice.service';
910
import { configureTestingModule, TestTranslocoModule } from 'xforge-common/test-utils';
1011
import { BuildDto } from './build-dto';
@@ -404,6 +405,7 @@ class TestEnvironment {
404405
readonly client: RemoteTranslationEngine;
405406
readonly mockedNoticeService: NoticeService;
406407
readonly mockedRouter: Router;
408+
readonly mockedI18nService: I18nService;
407409
readonly ngZone: NgZone;
408410

409411
constructor() {
@@ -425,11 +427,13 @@ class TestEnvironment {
425427
);
426428
this.mockedNoticeService = mock(NoticeService);
427429
this.mockedRouter = mock(Router);
430+
this.mockedI18nService = mock(I18nService);
428431
this.client = new RemoteTranslationEngine(
429432
'project01',
430433
instance(this.mockedHttpClient),
431434
instance(this.mockedNoticeService),
432-
instance(this.mockedRouter)
435+
instance(this.mockedRouter),
436+
instance(this.mockedI18nService)
433437
);
434438

435439
this.ngZone = TestBed.inject(NgZone);

src/SIL.XForge.Scripture/ClientApp/src/app/machine-api/remote-translation-engine.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Router } from '@angular/router';
2-
import { translate } from '@ngneat/transloco';
32
import {
43
createRange,
54
InteractiveTranslationEngine,
@@ -13,6 +12,7 @@ import {
1312
} from '@sillsdev/machine';
1413
import { lastValueFrom, Observable, of, throwError } from 'rxjs';
1514
import { catchError, expand, filter, map, mergeMap, share, startWith, takeWhile } from 'rxjs/operators';
15+
import { I18nService } from 'xforge-common/i18n.service';
1616
import { NoticeService } from 'xforge-common/notice.service';
1717
import { AlignedWordPairDto } from './aligned-word-pair-dto';
1818
import { BuildDto } from './build-dto';
@@ -35,7 +35,8 @@ export class RemoteTranslationEngine implements InteractiveTranslationEngine {
3535
public readonly projectId: string,
3636
private readonly httpClient: HttpClient,
3737
private readonly noticeService: NoticeService,
38-
private readonly router: Router
38+
private readonly router: Router,
39+
private readonly i18n: I18nService
3940
) {}
4041

4142
async translate(segment: string): Promise<TranslationResult> {
@@ -97,14 +98,14 @@ export class RemoteTranslationEngine implements InteractiveTranslationEngine {
9798
} catch (err: any) {
9899
if (err.status === 403 || err.status === 404 || err.status === 409) {
99100
this.noticeService.showError(
100-
translate('error_messages.suggestion_engine_requires_retrain'),
101-
translate('error_messages.go_to_retrain'),
101+
this.i18n.translateStatic('error_messages.suggestion_engine_requires_retrain'),
102+
this.i18n.translateStatic('error_messages.go_to_retrain'),
102103
() => {
103104
this.router.navigate(['projects', this.projectId, 'translate']);
104105
}
105106
);
106107
} else {
107-
this.noticeService.showError(translate('error_messages.failed_to_retrieve_suggestions'));
108+
this.noticeService.showError(this.i18n.translateStatic('error_messages.failed_to_retrieve_suggestions'));
108109
}
109110
}
110111

src/SIL.XForge.Scripture/ClientApp/src/app/my-projects/my-projects.component.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
22
import { Component, DestroyRef, OnInit } from '@angular/core';
33
import { Router } from '@angular/router';
4-
import { translate } from '@ngneat/transloco';
54
import { isPTUser } from 'realtime-server/lib/esm/common/models/user';
65
import { isResource } from 'realtime-server/lib/esm/scriptureforge/models/sf-project';
76
import { Observable } from 'rxjs';
8-
import { en } from 'xforge-common/i18n.service';
7+
import { en, I18nService } from 'xforge-common/i18n.service';
98
import { UserDoc } from 'xforge-common/models/user-doc';
109
import { NoticeService } from 'xforge-common/notice.service';
1110
import { OnlineStatusService } from 'xforge-common/online-status.service';
@@ -53,6 +52,7 @@ export class MyProjectsComponent implements OnInit {
5352
private readonly permissions: PermissionsService,
5453
private readonly noticeService: NoticeService,
5554
private readonly router: Router,
55+
private readonly i18n: I18nService,
5656
private destroyRef: DestroyRef
5757
) {}
5858

@@ -109,11 +109,11 @@ export class MyProjectsComponent implements OnInit {
109109
const isTranslateAccessible = this.permissions.canAccessTranslate(sfProject);
110110
const isCheckingAccessible = this.permissions.canAccessCommunityChecking(sfProject) ?? false;
111111

112-
const drafting = isTranslateAccessible ? translate('my_projects.drafting') : '';
112+
const drafting = isTranslateAccessible ? this.i18n.translateStatic('my_projects.drafting') : '';
113113
const checking = isCheckingAccessible
114114
? isTranslateAccessible
115-
? ' • ' + translate('app.community_checking')
116-
: translate('app.community_checking')
115+
? ' • ' + this.i18n.translateStatic('app.community_checking')
116+
: this.i18n.translateStatic('app.community_checking')
117117
: '';
118118

119119
return `${drafting}${checking}`;
@@ -126,7 +126,7 @@ export class MyProjectsComponent implements OnInit {
126126
await this.projectService.onlineAddCurrentUser(projectId);
127127
this.router.navigate(['projects', projectId]);
128128
} catch {
129-
this.noticeService.show(translate('my_projects.failed_to_join_project'));
129+
this.noticeService.show(this.i18n.translateStatic('my_projects.failed_to_join_project'));
130130
} finally {
131131
this.noticeService.loadingFinished(this.constructor.name);
132132
this.joiningProjects.pop();
@@ -137,11 +137,11 @@ export class MyProjectsComponent implements OnInit {
137137
try {
138138
this.noticeService.loadingStarted(this.constructor.name);
139139
await this.projectService.onlineSyncUserRole(projectId);
140-
this.noticeService.show(translate('my_projects.user_role_updated'));
140+
this.noticeService.show(this.i18n.translateStatic('my_projects.user_role_updated'));
141141
const project = this.userParatextProjects.find(project => project.projectId === projectId);
142142
if (project != null) project.hasUserRoleChanged = false;
143143
} catch {
144-
this.noticeService.showError(translate('my_projects.failed_to_update_user_role'));
144+
this.noticeService.showError(this.i18n.translateStatic('my_projects.failed_to_update_user_role'));
145145
} finally {
146146
this.noticeService.loadingFinished(this.constructor.name);
147147
}

0 commit comments

Comments
 (0)