2017-04-15 11:07:31 -04:00
|
|
|
import Jed from 'jed';
|
2018-08-16 15:03:57 -04:00
|
|
|
import ensureSingleLine from './ensure_single_line';
|
2017-09-26 05:46:59 -04:00
|
|
|
import sprintf from './sprintf';
|
|
|
|
|
2017-12-11 07:25:49 -05:00
|
|
|
const languageCode = () => document.querySelector('html').getAttribute('lang') || 'en';
|
2017-10-04 07:23:52 -04:00
|
|
|
const locale = new Jed(window.translations || {});
|
2017-10-06 04:03:56 -04:00
|
|
|
delete window.translations;
|
2017-05-03 15:52:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Translates `text`
|
|
|
|
@param text The text to be translated
|
|
|
|
@returns {String} The translated text
|
2018-05-28 05:53:00 -04:00
|
|
|
*/
|
2018-08-16 15:03:57 -04:00
|
|
|
const gettext = text => locale.gettext.bind(locale)(ensureSingleLine(text));
|
2017-05-03 15:52:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Translate the text with a number
|
|
|
|
if the number is more than 1 it will use the `pluralText` translation.
|
|
|
|
This method allows for contexts, see below re. contexts
|
|
|
|
|
|
|
|
@param text Singular text to translate (eg. '%d day')
|
|
|
|
@param pluralText Plural text to translate (eg. '%d days')
|
|
|
|
@param count Number to decide which translation to use (eg. 2)
|
|
|
|
@returns {String} Translated text with the number replaced (eg. '2 days')
|
2018-05-28 05:53:00 -04:00
|
|
|
*/
|
2017-05-03 15:52:18 -04:00
|
|
|
const ngettext = (text, pluralText, count) => {
|
2018-08-16 15:03:57 -04:00
|
|
|
const translated = locale
|
|
|
|
.ngettext(ensureSingleLine(text), ensureSingleLine(pluralText), count)
|
|
|
|
.replace(/%d/g, count)
|
|
|
|
.split('|');
|
2017-05-03 15:52:18 -04:00
|
|
|
|
|
|
|
return translated[translated.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
Translate context based text
|
|
|
|
Either pass in the context translation like `Context|Text to translate`
|
|
|
|
or allow for dynamic text by doing passing in the context first & then the text to translate
|
|
|
|
|
|
|
|
@param keyOrContext Can be either the key to translate including the context
|
|
|
|
(eg. 'Context|Text') or just the context for the translation
|
|
|
|
(eg. 'Context')
|
|
|
|
@param key Is the dynamic variable you want to be translated
|
|
|
|
@returns {String} Translated context based text
|
2018-05-28 05:53:00 -04:00
|
|
|
*/
|
2017-05-03 15:52:18 -04:00
|
|
|
const pgettext = (keyOrContext, key) => {
|
2018-08-16 15:03:57 -04:00
|
|
|
const normalizedKey = ensureSingleLine(key ? `${keyOrContext}|${key}` : keyOrContext);
|
2017-05-03 15:52:18 -04:00
|
|
|
const translated = gettext(normalizedKey).split('|');
|
|
|
|
|
|
|
|
return translated[translated.length - 1];
|
2017-04-28 05:49:28 -04:00
|
|
|
};
|
2017-04-15 11:07:31 -04:00
|
|
|
|
2017-12-11 07:25:49 -05:00
|
|
|
/**
|
|
|
|
Creates an instance of Intl.DateTimeFormat for the current locale.
|
|
|
|
|
|
|
|
@param formatOptions for available options, please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
|
|
|
|
@returns {Intl.DateTimeFormat}
|
|
|
|
*/
|
2018-08-16 15:03:57 -04:00
|
|
|
const createDateTimeFormat = formatOptions => Intl.DateTimeFormat(languageCode(), formatOptions);
|
2017-12-11 07:25:49 -05:00
|
|
|
|
|
|
|
export { languageCode };
|
2017-04-26 12:03:06 -04:00
|
|
|
export { gettext as __ };
|
|
|
|
export { ngettext as n__ };
|
|
|
|
export { pgettext as s__ };
|
2017-09-26 05:46:59 -04:00
|
|
|
export { sprintf };
|
2017-12-11 07:25:49 -05:00
|
|
|
export { createDateTimeFormat };
|
2017-04-26 12:03:06 -04:00
|
|
|
export default locale;
|