UNPKG

1.31 kBJavaScriptView Raw
1import addTranslations from './add-translations';
2import { missingMessage } from './-private/serialize-translation';
3
4/**
5 * Calling this helper will install a special `missing-message` util that will
6 * serialize all missing translations in a deterministic manner, including
7 * variables you've passed for interpolation. This means that you do not have
8 * to explicitly add any translations and can just rely on the implicit
9 * serialization. See the docs for detailed examples.
10 *
11 * Besides the `hooks` object you can also pass a `locale` string or array to
12 * set the locale, as well as an object of `translations`, if you do want to
13 * bootstrap translations. Both arguments are optional.
14 *
15 * @param {object} hooks
16 * @param {string} [locale]
17 * @param {object} [translations]
18 */
19export default function setupIntl(hooks, locale, translations) {
20 if (typeof locale === 'object' && !Array.isArray(locale)) {
21 translations = locale;
22 locale = null;
23 }
24
25 hooks.beforeEach(function() {
26 this.owner.register('util:intl/missing-message', missingMessage);
27 this.intl = this.owner.lookup('service:intl');
28 });
29
30 if (locale) {
31 hooks.beforeEach(function() {
32 this.intl.setLocale(locale);
33 });
34 }
35
36 if (translations) {
37 hooks.beforeEach(() => addTranslations(translations));
38 }
39}