From 80b36921317678dfcb948f5534c253d22a937114 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 26 Sep 2017 18:27:41 +0200 Subject: [PATCH] More work in removing locales from builds --- Makefile | 28 +++---- locale/nolocales.js | 20 ----- locale/single_locale.js | 23 ------ src/config.js | 29 +------ src/converse-core.js | 169 +++++++++++++++++++++++++++++++++++++--- src/converse-muc.js | 15 +++- src/locales.js | 52 ------------- src/utils.js | 127 +----------------------------- 8 files changed, 191 insertions(+), 272 deletions(-) delete mode 100644 locale/nolocales.js delete mode 100644 locale/single_locale.js delete mode 100755 src/locales.js diff --git a/Makefile b/Makefile index 90c5293b9..ccf335247 100644 --- a/Makefile +++ b/Makefile @@ -172,33 +172,33 @@ BUILDS = dist/converse.js \ dist/converse-no-dependencies.min.js \ dist/converse-no-dependencies.js -dist/converse.js: transpile src locale node_modules *.js +dist/converse.js: transpile src node_modules *.js $(RJS) -o src/build.js include=converse out=dist/converse.js optimize=none -dist/converse.min.js: src locale node_modules *.js +dist/converse.min.js: src node_modules *.js $(RJS) -o src/build.js include=converse out=dist/converse.min.js -dist/converse-esnext.js: src locale node_modules *.js transpile +dist/converse-esnext.js: src node_modules *.js transpile $(RJS) -o src/build-esnext.js include=converse out=dist/converse-esnext.js optimize=none -dist/converse-esnext.min.js: src locale node_modules *.js transpile +dist/converse-esnext.min.js: src node_modules *.js transpile $(RJS) -o src/build-esnext.js include=converse out=dist/converse-esnext.min.js -dist/inverse.js: transpile src locale node_modules *.js +dist/inverse.js: transpile src node_modules *.js $(RJS) -o src/build-inverse.js include=inverse out=dist/inverse.js optimize=none -dist/inverse.min.js: src locale node_modules *.js +dist/inverse.min.js: src node_modules *.js $(RJS) -o src/build-inverse.js include=inverse out=dist/inverse.min.js -dist/converse-no-jquery.js: transpile src locale node_modules *.js +dist/converse-no-jquery.js: transpile src node_modules *.js $(RJS) -o src/build.js include=converse wrap.endFile=end-no-jquery.frag exclude=jquery exclude=jquery.noconflict out=dist/converse-no-jquery.js optimize=none -dist/converse-no-jquery.min.js: src locale node_modules *.js transpile +dist/converse-no-jquery.min.js: src node_modules *.js transpile $(RJS) -o src/build.js include=converse wrap.endFile=end-no-jquery.frag exclude=jquery exclude=jquery.noconflict out=dist/converse-no-jquery.min.js -dist/converse-no-dependencies.js: transpile src locale node_modules *.js +dist/converse-no-dependencies.js: transpile src node_modules *.js $(RJS) -o src/build-no-dependencies.js optimize=none out=dist/converse-no-dependencies.js -dist/converse-no-dependencies.min.js: src locale node_modules *.js +dist/converse-no-dependencies.min.js: src node_modules *.js $(RJS) -o src/build-no-dependencies.js out=dist/converse-no-dependencies.min.js -dist/converse-mobile.js: transpile src locale node_modules *.js +dist/converse-mobile.js: transpile src node_modules *.js $(RJS) -o src/build.js paths.converse=src/converse-mobile include=converse out=dist/converse-mobile.js optimize=none -dist/converse-mobile.min.js: src locale node_modules *.js +dist/converse-mobile.min.js: src node_modules *.js $(RJS) -o src/build.js paths.converse=src/converse-mobile include=converse out=dist/converse-mobile.min.js -dist/converse-muc-embedded.js: transpile src locale node_modules *.js +dist/converse-muc-embedded.js: transpile src node_modules *.js $(RJS) -o src/build.js paths.converse=src/converse-embedded include=converse out=dist/converse-muc-embedded.js optimize=none -dist/converse-muc-embedded.min.js: src locale node_modules *.js +dist/converse-muc-embedded.min.js: src node_modules *.js $(RJS) -o src/build.js paths.converse=src/converse-embedded include=converse out=dist/converse-muc-embedded.min.js .PHONY: jsmin diff --git a/locale/nolocales.js b/locale/nolocales.js deleted file mode 100644 index 7f5135e66..000000000 --- a/locale/nolocales.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * This file can be used if no locale support is required. - */ -(function (root, factory) { - define("locales", ['jed'], function (Jed) { - var translations = { - "domain": "converse", - "locale_data": { - "converse": { - "": { - "domain": "converse", - "lang": "en", - "plural_forms": "nplurals=2; plural=(n != 1);" - } - } - } - }; - root.locales = { 'en': new Jed(translations) }; - }); -})(this); diff --git a/locale/single_locale.js b/locale/single_locale.js deleted file mode 100644 index 51732cf30..000000000 --- a/locale/single_locale.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * This file specifies a single language dependency (for English). - * - * Translations take up a lot of space and you are therefore advised to remove - * from here any languages that you don't need. - */ - -(function (root, factory) { - require.config({ - paths: { - "jed": "Libraries/jed", - "en": "locale/en/LC_MESSAGES/en" - } - }); - - define("locales", [ - 'jed', - 'en' - ], function (jed, en) { - root.locales = {}; - root.locales.en = en; - }); -})(this); diff --git a/src/config.js b/src/config.js index 4253a986d..c482bb719 100644 --- a/src/config.js +++ b/src/config.js @@ -26,6 +26,7 @@ require.config({ "es6-promise": "node_modules/es6-promise/dist/es6-promise.auto", "eventemitter": "node_modules/otr/build/dep/eventemitter", "form-utils": "src/form-utils", + "jed": "node_modules/jed/jed", "jquery": "node_modules/jquery/dist/jquery", "jquery.browser": "node_modules/jquery.browser/dist/jquery.browser", "jquery.noconflict": "src/jquery.noconflict", @@ -33,10 +34,9 @@ require.config({ "lodash.converter": "3rdparty/lodash.fp", "lodash.fp": "src/lodash.fp", "lodash.noconflict": "src/lodash.noconflict", + "moment_with_locales": "3rdparty/moment_locales", "pluggable": "node_modules/pluggable.js/dist/pluggable", "polyfill": "src/polyfill", - "virtual-dom": "node_modules/virtual-dom/dist/virtual-dom", - "vdom-parser": "node_modules/vdom-parser/dist", "sizzle": "node_modules/jquery/sizzle/dist/sizzle", "strophe": "node_modules/strophe.js/strophe", "strophe.disco": "node_modules/strophejs-plugin-disco/strophe.disco", @@ -47,6 +47,8 @@ require.config({ "tpl": "node_modules/lodash-template-loader/loader", "underscore": "src/underscore-shim", "utils": "src/utils", + "vdom-parser": "node_modules/vdom-parser/dist", + "virtual-dom": "node_modules/virtual-dom/dist/virtual-dom", "xss": "node_modules/xss/dist/xss", "xss.noconflict": "src/xss.noconflict", @@ -82,29 +84,6 @@ require.config({ "crypto": "node_modules/otr/build/dep/crypto", "salsa20": "node_modules/otr/build/dep/salsa20", "otr": "node_modules/otr/build/otr", - - // Locales paths - "locales": "src/locales", - "jed": "node_modules/jed/jed", - "af": "locale/af/LC_MESSAGES/converse.json", - "ca": "locale/ca/LC_MESSAGES/converse.json", - "de": "locale/de/LC_MESSAGES/converse.json", - "es": "locale/es/LC_MESSAGES/converse.json", - "fr": "locale/fr/LC_MESSAGES/converse.json", - "he": "locale/he/LC_MESSAGES/converse.json", - "hu": "locale/hu/LC_MESSAGES/converse.json", - "id": "locale/id/LC_MESSAGES/converse.json", - "it": "locale/it/LC_MESSAGES/converse.json", - "ja": "locale/ja/LC_MESSAGES/converse.json", - "nb": "locale/nb/LC_MESSAGES/converse.json", - "nl": "locale/nl/LC_MESSAGES/converse.json", - "pl": "locale/pl/LC_MESSAGES/converse.json", - "pt_BR": "locale/pt_BR/LC_MESSAGES/converse.json", - "ru": "locale/ru/LC_MESSAGES/converse.json", - "uk": "locale/uk/LC_MESSAGES/converse.json", - "zh": "locale/zh/LC_MESSAGES/converse.json", - - "moment_with_locales": "3rdparty/moment_locales", }, packages: [{ diff --git a/src/converse-core.js b/src/converse-core.js index e92a0d083..916d7ccb5 100755 --- a/src/converse-core.js +++ b/src/converse-core.js @@ -10,6 +10,7 @@ "es6-promise", "lodash.noconflict", "polyfill", + "jed", "utils", "moment_with_locales", "strophe", @@ -18,9 +19,7 @@ "backbone.browserStorage", "backbone.overview", ], factory); -}(this, function ( - sizzle, Promise, _, polyfill, - utils, moment, Strophe, pluggable, Backbone) { +}(this, function (sizzle, Promise, _, polyfill, Jed, utils, moment, Strophe, pluggable, Backbone) { /* Cannot use this due to Safari bug. * See https://github.com/jcbrand/converse.js/issues/196 @@ -52,7 +51,8 @@ _.templateSettings = { 'escape': /\{\{\{([\s\S]+?)\}\}\}/g, 'evaluate': /\{\[([\s\S]+?)\]\}/g, - 'interpolate': /\{\{([\s\S]+?)\}\}/g + 'interpolate': /\{\{([\s\S]+?)\}\}/g, + 'imports': { '_': _ } }; const _converse = { @@ -131,6 +131,19 @@ _converse.DEFAULT_IMAGE = "iVBORw0KGgoAAAANSUhEUg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D29nbug7iT5sIBcUAcEAfEAXFAHBAHxOVn+QMrmWpuPZx12gAAAABJRU5ErkJggg=="; _converse.log = function (message, level) { + /* Logs messages to the browser's developer console. + * + * Parameters: + * (String) message - The message to be logged. + * (Integer) level - The loglevel which allows for filtering of log + * messages. + * + * Available loglevels are 0 for 'debug', 1 for 'info', 2 for 'warn', + * 3 for 'error' and 4 for 'fatal'. + * + * When using the 'error' or 'warn' loglevels, a full stacktrace will be + * logged as well. + */ if (message instanceof Error) { message = message.stack; } @@ -163,6 +176,137 @@ } }; + // --------------------- + // Translation machinery + // --------------------- + _converse.__ = function (str) { + /* Translate the given string based on the current locale. + * + * Parameters: + * (String) str - The string to translate. + */ + if (_.isUndefined(Jed)) { + return str; + } + if (_.isUndefined(_converse.jed)) { + return Jed.sprintf.apply(Jed, arguments); + } + var t = _converse.jed.translate(str); + if (arguments.length>1) { + return t.fetch.apply(t, [].slice.call(arguments, 1)); + } else { + return t.fetch(); + } + }; + + const detectLocale = function (library_check) { + /* Determine which locale is supported by the user's system as well + * as by the relevant library (e.g. converse.js or moment.js). + * + * Parameters: + * (Function) library_check - Returns a boolean indicating whether + * the locale is supported. + */ + var locale, i; + if (window.navigator.userLanguage) { + locale = isLocaleAvailable(window.navigator.userLanguage, library_check); + } + if (window.navigator.languages && !locale) { + for (i=0; i + /* Fetch the translations for the given local at the given URL. + * + * Parameters: + * (String) locale: The given i18n locale + * (String) locale_url: The URL from which the translations should be fetched + */ + new Promise((resolve, reject) => { + if (!isLocaleSupported(locale) || locale === 'en') { + return resolve(); + } + const xhr = new XMLHttpRequest(); + xhr.open( + 'GET', + locale_url, + true + ); + xhr.setRequestHeader( + 'Accept', + "application/json, text/javascript" + ); + xhr.onload = function () { + if (xhr.status >= 200 && xhr.status < 400) { + resolve(new Jed(window.JSON.parse(xhr.responseText))); + } else { + xhr.onerror(); + } + }; + xhr.onerror = function () { + reject(xhr.statusText); + }; + xhr.send(); + }); + // -------------------------- + // END: Translation machinery + // -------------------------- + + const PROMISES = [ 'initialized', 'cachedRoster', @@ -234,9 +378,9 @@ }; /* Internationalization */ - moment.locale(utils.getLocale(settings.i18n, utils.isMomentLocale)); - _converse.locale = utils.getLocale(settings.i18n, utils.isLocaleSupported); - const __ = _converse.__ = _.partial(utils.__, _converse); + moment.locale(getLocale(settings.i18n, isMomentLocale)); + _converse.locale = getLocale(settings.i18n, isLocaleSupported); + const __ = _converse.__; // XEP-0085 Chat states // http://xmpp.org/extensions/xep-0085.html @@ -273,6 +417,11 @@ jid: undefined, keepalive: true, locales_url: '/locale/{{{locale}}}/LC_MESSAGES/converse.json', + locales: [ + 'af', 'ca', 'de', 'es', 'en', 'fr', 'he', + 'hu', 'id', 'it', 'ja', 'nb', 'nl', + 'pl', 'pt_BR', 'ru', 'uk', 'zh' + ], message_carbons: true, message_storage: 'session', password: undefined, @@ -1884,15 +2033,15 @@ finishInitialization(); return _converse; } else { - utils.fetchLocale( + fetchLocale( _converse.locale, - _converse.locales_url + _.template(_converse.locales_url)({'locale': _converse.locale}) ).then((jed) => { _converse.jed = jed; finishInitialization(); }).catch((reason) => { finishInitialization(); - _converse.log(reason, Strophe.LogLevel.FATAL); + _converse.log(reason, Strophe.LogLevel.ERROR); }); return init_promise.promise; } diff --git a/src/converse-muc.js b/src/converse-muc.js index e29b4ff0d..688504dac 100755 --- a/src/converse-muc.js +++ b/src/converse-muc.js @@ -246,8 +246,19 @@ * loaded by converse.js's plugin machinery. */ const { _converse } = this, - { __ } = _converse, - { ___ } = utils; + { __ } = _converse; + + function ___ (str) { + /* This is part of a hack to get gettext to scan strings to be + * translated. Strings we cannot send to the function above because + * they require variable interpolation and we don't yet have the + * variables at scan time. + * + * See actionInfoMessages further below. + */ + return str; + } + // XXX: Inside plugins, all calls to the translation machinery // (e.g. utils.__) should only be done in the initialize function. // If called before, we won't know what language the user wants, diff --git a/src/locales.js b/src/locales.js deleted file mode 100755 index d42cf0321..000000000 --- a/src/locales.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file specifies the language dependencies. - * - * Translations take up a lot of space and you are therefore advised to remove - * from here any languages that you don't need. - * - * See also src/moment_locales.js - */ -/*global define */ -(function (root, factory) { - define(['jed', - 'text!af', - 'text!ca', - 'text!de', - 'text!es', - 'text!fr', - 'text!he', - 'text!hu', - 'text!id', - 'text!it', - 'text!ja', - 'text!nb', - 'text!nl', - 'text!pl', - 'text!pt_BR', - 'text!ru', - 'text!uk', - 'text!zh' - ], function ($, Jed) { - root.locales = { - 'en': {}, - 'af': arguments[1], - 'ca': arguments[2], - 'de': arguments[3], - 'es': arguments[4], - 'fr': arguments[5], - 'he': arguments[6], - 'hu': arguments[7], - 'id': arguments[8], - 'it': arguments[9], - 'ja': arguments[10], - 'nb': arguments[11], - 'nl': arguments[12], - 'pl': arguments[13], - 'pt-br': arguments[14], - 'ru': arguments[15], - 'uk': arguments[16], - 'zh': arguments[17] - }; - return root.locales; - }); -})(this); diff --git a/src/utils.js b/src/utils.js index c13546c4f..8b1a8d529 100755 --- a/src/utils.js +++ b/src/utils.js @@ -6,14 +6,13 @@ // Copyright (c) 2012-2017, Jan-Carel Brand // Licensed under the Mozilla Public License (MPLv2) // -/*global define, escape, locales, window, Jed */ +/*global define, escape, window */ (function (root, factory) { define([ "sizzle", "es6-promise", "jquery.browser", "lodash.noconflict", - "locales", "moment_with_locales", "strophe", ], factory); @@ -22,12 +21,10 @@ Promise, jQBrowser, _, - locales, moment, Strophe ) { "use strict"; - locales = locales || {}; const b64_sha1 = Strophe.SHA1.b64_sha1; Strophe = Strophe.Strophe; @@ -109,78 +106,6 @@ var u = {}; - // Translation machinery - // --------------------- - u.fetchLocale = (locale, locales_url) => - new Promise((resolve, reject) => { - if (!u.isLocaleSupported(locale) || locale === 'en') { - resolve(); - } - const xhr = new XMLHttpRequest(); - xhr.open( - 'GET', - _.template(locales_url)({'locale': locale}), - true - ); - xhr.setRequestHeader( - 'Accept', - "application/json, text/javascript" - ); - xhr.onload = function () { - if (xhr.status >= 200 && xhr.status < 400) { - resolve(new Jed(window.JSON.parse(xhr.responseText))); - } else { - xhr.onerror(); - } - }; - xhr.onerror = function () { - reject(xhr.statusText); - }; - xhr.send(); - }); - - u.__ = function (_converse, str) { - if (_.isUndefined(window.Jed)) { - return str; - } - if (_.isUndefined(_converse.jed)) { - return Jed.sprintf.apply(window.Jed, [].slice.call(arguments, 1)); - } - var t = _converse.jed.translate(str); - if (arguments.length>1) { - return t.fetch.apply(t, [].slice.call(arguments, 2)); - } else { - return t.fetch(); - } - }; - - u.___ = function (str) { - /* XXX: This is part of a hack to get gettext to scan strings to be - * translated. Strings we cannot send to the function above because - * they require variable interpolation and we don't yet have the - * variables at scan time. - * - * See actionInfoMessages in src/converse-muc.js - */ - return str; - }; - - u.isLocaleAvailable = function (locale, available) { - /* Check whether the locale or sub locale (e.g. en-US, en) is supported. - * - * Parameters: - * (Function) available - returns a boolean indicating whether the locale is supported - */ - if (available(locale)) { - return locale; - } else { - var sublocale = locale.split("-")[0]; - if (sublocale !== locale && available(sublocale)) { - return sublocale; - } - } - }; - u.addHyperlinks = function (text) { const list = text.match(URL_REGEX) || []; var links = []; @@ -480,56 +405,6 @@ }; }; - - u.detectLocale = function (library_check) { - /* Determine which locale is supported by the user's system as well - * as by the relevant library (e.g. converse.js or moment.js). - * - * Parameters: - * (Function) library_check - returns a boolean indicating whether - * the locale is supported. - */ - var locale, i; - if (window.navigator.userLanguage) { - locale = u.isLocaleAvailable(window.navigator.userLanguage, library_check); - } - if (window.navigator.languages && !locale) { - for (i=0; i