diff --git a/builder.js b/builder.js index a846a6d..d813835 100644 --- a/builder.js +++ b/builder.js @@ -103,8 +103,8 @@ class UniverseI18nBuilder extends CachingCompiler { } addCompileResult (file, {locale, data, ts}) { - if (file.getArch() === 'web.browser' && !_.contains(this.localesInClientBundle, 'all')) { - if (!_.contains(this.localesInClientBundle, locale)) { + if (file.getArch() === 'web.browser' && !this.localesInClientBundle.includes('all')) { + if (!this.localesInClientBundle.includes(locale)) { file.addJavaScript({ path: file.getPathInPackage() + '.js', data: ts diff --git a/lib/i18n.js b/lib/i18n.js index d04566f..f22f4c7 100644 --- a/lib/i18n.js +++ b/lib/i18n.js @@ -1,5 +1,4 @@ import {Meteor} from 'meteor/meteor'; -import {_} from 'meteor/underscore'; import {Emitter, get, set, RecursiveIterator, deepExtend} from './utilities'; import {LOCALES, CURRENCIES, SYMBOLS} from './locales'; @@ -158,7 +157,7 @@ export const i18n = { _translations: {}, setOptions (options) { - i18n.options = _.extend(i18n.options || {}, options); + i18n.options = {...(i18n.options || {}), ...options}; }, //For blaze and autoruns diff --git a/package.js b/package.js index dbec5e5..5a222c0 100644 --- a/package.js +++ b/package.js @@ -25,13 +25,12 @@ Package.onUse(function (api) { api.use([ 'ddp', - 'http', + 'fetch', 'check', 'webapp', 'tracker', 'promise', 'ecmascript', - 'underscore', 'isobuild:compiler-plugin@1.0.0' ]); diff --git a/server/api.js b/server/api.js index 56f5e2b..5b8f32e 100644 --- a/server/api.js +++ b/server/api.js @@ -1,6 +1,5 @@ import i18n from '../lib/i18n'; import locales from '../lib/locales'; -import {_} from 'meteor/underscore'; import {set} from '../lib/utilities'; import YAML from 'js-yaml'; import stripJsonComments from 'strip-json-comments'; @@ -26,7 +25,7 @@ i18n.getCache = function getCache (locale) { }; function getDiff (locale, diffWith) { - const keys = _.difference(i18n.getAllKeysForLocale(locale), i18n.getAllKeysForLocale(diffWith)); + const keys = [i18n.getAllKeysForLocale(locale), i18n.getAllKeysForLocale(diffWith)].reduce((a,b) => a.filter(c => !b.includes(c))); const diffLoc = {}; keys.forEach(key => set(diffLoc, key, i18n.getTranslation(key))); return diffLoc; @@ -36,7 +35,7 @@ function getYML (locale, namespace, diffWith) { if (namespace && typeof namespace === 'string') { if (!cache[locale]['_yml' + namespace]) { let translations = i18n.getTranslations(namespace, locale) || {}; - translations = _.extend({_namespace: namespace}, translations); + translations = {_namespace: namespace, ...translations}; cache[locale]['_yml' + namespace] = YAML.dump(translations, YAML_OPTIONS); } return cache[locale]['_yml' + namespace]; @@ -57,7 +56,7 @@ function getJSON (locale, namespace, diffWith) { if (namespace && typeof namespace === 'string') { if (!cache[locale]['_json' + namespace]) { let translations = i18n.getTranslations(namespace, locale) || {}; - translations = _.extend({_namespace: namespace}, translations); + translations = {_namespace: namespace, ...translations}; cache[locale]['_json' + namespace] = JSON.stringify(translations); } return cache[locale]['_json' + namespace]; @@ -96,7 +95,7 @@ i18n.setOptions({ } }); -i18n.loadLocale = (localeName, { +i18n.loadLocale = async (localeName, { host = i18n.options.hostUrl, pathOnHost = i18n.options.pathOnHost, queryParams = {}, fresh = false, silent = false } = {}) => { @@ -106,30 +105,23 @@ i18n.loadLocale = (localeName, { queryParams.ts = (new Date().getTime()); } let url = URL.resolve(host, pathOnHost + localeName); - const promise = new Promise(function (resolve, reject) { - HTTP.get(url, {params: queryParams}, (error, result) => { - const {content} = result || {}; - if (error || !content) { - return reject(error || 'missing content'); - } - try { - i18n.addTranslations(localeName, JSON.parse(stripJsonComments(content))); - delete cache[localeName]; - } catch (e) { - return reject(e); - } - resolve(); - }); - }); - if (!silent) { - promise.then(() => { + try { + const data = await fetch(url, {method: "GET"}); + const json = await data.json(); + const {content} = json || {}; + if (!content) { + return console.error('missing content'); + } + i18n.addTranslations(localeName, JSON.parse(stripJsonComments(content))); + delete cache[localeName]; + if (!silent) { const locale = i18n.getLocale(); //If current locale is changed we must notify about that. if (locale.indexOf(localeName) === 0 || i18n.options.defaultLocale.indexOf(localeName) === 0) { - i18n._emitChange(); + i18n._emitChange(); } - }); + } + }catch(err){ + console.error(err); } - promise.catch(console.error.bind(console)); - return promise; }; diff --git a/server/handler.js b/server/handler.js index 59ed3b7..01367be 100644 --- a/server/handler.js +++ b/server/handler.js @@ -6,7 +6,7 @@ WebApp.connectHandlers.use('/universe/locale/', function(req, res, next) { const {pathname, query} = url.parse(req.url, true); const {type, namespace, preload=false, attachment=false, diff=false} = query || {}; - if (type && !_.contains(['yml', 'json', 'js'], type)) { + if (type && !['yml', 'json', 'js'].includes(type)) { res.writeHead(415); return res.end(); } @@ -27,22 +27,16 @@ WebApp.connectHandlers.use('/universe/locale/', function(req, res, next) { } switch (type) { case 'json': - res.writeHead(200, _.extend( - {'Content-Type': 'application/json; charset=utf-8'}, - i18n.options.translationsHeaders, headerPart - )); + res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8', + ...i18n.options.translationsHeaders, ...headerPart}); return res.end(cache.getJSON(locale, namespace, diff)); case 'yml': - res.writeHead(200, _.extend( - {'Content-Type': 'text/yaml; charset=utf-8'}, - i18n.options.translationsHeaders, headerPart - )); + res.writeHead(200, {'Content-Type': 'text/yaml; charset=utf-8', + ...i18n.options.translationsHeaders, ...headerPart}); return res.end(cache.getYML(locale, namespace, diff)); default: - res.writeHead(200, _.extend( - {'Content-Type': 'application/javascript; charset=utf-8'}, - i18n.options.translationsHeaders, headerPart - )); + res.writeHead(200, {'Content-Type': 'application/javascript; charset=utf-8', + ...i18n.options.translationsHeaders, ...headerPart}); return res.end(cache.getJS(locale, namespace, preload)); } });