Files: cabc473e5efae9784e813e91373c35c0b8c5635f / plugs / intl / sync / i18n.js
2075 bytesRaw
1 | const nest = require('depnest') |
2 | var { watch } = require('mutant') |
3 | var appRoot = require('app-root-path'); |
4 | var i18nL = require("i18n") |
5 | |
6 | exports.gives = nest('intl.sync', [ |
7 | 'locale', |
8 | 'locales', |
9 | 'i18n', |
10 | 'time', |
11 | ]) |
12 | |
13 | exports.needs = nest({ |
14 | 'intl.sync.locale':'first', |
15 | 'intl.sync.locales':'reduce', |
16 | 'settings.obs.get': 'first', |
17 | 'settings.sync.set': 'first' |
18 | }) |
19 | |
20 | exports.create = (api) => { |
21 | let _locale |
22 | |
23 | const { |
24 | locale: getLocale, |
25 | locales: getLocales, |
26 | i18n: getI18n, |
27 | } = api.intl.sync |
28 | |
29 | return nest('intl.sync', { |
30 | locale, |
31 | locales, |
32 | i18n, |
33 | time |
34 | }) |
35 | |
36 | //Get locale value in setting |
37 | function locale () { |
38 | return api.settings.obs.get('patchwork.lang') |
39 | } |
40 | |
41 | //Get all locales loaded in i18nL |
42 | function locales (sofar = {}) { |
43 | return i18nL.getLocales() |
44 | } |
45 | |
46 | //Get translation |
47 | function i18n (value) { |
48 | _init() |
49 | return i18nL.__(value) |
50 | } |
51 | |
52 | function time (date){ |
53 | return date |
54 | .replace(/from now/, i18n('form now')) |
55 | .replace(/ago/, i18n('ago')) |
56 | .replace(/years/,i18n('years')) |
57 | .replace(/months/,i18n('months')) |
58 | .replace(/weeks/,i18n('weeks')) |
59 | .replace(/days/,i18n('days')) |
60 | .replace(/hours/,i18n('hours')) |
61 | .replace(/minutes/,i18n('minutes')) |
62 | .replace(/seconds/,i18n('seconds')) |
63 | .replace(/year/,i18n('year')) |
64 | .replace(/month/,i18n('month')) |
65 | .replace(/week/,i18n('week')) |
66 | .replace(/day/,i18n('day')) |
67 | .replace(/hour/,i18n('hour')) |
68 | .replace(/minute/,i18n('minute')) |
69 | .replace(/second/,i18n('second')) |
70 | } |
71 | |
72 | //Init an subscribe to settings changes. |
73 | function _init() { |
74 | if (_locale) return |
75 | //TODO: Depject this! |
76 | _locale = true; |
77 | i18nL.configure({ |
78 | locales:['en','ki','es'], |
79 | directory: appRoot + '/locales', |
80 | defaultLocale: 'en' |
81 | }); |
82 | |
83 | watch(api.settings.obs.get('patchwork.lang',navigator.language), currentLocale => { |
84 | i18nL.setLocale(getSubLocal(currentLocale)) |
85 | }) |
86 | } |
87 | |
88 | } |
89 | |
90 | //For now get only global languages |
91 | function getSubLocal(loc) { |
92 | return loc.split('-')[0] |
93 | } |
94 |
Built with git-ssb-web