git ssb

10+

Matt McKegg / patchwork



Tree: e81b654394d0126eee1fae14be659f26ed3c31cc

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

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

Built with git-ssb-web