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