ember-intl
Version:
Internationalization for Ember projects
79 lines (73 loc) • 1.8 kB
JavaScript
import { getContext, settled } from '@ember/test-helpers';
/**
* Updates the translations as if you had somehow added them (e.g.
* via lazy loading).
*
* @function addTranslations
* @param {string} locale
* @param {object} translations
*/
async function addTranslations(locale, translations) {
const {
owner
} = getContext();
const intl = owner.lookup('service:intl');
intl.addTranslations(locale, translations);
await settled();
}
/**
* Updates the locale as if the user had changed their preferred language.
*
* @function setLocale
* @param {string} locale
*/
async function setLocale(locale) {
const {
owner
} = getContext();
const intl = owner.lookup('service:intl');
intl.setLocale(locale);
await settled();
}
/**
* In addition to the `hooks` object, you must specify the locale
* under which your tests make sense.
*
* You may pass a `translations` object to stub the translations.
*
* @param {object} hooks
* @param {string} locale
* @param {object} [translations]
*/
function setupIntl(hooks, locale, translations) {
hooks.beforeEach(async function () {
const {
owner
} = getContext();
const intl = owner.lookup('service:intl');
intl.setLocale(locale);
intl.setOnMissingTranslation(key => `t:${key}`);
if (translations) {
await addTranslations(locale, translations);
} else {
await settled();
}
});
}
/**
* Invokes the `t` method of the `intl` service.
*
* @function t
* @param {string} key
* @param {object} [options]
* @return {string}
*/
function t(key, options) {
const {
owner
} = getContext();
const intl = owner.lookup('service:intl');
return intl.t(key, options);
}
export { addTranslations, setLocale, setupIntl, t };
//# sourceMappingURL=test-support.js.map