Skip to content

Commit 33458cc

Browse files
[Translator] add throwWhenNotFound method
1 parent 412da63 commit 33458cc

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/Translator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.20.0
4+
5+
- Add `throwWhenNotFound` function to configure the behavior when a translation is not found.
6+
37
## 2.19.0
48

59
- Add configuration to filter dumped translations by domain.

src/Translator/assets/dist/translator.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface Message<Translations extends TranslationsType, Locale extends L
2121
}
2222
export declare function setLocale(locale: LocaleType | null): void;
2323
export declare function getLocale(): LocaleType;
24+
export declare function throwWhenNotFound(enabled: boolean): void;
2425
export declare function setLocaleFallbacks(localeFallbacks: Record<LocaleType, LocaleType>): void;
2526
export declare function getLocaleFallbacks(): Record<LocaleType, LocaleType>;
2627
export declare function trans<M extends Message<TranslationsType, LocaleType>, D extends DomainsOf<M>, P extends ParametersOf<M, D>>(...args: P extends NoParametersType ? [message: M, parameters?: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>] : [message: M, parameters: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>]): string;

src/Translator/assets/dist/translator_controller.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ function getPluralizationRule(number, locale) {
220220

221221
let _locale = null;
222222
let _localeFallbacks = {};
223+
let _throwWhenNotFound = false;
223224
function setLocale(locale) {
224225
_locale = locale;
225226
}
@@ -229,6 +230,9 @@ function getLocale() {
229230
document.documentElement.lang ||
230231
'en');
231232
}
233+
function throwWhenNotFound(enabled) {
234+
_throwWhenNotFound = enabled;
235+
}
232236
function setLocaleFallbacks(localeFallbacks) {
233237
_localeFallbacks = localeFallbacks;
234238
}
@@ -270,7 +274,10 @@ function trans(message, parameters = {}, domain = 'messages', locale = null) {
270274
return format(translations[locale], parameters, locale);
271275
}
272276
}
277+
if (_throwWhenNotFound) {
278+
throw new Error(`No translation message found with id "${message.id}".`);
279+
}
273280
return message.id;
274281
}
275282

276-
export { getLocale, getLocaleFallbacks, setLocale, setLocaleFallbacks, trans };
283+
export { getLocale, getLocaleFallbacks, setLocale, setLocaleFallbacks, throwWhenNotFound, trans };

src/Translator/assets/src/translator.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { format } from './formatters/formatter';
3838

3939
let _locale: LocaleType | null = null;
4040
let _localeFallbacks: Record<LocaleType, LocaleType> = {};
41+
let _throwWhenNotFound = false;
4142

4243
export function setLocale(locale: LocaleType | null) {
4344
_locale = locale;
@@ -52,6 +53,10 @@ export function getLocale(): LocaleType {
5253
);
5354
}
5455

56+
export function throwWhenNotFound(enabled: boolean): void {
57+
_throwWhenNotFound = enabled;
58+
}
59+
5560
export function setLocaleFallbacks(localeFallbacks: Record<LocaleType, LocaleType>): void {
5661
_localeFallbacks = localeFallbacks;
5762
}
@@ -162,5 +167,9 @@ export function trans<
162167
}
163168
}
164169

170+
if (_throwWhenNotFound) {
171+
throw new Error(`No translation message found with id "${message.id}".`);
172+
}
173+
165174
return message.id;
166175
}

src/Translator/assets/test/translator.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import {
44
type NoParametersType,
55
setLocale,
66
setLocaleFallbacks,
7+
throwWhenNotFound,
78
trans,
89
} from '../src/translator';
910

1011
describe('Translator', () => {
1112
beforeEach(() => {
1213
setLocale(null);
1314
setLocaleFallbacks({});
15+
throwWhenNotFound(false);
1416
document.documentElement.lang = '';
1517
document.documentElement.removeAttribute('data-symfony-ux-translator-locale');
1618
});
@@ -387,6 +389,40 @@ describe('Translator', () => {
387389
);
388390
});
389391

392+
test('missing message should return the message id when `throwWhenNotFound` is false', () => {
393+
throwWhenNotFound(false);
394+
setLocale('fr');
395+
396+
const MESSAGE_IN_ANOTHER_DOMAIN: Message<{ security: { parameters: NoParametersType } }, 'en'> = {
397+
id: 'Invalid credentials.',
398+
translations: {
399+
messages: {
400+
en: 'Invalid credentials.',
401+
},
402+
},
403+
};
404+
405+
expect(trans(MESSAGE_IN_ANOTHER_DOMAIN)).toEqual('Invalid credentials.');
406+
});
407+
408+
test('missing message should throw an error if `throwWhenNotFound` is true', () => {
409+
throwWhenNotFound(true);
410+
setLocale('fr');
411+
412+
const MESSAGE_IN_ANOTHER_DOMAIN: Message<{ security: { parameters: NoParametersType } }, 'en'> = {
413+
id: 'Invalid credentials.',
414+
translations: {
415+
messages: {
416+
en: 'Invalid credentials.',
417+
},
418+
},
419+
};
420+
421+
expect(() => {
422+
trans(MESSAGE_IN_ANOTHER_DOMAIN);
423+
}).toThrow(`No translation message found with id "Invalid credentials.".`);
424+
});
425+
390426
test('message from intl domain should be prioritized over its non-intl equivalent', () => {
391427
const MESSAGE: Message<
392428
{ 'messages+intl-icu': { parameters: NoParametersType }; messages: { parameters: NoParametersType } },

src/Translator/doc/index.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ By default, the default locale is ``en`` (English) that you can configure throug
9898
#. Or with ``<html data-symfony-ux-translator-locale="your-locale">`` attribute
9999
#. Or with ``<html lang="your-locale">`` attribute
100100

101+
Detecting missing translations
102+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
104+
By default, the translator will return the translation key if the translation is missing.
105+
106+
You can change this behavior by calling ``throwWhenNotFound(true)``:
107+
108+
.. code-block:: diff
109+
110+
// assets/translator.js
111+
112+
- import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-translator';
113+
+ import { trans, getLocale, setLocale, setLocaleFallbacks, throwWhenNotFound } from '@symfony/ux-translator';
114+
import { localeFallbacks } from '../var/translations/configuration';
115+
116+
setLocaleFallbacks(localeFallbacks);
117+
+ throwWhenNotFound(true)
118+
119+
export { trans }
120+
export * from '../var/translations';
121+
101122
Importing and using translations
102123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103124

0 commit comments

Comments
 (0)