Skip to content

Commit bdc49bc

Browse files
authored
Autofill editor from composed Machinery matches (#4417)
Expand autofilling of untranslated string editor with a perfect TM match to composed (multi-field) Machinery suggestions. Composed Machinery suggestions are perfect matches only when every one of its fields comes from TM. Show the match indicator in the source view too
1 parent b2240fd commit bdc49bc

4 files changed

Lines changed: 145 additions & 41 deletions

File tree

‎translate/src/context/Editor.test.jsx‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ function mountSpy(
111111
fetching: false,
112112
source: '',
113113
composed: [],
114-
translations: machinery ?? [],
114+
translations: [],
115+
...(Array.isArray(machinery) ? { translations: machinery } : machinery),
115116
};
116117

117118
const Wrapper = () => (
@@ -866,4 +867,92 @@ describe('<EditorProvider>', () => {
866867
act(() => actions.setResultFromInput());
867868
expect(unsaved.check()).toBe(true);
868869
});
870+
871+
it('reports pending changes for a manually copied translation', () => {
872+
let editor, actions, unsaved;
873+
const Spy = () => {
874+
editor = useContext(EditorData);
875+
actions = useContext(EditorActions);
876+
unsaved = useContext(UnsavedChanges);
877+
return null;
878+
};
879+
mountSpy(Spy, 'simple', undefined, 'message');
880+
881+
act(() =>
882+
actions.setEditorFromHelpers(
883+
'messaggio_it',
884+
['translation-memory'],
885+
true,
886+
),
887+
);
888+
889+
expect(editor.autofilled).toBeNull();
890+
act(() => actions.setResultFromInput());
891+
expect(unsaved.check()).toBe(true);
892+
});
893+
894+
it('autofills a multi-field entry from a composed 100% match', () => {
895+
let editor, actions, unsaved;
896+
const Spy = () => {
897+
editor = useContext(EditorData);
898+
actions = useContext(EditorActions);
899+
unsaved = useContext(UnsavedChanges);
900+
return null;
901+
};
902+
mountSpy(
903+
Spy,
904+
'fluent',
905+
undefined,
906+
ftl`
907+
key = Value
908+
.label = Label
909+
`,
910+
{
911+
composed: [
912+
{
913+
sources: ['translation-memory'],
914+
quality: 100,
915+
value: ['Valeur'],
916+
properties: { label: ['Étiquette'] },
917+
},
918+
],
919+
},
920+
);
921+
922+
expect(editor.fields.map((f) => f.handle.current.value)).toEqual([
923+
'Valeur',
924+
'Étiquette',
925+
]);
926+
927+
act(() => actions.setResultFromInput());
928+
expect(unsaved.check()).toBe(false);
929+
});
930+
931+
it('does not autofill a multi-field entry without a perfect composed match', () => {
932+
let editor;
933+
const Spy = () => {
934+
editor = useContext(EditorData);
935+
return null;
936+
};
937+
mountSpy(
938+
Spy,
939+
'fluent',
940+
undefined,
941+
ftl`
942+
key = Value
943+
.label = Label
944+
`,
945+
{
946+
composed: [
947+
{
948+
sources: ['google-translate'],
949+
value: ['Valeur'],
950+
properties: { label: ['Étiquette'] },
951+
},
952+
],
953+
},
954+
);
955+
956+
expect(editor.fields.map((f) => f.handle.current.value)).toEqual(['', '']);
957+
});
869958
});

‎translate/src/context/Editor.tsx‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,14 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
418418
const status = useTranslationStatus(entity);
419419
useEffect(() => {
420420
if (
421-
status === 'missing' &&
422-
state.machinery === null &&
423-
!state.sourceView &&
424-
state.fields.length === 1 &&
425-
state.fields[0].handle.current.value === ''
421+
status !== 'missing' ||
422+
state.machinery !== null ||
423+
state.sourceView ||
424+
state.fields.some((field) => field.handle.current.value !== '')
426425
) {
426+
return;
427+
}
428+
if (state.fields.length === 1) {
427429
const perfect = machinery.translations.find((tx) => tx.quality === 100);
428430
if (perfect) {
429431
actions.setEditorFromHelpers(
@@ -432,8 +434,18 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
432434
false,
433435
);
434436
}
437+
} else if (state.fields.length > 1) {
438+
const perfect = machinery.composed.find((tx) => tx.quality === 100);
439+
if (perfect) {
440+
actions.setEditorFromComposed(
441+
perfect.value,
442+
perfect.properties,
443+
perfect.sources,
444+
false,
445+
);
446+
}
435447
}
436-
}, [state, actions, status, machinery.translations]);
448+
}, [state, actions, status, machinery.translations, machinery.composed]);
437449

438450
useEffect(() => {
439451
// Changes in `result` need to be reflected in `UnsavedChanges`,

‎translate/src/modules/editor/components/MachinerySourceIndicator.test.jsx‎

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,69 @@ import React from 'react';
22
import { render } from '@testing-library/react';
33
import { describe, expect, it } from 'vitest';
44

5-
import { EditorData } from '~/context/Editor';
5+
import { EditorData, EditorResult } from '~/context/Editor';
66
import { MockLocalizationProvider } from '~/test/utils';
77

88
import { MachinerySourceIndicator } from './MachinerySourceIndicator';
99

10-
const field = (value) => ({
11-
id: '',
12-
name: '',
13-
keys: [],
14-
labels: [],
15-
handle: { current: { value } },
16-
});
10+
const entry = (value) => ({ format: 'plain', id: 'key', value: [value] });
1711

18-
const mount = (editor) =>
12+
const mount = ({
13+
autofilled = entry('Bonjour'),
14+
result = entry('Bonjour'),
15+
sourceView = false,
16+
}) =>
1917
render(
2018
<MockLocalizationProvider
2119
resources={[
2220
`editor-MachinerySourceIndicator--match = <stress>100%</stress> MATCH
2321
.title = 100% Translation Memory match`,
2422
]}
2523
>
26-
<EditorData.Provider
27-
value={{
28-
fields: [field('Bonjour')],
29-
machinery: { manual: false, sources: [], translation: 'Bonjour' },
30-
sourceView: false,
31-
...editor,
32-
}}
33-
>
34-
<MachinerySourceIndicator />
24+
<EditorData.Provider value={{ autofilled, sourceView }}>
25+
<EditorResult.Provider value={result}>
26+
<MachinerySourceIndicator />
27+
</EditorResult.Provider>
3528
</EditorData.Provider>
3629
</MockLocalizationProvider>,
3730
);
3831

3932
describe('<MachinerySourceIndicator>', () => {
4033
it('shows for content that was filled in automatically', () => {
41-
const { container } = mount();
34+
const { container } = mount({});
4235

4336
const indicator = container.querySelector('.tm-source');
4437
expect(indicator).not.toBeNull();
4538
expect(indicator.textContent).toContain('100%');
4639
expect(indicator.getAttribute('title')).toBeTruthy();
4740
});
4841

49-
it('shows nothing for a manual copy', () => {
50-
const { container } = mount({
51-
machinery: { manual: true, sources: [], translation: 'Bonjour' },
52-
});
42+
it('shows for a multi-field entry filled in from a composed match', () => {
43+
const composed = {
44+
format: 'fluent',
45+
id: 'key',
46+
value: ['Valeur'],
47+
attributes: new Map([['label', ['Étiquette']]]),
48+
};
49+
const { container } = mount({ autofilled: composed, result: composed });
50+
51+
expect(container.querySelector('.tm-source')).not.toBeNull();
52+
});
53+
54+
it('shows in the source view as well', () => {
55+
const { container } = mount({ sourceView: true });
56+
57+
expect(container.querySelector('.tm-source')).not.toBeNull();
58+
});
59+
60+
it('shows nothing when nothing was filled in', () => {
61+
const { container } = mount({ autofilled: null });
5362

5463
expect(container.querySelector('.tm-source')).toBeNull();
5564
});
5665

5766
it('shows nothing once the filled-in content has been edited', () => {
58-
const { container } = mount({ fields: [field('Bonjour !')] });
67+
const { container } = mount({ result: entry('Bonjour !') });
5968

6069
expect(container.querySelector('.tm-source')).toBeNull();
6170
});

‎translate/src/modules/editor/components/MachinerySourceIndicator.tsx‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ import { Localized } from '@fluent/react';
22
import React, { useContext } from 'react';
33

44
import { EditorData, EditorResult } from '~/context/Editor';
5+
import { pojoEquals } from '~/utils/pojo';
56

67
import './MachinerySourceIndicator.css';
78

89
export function MachinerySourceIndicator() {
9-
const { fields, machinery, sourceView } = useContext(EditorData);
10-
// Included to re-render on input changes
11-
useContext(EditorResult);
10+
const { autofilled } = useContext(EditorData);
11+
const result = useContext(EditorResult);
1212

13-
if (
14-
!machinery ||
15-
machinery.manual ||
16-
sourceView ||
17-
fields.length !== 1 ||
18-
machinery.translation !== fields[0].handle.current.value
19-
) {
13+
if (!autofilled || !pojoEquals(autofilled, result)) {
2014
return null;
2115
}
2216

0 commit comments

Comments
 (0)