cncjs
1import sha1 from 'sha1';2import i18next from 'i18next';3
4const t = (...args) => {5const key = args[0];6const options = args[1];7
8let text = i18next.t(key, options);9if (typeof text === 'string' && text.length === 0) {10text = i18next.t(key, { ...options, lng: 'en' });11}12
13return text;14};15
16const _ = (...args) => {17if ((args.length === 0) || (typeof args[0] === 'undefined')) {18return i18next.t.apply(i18next, args);19}20
21const [value = '', options = {}] = args;22const key = ((value, options) => {23const { context, count } = { ...options };24const containsContext = (context !== undefined) && (context !== null);25const containsPlural = (typeof count === 'number');26if (containsContext) {27value = value + i18next.options.contextSeparator + options.context;28}29if (containsPlural) {30value = value + i18next.options.pluralSeparator + 'plural';31}32return sha1(value);33})(value, options);34
35options.defaultValue = value;36
37let text = i18next.t(key, options);38if (typeof text !== 'string' || text.length === 0) {39text = i18next.t(key, { ...options, lng: 'en' });40}41
42return text;43};44
45export default {46t,47_
48};49