git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: c8278cc719a12db5a2a110402e4c30516a93e788

Files: c8278cc719a12db5a2a110402e4c30516a93e788 / plugs / intl / sync / i18n.js

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

Built with git-ssb-web