Skip to content

Commit f66ef66

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

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
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: 2 additions & 1 deletion
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>;
26-
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;
27+
export declare function trans<M extends Message<TranslationsType, LocaleType>, D extends DomainsOf<M>, P extends ParametersOf<M, D>, T>(...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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type NoParametersType,
55
setLocale,
66
setLocaleFallbacks,
7+
throwWhenNotFound,
78
trans,
89
} from '../src/translator';
910

@@ -387,6 +388,27 @@ describe('Translator', () => {
387388
);
388389
});
389390

391+
test('missing message should throw an error if `throwWhenNotFound` is true', () => {
392+
throwWhenNotFound(true);
393+
setLocale('fr');
394+
395+
const MESSAGE_IN_ANOTHER_DOMAIN: Message<{ security: { parameters: NoParametersType } }, 'en'> = {
396+
id: 'Invalid credentials.',
397+
translations: {
398+
messages: {
399+
en: 'Invalid credentials.',
400+
},
401+
},
402+
};
403+
404+
expect(() => {
405+
trans(MESSAGE_IN_ANOTHER_DOMAIN);
406+
}).toThrow(`No translation message with id "${MESSAGE_IN_ANOTHER_DOMAIN.id}" is found.`);
407+
408+
// Set the default behavior for the next tests
409+
throwWhenNotFound(false);
410+
});
411+
390412
test('message from intl domain should be prioritized over its non-intl equivalent', () => {
391413
const MESSAGE: Message<
392414
{ 'messages+intl-icu': { parameters: NoParametersType }; messages: { parameters: NoParametersType } },

src/Translator/doc/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ 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)`` from ``@symfony/ux-translator`` package.
107+
101108
Importing and using translations
102109
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103110

0 commit comments

Comments
 (0)