Skip to content

Commit

Permalink
fix: loading messages from script tags. (google#6184)
Browse files Browse the repository at this point in the history
* fix: loading messages in the browser

* chore: fix comment

* fix: change unwrapped message files to write to a new object, rather than Blockly.Msg

* fix: fixup exports

* fix: PR comments

* fix: change to use for-in loop

* fix: ES6 compatibility and formatting
  • Loading branch information
BeksOmega committed Jun 13, 2022
1 parent 24a808d commit 188ba98
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
15 changes: 6 additions & 9 deletions scripts/gulpfiles/package_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ const TEMPLATE_DIR = 'scripts/package/templates';
* @param {string} namespace The export namespace.
* @param {Array<Object>} dependencies An array of dependencies to inject.
*/
function packageUMD(namespace, dependencies) {
function packageUMD(namespace, dependencies, template = 'umd.template') {
return gulp.umd({
dependencies: function () { return dependencies; },
namespace: function () { return namespace; },
exports: function () { return namespace; },
template: path.join(TEMPLATE_DIR, 'umd.template')
template: path.join(TEMPLATE_DIR, template)
});
};

Expand Down Expand Up @@ -321,13 +321,10 @@ function packageLocales() {
// Remove references to goog.provide and goog.require.
return gulp.src(`${BUILD_DIR}/msg/js/*.js`)
.pipe(gulp.replace(/goog\.[^\n]+/g, ''))
.pipe(gulp.insert.prepend(`
var Blockly = {};Blockly.Msg={};`))
.pipe(packageUMD('Blockly.Msg', [{
name: 'Blockly',
amd: '../core',
cjs: '../core',
}]))
.pipe(packageUMD(
'Blockly.Msg',
[{name: 'Blockly'}],
'umd-msg.template'))
.pipe(gulp.dest(`${RELEASE_DIR}/msg`));
};

Expand Down
8 changes: 5 additions & 3 deletions scripts/i18n/create_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def load_constants(filename):
for key in constant_defs:
value = constant_defs[key]
value = value.replace('"', '\\"')
constants_text += u'\nBlockly.Msg["{0}"] = \"{1}\";'.format(
constants_text += u'\nmessages["{0}"] = \"{1}\";'.format(
key, value)
return constants_text

Expand Down Expand Up @@ -88,7 +88,7 @@ def main():
os.curdir, args.source_synonym_file))

# synonym_defs is also being sorted to ensure the same order is kept
synonym_text = '\n'.join([u'Blockly.Msg["{0}"] = Blockly.Msg["{1}"];'
synonym_text = '\n'.join([u'messages["{0}"] = messages["{1}"];'
.format(key, synonym_defs[key]) for key in sorted(synonym_defs)])

# Read in constants file, which must be output in every language.
Expand Down Expand Up @@ -123,6 +123,8 @@ def main():
'use strict';
const messages = Object.create(null);
""".format(target_lang.replace('-', '.')))
# For each key in the source language file, output the target value
# if present; otherwise, output the source language value with a
Expand All @@ -136,7 +138,7 @@ def main():
value = source_defs[key]
comment = ' // untranslated'
value = value.replace('"', '\\"')
outfile.write(u'Blockly.Msg["{0}"] = "{1}";{2}\n'
outfile.write(u'messages["{0}"] = "{1}";{2}\n'
.format(key, value, comment))

# Announce any keys defined only for target language.
Expand Down
16 changes: 16 additions & 0 deletions scripts/package/templates/umd-msg.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) { // AMD
define(<%= amd %>, factory);
} else if (typeof exports === 'object') { // Node.js
module.exports = factory();
} else { // Browser
var messages = factory();
for (var key in messages) {
root.<%= namespace %>[key] = messages[key];
}
}
}(this, function() {
<%= contents %>
return messages;
}));

0 comments on commit 188ba98

Please sign in to comment.