Skip to content

Commit

Permalink
Merge pull request #106 from sakulstra/remove-underscore
Browse files Browse the repository at this point in the history
Remove underscore
  • Loading branch information
cristo-rabani committed Oct 18, 2018
2 parents 0ebbac1 + b25e56b commit e826ad0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 45 deletions.
4 changes: 2 additions & 2 deletions builder.js
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions 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';
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions package.js
Expand Up @@ -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'
]);

Expand Down
44 changes: 18 additions & 26 deletions 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';
Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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
} = {}) => {
Expand All @@ -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;
};
20 changes: 7 additions & 13 deletions server/handler.js
Expand Up @@ -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();
}
Expand All @@ -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));
}
});

0 comments on commit e826ad0

Please sign in to comment.