From 2bcd4e1e3fe5329be7e3289f503265c6178e8046 Mon Sep 17 00:00:00 2001 From: Ole R Date: Thu, 4 Feb 2016 23:10:50 +0100 Subject: [PATCH] Fix undefined S within translator If you `require(['string'], function (stringLib) { S = stringLib; })` it is an async call and thus you need to ensure any usage of `S` happens when this is resolved. --- public/src/modules/translator.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index 6c3db46116..4cd38851d5 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -2,15 +2,19 @@ "use strict"; /* globals RELATIVE_PATH, config, define */ - var S; + var S = null; + var stringDefer = null; // export the class if we are in a Node-like system. if (typeof module === 'object' && module.exports === translator) { exports = module.exports = translator; S = require('string'); + stringDefer.resolve(S); } else { + stringDefer = $.Deferred(); require(['string'], function(stringLib) { S = stringLib; + stringDefer.resolve(S); }); } @@ -161,6 +165,12 @@ if (!count) { return callback(text); } + + if (S === null) { // browser environment and S not yet initialized + // we need to wait for async require call + stringDefer.then(function () { translateKeys(keys, text, language, callback); }); + return; + } var data = {text: text}; keys.forEach(function(key) {