Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SF-3162 Warn user when training and drafting sources are different #2982

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -49,4 +49,10 @@ <h2>{{ t("drafting_from_heading", { draftingSourceShortNames, draftingSourceLang
<p>{{ t("how_to_change_language_codes") }}</p>
<mat-checkbox (change)="confirmationChanged($event)">{{ t("confirm_lang_codes_correct") }}</mat-checkbox>
</app-notice>

@if (showSourceLanguagesNotCompatibleError) {
<app-notice type="error">
<span>{{ t("sources_must_be_same_language") }}</span>
</app-notice>
}
</ng-container>
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ h2 {
}

app-notice {
margin-top: 4em;
margin-top: 2em;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,32 @@ export class ConfirmSourcesComponent implements OnInit {
trainingSources: TranslateSource[] = [];
trainingTargets: TranslateSource[] = [];
draftingSources: TranslateSource[] = [];
languageCodesCompatible: boolean = true;

protected showSourceLanguagesNotCompatibleError = false;

constructor(
private readonly destroyRef: DestroyRef,
private readonly i18nService: I18nService,
private readonly draftSourcesService: DraftSourcesService
) {}

get referenceLanguage(): string {
return this.displayNameForProjectsLanguages(this.trainingSources);
}

get targetLanguage(): string {
return this.displayNameForProjectsLanguages(this.trainingTargets);
}

get draftingSourceLanguage(): string {
return this.displayNameForProjectsLanguages(this.draftingSources);
}

get draftingSourceShortNames(): string {
return this.i18nService.enumerateList(this.draftingSources.filter(p => p != null).map(p => p.shortName));
}

ngOnInit(): void {
this.draftSourcesService
.getDraftProjectSources()
Expand All @@ -37,6 +56,17 @@ export class ConfirmSourcesComponent implements OnInit {
this.trainingSources = trainingSources.filter(s => s !== undefined);
this.trainingTargets = trainingTargets.filter(t => t !== undefined);
this.draftingSources = draftingSources.filter(s => s !== undefined);

// compare language codes
const trainingSourcesMatch =
this.trainingSources.length <= 1 ||
this.sourceLanguagesAreCompatible(this.trainingSources[0], this.trainingSources[1]);
const trainingDraftingSourcesMatch = this.sourceLanguagesAreCompatible(
this.trainingSources[0],
this.draftingSources[0]
);
this.languageCodesCompatible = trainingSourcesMatch && trainingDraftingSourcesMatch;
this.showSourceLanguagesNotCompatibleError = !this.languageCodesCompatible;
});
}

Expand All @@ -50,19 +80,11 @@ export class ConfirmSourcesComponent implements OnInit {
return this.i18nService.enumerateList(displayNames);
}

get referenceLanguage(): string {
return this.displayNameForProjectsLanguages(this.trainingSources);
}

get targetLanguage(): string {
return this.displayNameForProjectsLanguages(this.trainingTargets);
}

get draftingSourceLanguage(): string {
return this.displayNameForProjectsLanguages(this.draftingSources);
}

get draftingSourceShortNames(): string {
return this.i18nService.enumerateList(this.draftingSources.filter(p => p != null).map(p => p.shortName));
/**
* Determines of the source languages for the project are compatible for generating a draft.
*/
private sourceLanguagesAreCompatible(source: TranslateSource, other: TranslateSource): boolean {
if (source.writingSystem.tag === other.writingSystem.tag) return true;
return this.i18nService.languageCodesEquivalent(source.writingSystem.tag, other.writingSystem.tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<!-- Icon overrides -->
<ng-template matStepperIcon="edit" let-index="index">{{ index + 1 }}</ng-template>

<mat-step [completed]="languagesVerified">
<mat-step [completed]="languagesVerified && languagesAreCompatible">
<ng-template matStepLabel>{{ t("overview") }}</ng-template>
<app-confirm-sources (languageCodesVerified)="languagesVerified = $event"></app-confirm-sources>
@if (nextClickedOnLanguageVerification && !languagesVerified) {
<app-notice type="error" mode="fill-dark">{{ t("confirm_codes_correct_to_continue") }}</app-notice>
<app-notice type="error">{{ t("confirm_codes_correct_to_continue") }}</app-notice>
}
<div class="button-strip">
<button mat-stroked-button class="backout-button" (click)="cancel.emit()">{{ t("back") }}</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,79 @@ describe('DraftGenerationStepsComponent', () => {
when(mockOnlineStatusService.isOnline).thenReturn(true);
}));

describe('training and drafting sources different', async () => {
const availableBooks = [{ bookNum: 1 }, { bookNum: 2 }, { bookNum: 3 }];
const config = {
trainingSources: [
{
projectRef: 'source1',
paratextId: 'PT_SP',
name: 'Source Project',
shortName: 'sP1',
writingSystem: { tag: 'eng' },
texts: availableBooks
},
undefined
] as [DraftSource, DraftSource?],
trainingTargets: [
{
projectRef: mockActivatedProjectService.projectId,
shortName: 'tT',
writingSystem: { tag: 'xyz' },
texts: availableBooks
}
] as [DraftSource],
draftingSources: [
{
projectRef: 'source2',
paratextId: 'PT_SP2',
shortName: 'sP2',
writingSystem: { tag: 'es' },
texts: availableBooks
}
] as [DraftSource]
};

beforeEach(fakeAsync(() => {
when(mockDraftSourceService.getDraftProjectSources()).thenReturn(of(config));
const mockTargetProjectDoc = {
id: 'project01',
data: createTestProjectProfile({
texts: availableBooks,
translateConfig: {
source: { projectRef: 'sourceProject', shortName: 'sP', writingSystem: { tag: 'xyz' } }
},
writingSystem: { tag: 'eng' }
})
} as SFProjectProfileDoc;
const targetProjectDoc$ = new BehaviorSubject<SFProjectProfileDoc>(mockTargetProjectDoc);

when(mockActivatedProjectService.projectDoc).thenReturn(mockTargetProjectDoc);
when(mockActivatedProjectService.projectDoc$).thenReturn(targetProjectDoc$);
when(mockTrainingDataService.queryTrainingDataAsync(anything(), anything())).thenResolve(
instance(mockTrainingDataQuery)
);
when(mockTrainingDataQuery.docs).thenReturn([]);
when(mockFeatureFlagService.allowFastTraining).thenReturn(createTestFeatureFlag(false));

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

it('should not advance steps if drafting and training sources are different', fakeAsync(() => {
expect(component.stepper.selectedIndex).toBe(0);
clickConfirmLanguages(fixture);
fixture.detectChanges();
component.tryAdvanceStep();
tick();
fixture.detectChanges();
expect(component.stepper.selectedIndex).toBe(0);
}));
});

describe('one training source', async () => {
const availableBooks = [{ bookNum: 1 }, { bookNum: 2 }, { bookNum: 3 }];
const allBooks = [...availableBooks, { bookNum: 6 }, { bookNum: 7 }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class DraftGenerationStepsComponent implements OnInit {
@Output() readonly done = new EventEmitter<DraftGenerationStepsResult>();
@Output() readonly cancel = new EventEmitter();
@ViewChild(MatStepper) stepper!: MatStepper;
@ViewChild(ConfirmSourcesComponent) confirmSourcesComponent?: ConfirmSourcesComponent;

availableTranslateBooks: Book[] = [];
availableTrainingBooks: { [projectRef: string]: Book[] } = {}; //books in both source and target
Expand Down Expand Up @@ -255,6 +256,10 @@ export class DraftGenerationStepsComponent implements OnInit {
);
}

get languagesAreCompatible(): boolean {
return !!this.confirmSourcesComponent?.languageCodesCompatible;
}

get trainingSourceBooksSelected(): boolean {
for (const source of this.trainingSources) {
if (this.availableTrainingBooks[source.projectRef]?.filter(b => b.selected)?.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"language_code": "Language code: ",
"references_heading": "References ({{ referenceLanguage }})",
"review_draft_setup": "Review draft setup",
"sources_must_be_same_language": "The training source must be the same language as the drafting source.",
"targets_heading": "Translation project ({{ targetLanguage }})",
"training_language_model": "Training the language model"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,40 @@ describe('I18nService', () => {
});
});

describe('areLanguagesCompatible', () => {
it('should return true for identical codes', () => {
const service = getI18nService();
service.setLocale('en');
expect(service.languageCodesEquivalent('fr', 'fr')).toBe(true);
});

it('should return true when codes have different regions', () => {
const service = getI18nService();
service.setLocale('en');
expect(service.languageCodesEquivalent('es', 'es-SP-latn')).toBe(true);
expect(service.languageCodesEquivalent('zh-CN', 'zh-Hant')).toBe(true);
});

it('should return true if codes have the same name', () => {
const service = getI18nService();
service.setLocale('en');
expect(service.languageCodesEquivalent('npi', 'nep')).toBe(true);
expect(service.languageCodesEquivalent('zho', 'chi')).toBe(true);
});

it('should return false for different codes', () => {
const service = getI18nService();
service.setLocale('en');
expect(service.languageCodesEquivalent('ar', 'az')).toBe(false);
});

it('should return false if codes are not formatted correctly', () => {
const service = getI18nService();
service.setLocale('en');
expect(service.languageCodesEquivalent('a-r', 'a-r')).toBe(false);
});
});

describe('getPluralRule', () => {
it('should return rule for zero, one and other', () => {
const service = getI18nService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type I18nKeyForComponent<T extends keyof typeof en> = ObjectPaths<(typeof
// this mismatch is left to be solved at another time.

export const IGNORE_COOKIE_LOCALE = new InjectionToken<boolean>('IGNORE_COOKIE_LOCALE');
const LANGUAGE_CODE_REGEX = /(^[a-zA-Z]{2,3})-*/;

@Injectable()
export class TranslationLoader implements TranslocoLoader {
Expand Down Expand Up @@ -412,6 +413,18 @@ export class I18nService {
}
}

languageCodesEquivalent(languageCode: string, otherLanguageCode: string): boolean {
const languageCodeMatch = LANGUAGE_CODE_REGEX.exec(languageCode);
const otherLanguageCodeMatch = LANGUAGE_CODE_REGEX.exec(otherLanguageCode);
if (languageCodeMatch == null || otherLanguageCodeMatch == null) return false;
if (languageCodeMatch[1] === otherLanguageCodeMatch[1]) return true;

return (
new Intl.DisplayNames(['en'], { type: 'language' }).of(languageCodeMatch[1]) ===
new Intl.DisplayNames(['en'], { type: 'language' }).of(otherLanguageCodeMatch[1])
);
}

static getHumanReadableTimeZoneOffset(localeCode: string, date: Date): string {
return new Intl.DateTimeFormat(localeCode, { timeZoneName: 'short' })
.formatToParts(date)
Expand Down
Loading