From 2db4b7b7b29f70ba28e471c002b323d9cd35d5e8 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Mon, 28 Sep 2015 17:33:45 -0500 Subject: [PATCH] Move i18n to javascript --- common/script/i18n.coffee | 44 ------------------------- common/script/index.coffee | 3 +- common/script/src/content/helpers.js | 3 +- common/script/src/i18n.js | 49 ++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 47 deletions(-) delete mode 100644 common/script/i18n.coffee create mode 100644 common/script/src/i18n.js diff --git a/common/script/i18n.coffee b/common/script/i18n.coffee deleted file mode 100644 index edc3ae02c8..0000000000 --- a/common/script/i18n.coffee +++ /dev/null @@ -1,44 +0,0 @@ -_ = require 'lodash' - -module.exports = - strings: null, # Strings for one single language - translations: {} # Strings for multiple languages {en: strings, de: strings, ...} - t: (stringName) -> # Other parameters allowed are vars (Object) and locale (String) - vars = arguments[1] - - if _.isString(arguments[1]) - vars = null - locale = arguments[1] - else if arguments[2]? - vars = arguments[1] - locale = arguments[2] - - locale = 'en' if (!locale? or (!module.exports.strings and !module.exports.translations[locale])) - - if module.exports.strings - string = module.exports.strings[stringName] - else - string = - module.exports.translations[locale] and - module.exports.translations[locale][stringName] - - clonedVars = _.clone(vars) or {} - clonedVars.locale = locale - - if string - try - _.template(string, (clonedVars)) - catch e - 'Error processing the string. Please see Help > Report a Bug.' - else - if module.exports.strings - stringNotFound = module.exports.strings.stringNotFound - else if module.exports.translations[locale] - stringNotFound = - module.exports.translations[locale] and - module.exports.translations[locale].stringNotFound - - try - _.template(stringNotFound, {string: stringName}) - catch e - 'Error processing the string. Please see Help > Report a Bug.' diff --git a/common/script/index.coffee b/common/script/index.coffee index 3ad5d6e19a..189bdcbc59 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -1,7 +1,8 @@ moment = require('moment') _ = require('lodash') content = require('../dist/scripts/content/index') -i18n = require('./i18n.coffee') +i18n = require('../dist/scripts/i18n') + api = module.exports = {} api.i18n = i18n diff --git a/common/script/src/content/helpers.js b/common/script/src/content/helpers.js index d6f0b8e55b..6b8ff9be88 100644 --- a/common/script/src/content/helpers.js +++ b/common/script/src/content/helpers.js @@ -2,8 +2,7 @@ import {each, defaults, assign} from 'lodash'; import capitalize from 'lodash.capitalize'; import camelCase from 'lodash.camelcase'; -require('coffee-script'); -import i18n from '../../../script/i18n.coffee'; +import i18n from '../i18n'; //---------------------------------------- // Translator Helpers diff --git a/common/script/src/i18n.js b/common/script/src/i18n.js new file mode 100644 index 0000000000..315624e76a --- /dev/null +++ b/common/script/src/i18n.js @@ -0,0 +1,49 @@ +let _ = require('lodash'); + +module.exports = { + strings: null, + translations: {}, + t: function(stringName) { + var clonedVars, e, locale, string, stringNotFound, vars; + vars = arguments[1]; + if (_.isString(arguments[1])) { + vars = null; + locale = arguments[1]; + } else if (arguments[2] != null) { + vars = arguments[1]; + locale = arguments[2]; + } + if ((locale == null) || (!module.exports.strings && !module.exports.translations[locale])) { + locale = 'en'; + } + if (module.exports.strings) { + string = module.exports.strings[stringName]; + } else { + string = module.exports.translations[locale] && module.exports.translations[locale][stringName]; + } + clonedVars = _.clone(vars) || {}; + clonedVars.locale = locale; + if (string) { + try { + return _.template(string, clonedVars); + } catch (_error) { + e = _error; + return 'Error processing the string. Please see Help > Report a Bug.'; + } + } else { + if (module.exports.strings) { + stringNotFound = module.exports.strings.stringNotFound; + } else if (module.exports.translations[locale]) { + stringNotFound = module.exports.translations[locale] && module.exports.translations[locale].stringNotFound; + } + try { + return _.template(stringNotFound, { + string: stringName + }); + } catch (_error) { + e = _error; + return 'Error processing the string. Please see Help > Report a Bug.'; + } + } + } +};