Skip to content
Merged
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
214 changes: 141 additions & 73 deletions translate/src/context/Editor.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import { fluentParseEntry, mf2ParseMessage } from '@mozilla/l10n';
function mountSpy(
Spy,
format,
formatTranslation,
formatSource = 'key = test',
machinery = null,
locale = { code: 'sl', cldrPlurals: [1, 2, 3, 5] },
targetStr,
sourceStr = 'key = test',
{
entity = null,
locale = { code: 'sl', cldrPlurals: [1, 2, 3, 5] },
machinery = null,
} = {},
) {
const history = createMemoryHistory({
initialEntries: [`/sl/pro/all/?string=42`],
Expand All @@ -37,68 +40,53 @@ function mountSpy(
let translation = undefined;
switch (format) {
case 'fluent': {
const [id, entry] = fluentParseEntry(formatSource);
const [id, entry] = fluentParseEntry(sourceStr);
key = [id];
value = entry['='];
properties = entry['+'];
if (formatTranslation) {
const [, entry] = fluentParseEntry(formatTranslation);
if (targetStr) {
const [, entry] = fluentParseEntry(targetStr);
translation = {
string: formatTranslation,
string: targetStr,
value: entry['='],
properties: entry['+'],
};
}
break;
}
case 'android':
value = mf2ParseMessage(formatSource);
if (formatTranslation) {
value = mf2ParseMessage(sourceStr);
if (targetStr) {
translation = {
string: formatTranslation,
value: mf2ParseMessage(formatTranslation),
string: targetStr,
value: mf2ParseMessage(targetStr),
};
}
break;
default:
value = [formatSource];
if (formatTranslation) {
translation = { string: formatTranslation, value: [formatTranslation] };
value = [sourceStr];
if (targetStr) {
translation = { string: targetStr, value: [targetStr] };
}
}

const gettextSource =
'.input {$n :number}\n.match $n\none {{orig one}}\n* {{orig other}}';
const gettextTranslation =
'.input {$n :number}\n.match $n\none {{trans one}}\n* {{trans other}}';

const initialState = {
entities: {
entities: [
{
pk: 42,
format,
key,
original: formatSource,
value,
properties,
translation,
project: { contact: '' },
},
{
pk: 13,
format: 'gettext',
key: ['plural'],
original: gettextSource,
value: mf2ParseMessage(gettextSource),
translation: {
string: gettextTranslation,
value: mf2ParseMessage(gettextTranslation),
},
project: { contact: '' },
},
],
const entities = [
{
pk: 42,
format,
key,
original: sourceStr,
value,
properties,
translation,
project: { contact: '' },
},
];
if (entity) {
entities.push(entity);
}
const initialState = {
entities: { entities },
otherlocales: { translations: [] },
user: {
isAuthenticated: true,
Expand Down Expand Up @@ -472,7 +460,26 @@ describe('<EditorProvider>', () => {
entity = useContext(EntityView).entity;
return null;
};
mountSpy(Spy, 'plain', 'translated');

const gettextSource =
'.input {$n :number}\n.match $n\none {{orig one}}\n* {{orig other}}';
const gettextTranslation =
'.input {$n :number}\n.match $n\none {{trans one}}\n* {{trans other}}';

mountSpy(Spy, 'plain', 'translated', 'source', {
entity: {
pk: 13,
format: 'gettext',
key: ['plural'],
original: gettextSource,
value: mf2ParseMessage(gettextSource),
translation: {
string: gettextTranslation,
value: mf2ParseMessage(gettextTranslation),
},
project: { contact: '' },
},
});

act(() => location.push({ entity: 13 }));

Expand Down Expand Up @@ -500,6 +507,62 @@ describe('<EditorProvider>', () => {
});
});

it('allows editing an approved webext translation with no placeholder', () => {
let actions, editor, result, location;
const Spy = () => {
actions = useContext(EditorActions);
editor = useContext(EditorData);
result = useContext(EditorResult);
location = useContext(Location);
return null;
};

const sourceStr =
'.local $ph = {$arg1 @source=|$1|}\n' +
'{{source with {$ph @source=|$ph$|}}}';
const targetStr = 'target with $ph$';

mountSpy(Spy, 'plain', 'translated', 'source', {
entity: {
pk: 13,
format: 'webext',
key: ['foo'],
original: sourceStr,
value: mf2ParseMessage(sourceStr),
translation: { string: targetStr, value: mf2ParseMessage(targetStr) },
project: { contact: '' },
},
});

act(() => location.push({ entity: 13 }));

const targetWithDecl = {
format: 'webext',
id: 'foo',
value: {
decl: { ph: { $: 'arg1', attr: { source: '$1' } } },
msg: ['target with $ph$'],
},
};

expect(editor).toMatchObject({
initial: targetWithDecl,
fields: [{ handle: { current: { value: 'target with $ph$' } } }],
});
expect(result).toEqual(targetWithDecl);

// String value is re-parsed on input
act(() => actions.setResultFromInput());
expect(result).toEqual({
format: 'webext',
id: 'foo',
value: {
decl: { ph: { $: 'arg1', attr: { source: '$1' } } },
msg: ['target with ', { $: 'ph', attr: { source: '$ph$' } }],
},
});
});

it('clears a rich Fluent value', () => {
let editor, actions;
const Spy = () => {
Expand Down Expand Up @@ -589,9 +652,8 @@ describe('<EditorProvider>', () => {
*[other] OTHER
}
`;
mountSpy(Spy, 'fluent', undefined, source, null, {
code: 'uk',
cldrPlurals: [1, 3, 4],
mountSpy(Spy, 'fluent', undefined, source, {
locale: { code: 'uk', cldrPlurals: [1, 3, 4] },
});

expect(editor.fields.map((f) => f.id)).toEqual(['|one', '|few', '|*']);
Expand Down Expand Up @@ -892,14 +954,16 @@ describe('<EditorProvider>', () => {
unsaved = useContext(UnsavedChanges);
return null;
};
mountSpy(Spy, 'simple', undefined, 'message', [
{
original: 'message',
translation: 'messaggio_it',
quality: 100,
sources: ['translation-memory'],
},
]);
mountSpy(Spy, 'simple', undefined, 'message', {
machinery: [
{
original: 'message',
translation: 'messaggio_it',
quality: 100,
sources: ['translation-memory'],
},
],
});

expect(editor).toMatchObject({
machinery: { manual: false, translation: 'messaggio_it' },
Expand Down Expand Up @@ -954,14 +1018,16 @@ describe('<EditorProvider>', () => {
.label = Label
`,
{
composed: [
{
sources: ['translation-memory'],
quality: 100,
value: ['Valeur'],
properties: { label: ['Étiquette'] },
},
],
machinery: {
composed: [
{
sources: ['translation-memory'],
quality: 100,
value: ['Valeur'],
properties: { label: ['Étiquette'] },
},
],
},
},
);

Expand Down Expand Up @@ -989,13 +1055,15 @@ describe('<EditorProvider>', () => {
.label = Label
`,
{
composed: [
{
sources: ['google-translate'],
value: ['Valeur'],
properties: { label: ['Étiquette'] },
},
],
machinery: {
composed: [
{
sources: ['google-translate'],
value: ['Valeur'],
properties: { label: ['Étiquette'] },
},
],
},
},
);

Expand Down
41 changes: 39 additions & 2 deletions translate/src/context/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CatchallKey, type Message } from '@mozilla/l10n';
import type { CatchallKey, Message } from '@mozilla/l10n';
import React, {
createContext,
useContext,
Expand All @@ -21,12 +21,12 @@ import {
requiresSourceView,
serializeEntry,
} from '~/utils/message';
import { createMessageEntry } from '~/utils/message/createMessageEntry';
import {
hasOuterWhitespace,
htmlElementEscapes,
} from '~/utils/message/entryInformation';
import { messageEntryFromEntityTranslation } from '~/utils/message/fromEntity';
import { createMessageEntry } from '~/utils/message/createMessageEntry';
import { getMessageEntryFormat } from '~/utils/message/getMessageEntryFormat';
import { specialFormats } from '~/utils/message/specialFormats';
import { pojoEquals } from '~/utils/pojo';
Expand Down Expand Up @@ -188,6 +188,40 @@ function parseEntryFromFluentSource(
return entry;
}

/**
* Modifies `base` in-place, ensuring that it contains
* the attributes and declarations present on `sourceEntry`.
*/
function includeSourceAttributesAndDeclarations(
base: MessageEntry,
sourceEntry: MessageEntry,
) {
const apply = (source: Message, target: Message): Message => {
if (Array.isArray(source)) {
return target;
}
if (Array.isArray(target)) {
return { decl: structuredClone(source.decl), msg: target };
}
for (const [name, expression] of Object.entries(source.decl)) {
target.decl[name] ??= structuredClone(expression);
}
return target;
};

if (sourceEntry.value) {
base.value = apply(sourceEntry.value, base.value ?? []);
}

if (sourceEntry.attributes) {
base.attributes ??= new Map();
for (const [name, message] of sourceEntry.attributes) {
const prev = base.attributes.get(name) ?? [];
base.attributes.set(name, apply(message, prev));
}
}
}

const initEditorData: EditorData = {
pk: 0,
busy: false,
Expand Down Expand Up @@ -324,6 +358,7 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
if (specialFormats.has(format)) {
const entry = parseEntry(format, str);
if (entry) {
includeSourceAttributesAndDeclarations(entry, sourceEntry);
next.base = entry;
} else if (format !== 'fluent') {
return prev;
Expand Down Expand Up @@ -369,6 +404,7 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {
if (sourceView) {
const entry = parseEntryFromFluentSource(sourceEntry, fields);
if (entry && !requiresSourceView(entry)) {
includeSourceAttributesAndDeclarations(entry, sourceEntry);
const fields = editMessageEntry(sourceEntry, entry);
state.focusField.current = fields[0];
setResult(entry);
Expand All @@ -391,6 +427,7 @@ export function EditorProvider({ children }: { children: React.ReactElement }) {

useEffect(() => {
const base = messageEntryFromEntityTranslation(entity, locale);
includeSourceAttributesAndDeclarations(base, sourceEntry);
const sourceView = requiresSourceView(base);
const fields = sourceView
? editSource(serializeEntry(base))
Expand Down
Loading