git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 1aabebc53a78d5dfda5dbeacba5db26ca946d64e

Files: 1aabebc53a78d5dfda5dbeacba5db26ca946d64e / plugs / intl / sync / i18n.js

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

Built with git-ssb-web