git ssb

10+

Matt McKegg / patchwork



Tree: 719c20c66b72e2b795bcbf2367d4ec4e85bb0d1a

Files: 719c20c66b72e2b795bcbf2367d4ec4e85bb0d1a / plugs / intl / sync / i18n.js

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

Built with git-ssb-web