Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loading messages from script tags. #6184

Merged
merged 7 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Comment on lines -328 to -329
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dependencies because messages don't actually depend on 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;
}));