git ssb

10+

Matt McKegg / patchwork



Tree: f2ca2ca157f5185508d7d9ac0fc3e92532a793cd

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

3159 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 var locales = i18nL.getLocales()
96
97 // Try BCP47 codes, otherwise load family language if exist
98 if (locales.indexOf(currentLocale) !== -1) {
99 i18nL.setLocale(currentLocale)
100 } else {
101 i18nL.setLocale(getSimilar(locales, currentLocale))
102 }
103
104 // Only refresh if the language has already been selected once.
105 // This will prevent the update loop
106 if (_locale) {
107 electron.remote.getCurrentWebContents().reloadIgnoringCache()
108 }
109 })
110
111 _locale = true
112 }
113}
114
115// For now get only global languages
116function getSubLocal (loc) {
117 return loc.split('-')[0]
118}
119
120function getSimilar (locales, option) {
121 var reindexed = {}
122 locales.forEach(function (local) {
123 (reindexed[getSubLocal(local)])
124 ? reindexed[getSubLocal(local)].concat(local)
125 : reindexed[getSubLocal(local)] = [local]
126 }, this)
127 if (reindexed[getSubLocal(option)]) {
128 return reindexed[getSubLocal(option)][0]
129 }
130 return option
131}
132

Built with git-ssb-web