Skip to content

Commit 447b67b

Browse files
[Translator] add throwWhenNotFound method
1 parent 412da63 commit 447b67b

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-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 option to throw an exception when a translation is missing.
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 with id "${message.id}" is found.`);
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 with id "${message.id}" is found.`);
172+
}
173+
165174
return message.id;
166175
}

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

Lines changed: 20 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,24 @@ describe('Translator', () => {
387389
);
388390
});
389391

392+
test('missing message should throw an error if `throwWhenNotFound` is true', () => {
393+
throwWhenNotFound(true);
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(() => {
406+
trans(MESSAGE_IN_ANOTHER_DOMAIN);
407+
}).toThrow(`No translation message with id "Invalid credentials." is found.`);
408+
});
409+
390410
test('message from intl domain should be prioritized over its non-intl equivalent', () => {
391411
const MESSAGE: Message<
392412
{ 'messages+intl-icu': { parameters: NoParametersType }; messages: { parameters: NoParametersType } },

src/Translator/doc/index.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ 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 on the front-end
102+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
104+
By default, the translator will return the translation key as is when the translation is missing.
105+
106+
You can configure this behavior to throw an error instead by calling ``throwWhenNotFound(true)``:
107+
108+
.. code-block:: diff
109+
110+
// assets/translator.js
111+
112+
/*
113+
* This file is part of the Symfony UX Translator package.
114+
*
115+
* If folder "../var/translations" does not exist, or some translations are missing,
116+
* you must warmup your Symfony cache to refresh JavaScript translations.
117+
*
118+
* If you use TypeScript, you can rename this file to "translator.ts" to take advantage of types checking.
119+
*/
120+
121+
-import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-translator';
122+
+import { trans, getLocale, setLocale, setLocaleFallbacks, throwWhenNotFound } from '@symfony/ux-translator';
123+
import { localeFallbacks } from '../var/translations/configuration';
124+
125+
setLocaleFallbacks(localeFallbacks);
126+
+throwWhenNotFound(true)
127+
128+
export { trans }
129+
export * from '../var/translations';
130+
101131
Importing and using translations
102132
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103133

0 commit comments

Comments
 (0)