]> git.localhorst.tv Git - alttp.git/blob - resources/js/i18n/index.js
chat bot config
[alttp.git] / resources / js / i18n / index.js
1 import i18n from 'i18next';
2 import LanguageDetector from 'i18next-browser-languagedetector';
3 import moment from 'moment';
4 import numeral from 'numeral';
5 import { initReactI18next } from 'react-i18next';
6
7 import de from './de';
8 import en from './en';
9 import 'numeral/locales/de';
10 import 'moment/locale/de';
11
12 const supportedLocales = [
13         'de',
14         'en',
15 ];
16
17 const resolveLocale = (loc) => {
18         if (supportedLocales.includes(loc)) return loc;
19         const sub = loc.substr(0, 2);
20         if (supportedLocales.includes(sub)) return sub;
21         return 'de';
22 };
23
24 i18n
25         .use(LanguageDetector)
26         .use(initReactI18next)
27         .on('languageChanged', (lng) => {
28                 const loc = resolveLocale(lng);
29                 moment.locale(loc);
30                 numeral.locale(loc);
31         })
32         .init({
33                 fallbackLng: 'de',
34                 interpolation: {
35                         escapeValue: false,
36                         format: (value, format) => {
37                                 if (value instanceof Date) return moment(value).format(format);
38                                 return value;
39                         },
40                 },
41                 resources: {
42                         de,
43                         en,
44                 },
45                 supportedLngs: supportedLocales,
46         });
47
48 i18n.number = (value, options) => {
49         const num = numeral(value);
50         if (Number.isNaN(num.value())) {
51                 return `${value}`;
52         }
53         const format = options && Object.prototype.hasOwnProperty.call(options, 'decimals')
54                 ? `0,0.${'0'.repeat(options.decimals)}`
55                 : '0,0.[000000]';
56         return num.format(format);
57 };
58
59 export default i18n;
60