git ssb

10+

Matt McKegg / patchwork



Tree: e836f599f0095c03c6bb4b228c714e7130f797f6

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

1997 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('form 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 locales:['en','ki','es'],
78 directory: appRoot + '/locales'
79 });
80
81 watch(api.settings.obs.get('patchwork.lang'), currentLocale => {
82 i18nL.setLocale(currentLocale)
83 })
84 }
85
86}
87
88//For now get only global languages
89function getSubLocal(loc) {
90 return loc.split('-')[0]
91}
92

Built with git-ssb-web