mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
fix: loading messages from script tags. (#6184)
* 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
This commit is contained in:
@@ -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)
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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`));
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
@@ -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
|
||||
@@ -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.
|
||||
|
||||
16
scripts/package/templates/umd-msg.template
Normal file
16
scripts/package/templates/umd-msg.template
Normal file
@@ -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;
|
||||
}));
|
||||
Reference in New Issue
Block a user