plugs/intl/sync/i18n.jsView |
---|
| 1 | +const nest = require('depnest') |
| 2 | +var { watch } = require('mutant') |
| 3 | +var appRoot = require('app-root-path'); |
| 4 | +var i18nL = require("i18n") |
| 5 | + |
| 6 | +exports.gives = nest('intl.sync', [ |
| 7 | + 'locale', |
| 8 | + 'locales', |
| 9 | + 'i18n', |
| 10 | +]) |
| 11 | + |
| 12 | +exports.needs = nest({ |
| 13 | + 'intl.sync.locale':'first', |
| 14 | + 'intl.sync.locales':'reduce', |
| 15 | + 'settings.obs.get': 'first', |
| 16 | + 'settings.sync.set': 'first' |
| 17 | +}) |
| 18 | + |
| 19 | +exports.create = (api) => { |
| 20 | + let _locale |
| 21 | + |
| 22 | + const { |
| 23 | + locale: getLocale, |
| 24 | + locales: getLocales, |
| 25 | + i18n: getI18n, |
| 26 | + } = api.intl.sync |
| 27 | + |
| 28 | + return nest('intl.sync', { |
| 29 | + locale, |
| 30 | + locales, |
| 31 | + i18n |
| 32 | + }) |
| 33 | + |
| 34 | + |
| 35 | + function locale () { |
| 36 | + return api.settings.obs.get('patchwork.lang') |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + function locales (sofar = {}) { |
| 41 | + return i18nL.getLocales() |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + function i18n (value) { |
| 46 | + _init() |
| 47 | + return i18nL.__(value) |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + function _init() { |
| 52 | + if (_locale) return |
| 53 | + |
| 54 | + i18nL.configure({ |
| 55 | + locales:['en','ki','es'], |
| 56 | + directory: appRoot + '/locales' |
| 57 | + }); |
| 58 | + |
| 59 | + watch(api.settings.obs.get('patchwork.lang'), currentLocale => { |
| 60 | + i18nL.setLocale(currentLocale) |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +function getSubLocal(loc) { |
| 68 | + return loc.split('-')[0] |
| 69 | +} |