cncjs

Форк
0
/
i18n.js 
48 строк · 1.2 Кб
1
import sha1 from 'sha1';
2
import i18next from 'i18next';
3

4
const t = (...args) => {
5
  const key = args[0];
6
  const options = args[1];
7

8
  let text = i18next.t(key, options);
9
  if (typeof text === 'string' && text.length === 0) {
10
    text = i18next.t(key, { ...options, lng: 'en' });
11
  }
12

13
  return text;
14
};
15

16
const _ = (...args) => {
17
  if ((args.length === 0) || (typeof args[0] === 'undefined')) {
18
    return i18next.t.apply(i18next, args);
19
  }
20

21
  const [value = '', options = {}] = args;
22
  const key = ((value, options) => {
23
    const { context, count } = { ...options };
24
    const containsContext = (context !== undefined) && (context !== null);
25
    const containsPlural = (typeof count === 'number');
26
    if (containsContext) {
27
      value = value + i18next.options.contextSeparator + options.context;
28
    }
29
    if (containsPlural) {
30
      value = value + i18next.options.pluralSeparator + 'plural';
31
    }
32
    return sha1(value);
33
  })(value, options);
34

35
  options.defaultValue = value;
36

37
  let text = i18next.t(key, options);
38
  if (typeof text !== 'string' || text.length === 0) {
39
    text = i18next.t(key, { ...options, lng: 'en' });
40
  }
41

42
  return text;
43
};
44

45
export default {
46
  t,
47
  _
48
};
49

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.