Files: 1823d4041d777bbf71b294bd04d0a49e43727c24 / plugs / intl / sync / i18n.js
2475 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 | 'localeNames', |
10 | 'i18n', |
11 | 'time' |
12 | ]) |
13 | |
14 | exports.needs = nest({ |
15 | 'intl.sync.locale':'first', |
16 | 'intl.sync.locales':'reduce', |
17 | 'settings.obs.get': 'first', |
18 | 'settings.sync.set': 'first' |
19 | }) |
20 | |
21 | exports.create = (api) => { |
22 | let _locale |
23 | |
24 | const { |
25 | locale: getLocale, |
26 | locales: getLocales, |
27 | i18n: getI18n, |
28 | } = api.intl.sync |
29 | |
30 | return nest('intl.sync', { |
31 | locale, |
32 | locales, |
33 | localeNames, |
34 | i18n, |
35 | time |
36 | }) |
37 | |
38 | //Get locale value in setting |
39 | function locale () { |
40 | return api.settings.obs.get('patchwork.lang') |
41 | } |
42 | |
43 | //Get all locales loaded in i18nL |
44 | function locales (sofar = {}) { |
45 | return i18nL.getLocales() |
46 | } |
47 | |
48 | function localeNames () { |
49 | var names = i18nL.__l('$name') |
50 | return locales().reduce((result, item, i) => { |
51 | result[item] = names[i] |
52 | return result |
53 | }, {}) |
54 | } |
55 | |
56 | //Get translation |
57 | function i18n (value) { |
58 | _init() |
59 | return i18nL.__(value) |
60 | } |
61 | |
62 | function time (date){ |
63 | return date |
64 | .replace(/from now/, i18n('from now')) |
65 | .replace(/ago/, i18n('ago')) |
66 | .replace(/years/,i18n('years')) |
67 | .replace(/months/,i18n('months')) |
68 | .replace(/weeks/,i18n('weeks')) |
69 | .replace(/days/,i18n('days')) |
70 | .replace(/hours/,i18n('hours')) |
71 | .replace(/minutes/,i18n('minutes')) |
72 | .replace(/seconds/,i18n('seconds')) |
73 | .replace(/year/,i18n('year')) |
74 | .replace(/month/,i18n('month')) |
75 | .replace(/week/,i18n('week')) |
76 | .replace(/day/,i18n('day')) |
77 | .replace(/hour/,i18n('hour')) |
78 | .replace(/minute/,i18n('minute')) |
79 | .replace(/second/,i18n('second')) |
80 | } |
81 | |
82 | //Init an subscribe to settings changes. |
83 | function _init() { |
84 | if (_locale) return |
85 | //TODO: Depject this! |
86 | i18nL.configure({ |
87 | directory: appRoot + '/locales', |
88 | defaultLocale: 'en' |
89 | }); |
90 | |
91 | watch(api.settings.obs.get('patchwork.lang',navigator.language), currentLocale => { |
92 | i18nL.setLocale(getSubLocal(currentLocale)) |
93 | |
94 | // Only refresh if the language has already been selected once. |
95 | // This will prevent the update loop |
96 | if (_locale) { |
97 | electron.remote.getCurrentWebContents().reloadIgnoringCache() |
98 | } |
99 | }) |
100 | |
101 | _locale = true; |
102 | } |
103 | |
104 | } |
105 | |
106 | //For now get only global languages |
107 | function getSubLocal(loc) { |
108 | return loc.split('-')[0] |
109 | } |
110 |
Built with git-ssb-web