Skip to content

Commit 7e6b986

Browse files
author
Ke, Mingze
committed
Tune translation
1 parent 58a0e4c commit 7e6b986

3 files changed

Lines changed: 35 additions & 37 deletions

File tree

code.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,8 @@ Code.init = function (toolbox) {
839839
ctx = Code.getContext();
840840

841841
launcher.loadTemplate('./templates/' + ctx.tpl + '.html', function (data) {
842+
data.body = launcher.translate(data.body, MSG);
843+
842844
if (ctx.jsPreprocessor === 'babel') {
843845
data.js = Code.transform(ctx.data.js);
844846
} else {
@@ -863,7 +865,6 @@ Code.init = function (toolbox) {
863865
var ctx = Code.getContext(),
864866
urls = Code.getUrlParts();
865867
urls.pop();
866-
ctx.lang = Code.LANG;
867868
localStorage.setItem(urls.join('/') + '/launcher.html', JSON.stringify(ctx));
868869
});
869870

@@ -1061,17 +1062,7 @@ Code.reloadSandbox = function () {
10611062
Code.sandboxLoaded = false;
10621063

10631064
launcher.loadTemplate('./templates/' + ctx.tpl + '.html', function (data) {
1064-
1065-
var $body = $('<div/>', {
1066-
html: data.body
1067-
});
1068-
// find if element has translation
1069-
$body.find('[data-translation]').each(function () {
1070-
// Translate string with locale file in /msg
1071-
$(this).html(window.MSG[$(this).data('translation')]).removeAttr('data-translation');
1072-
});
1073-
// covert element to string
1074-
data.body = $body.clone().wrap('<div/>').parent().html();
1065+
data.body = launcher.translate(data.body, MSG);
10751066

10761067
if (Code.running) {
10771068
if (ctx.jsPreprocessor === 'babel') {
@@ -1174,10 +1165,13 @@ Code.unhookEvents = function (window, frame) {
11741165
Code.getContext = function () {
11751166
var code = Blockly.JavaScript.workspaceToCode(Code.workspace),
11761167
babelize = code.indexOf('async function') !== -1,
1177-
page = Code.PAGE;
1168+
page = Code.PAGE,
1169+
lang = Code.LANG;
11781170

11791171
return {
1172+
page: page,
11801173
tpl: page === 'index' ? Code.getDemoPage() : page,
1174+
lang: lang,
11811175
modes: 'html,css,js,output',
11821176
data: {
11831177
js: code
@@ -1240,6 +1234,13 @@ Code.getBlockDemo = function (type) {
12401234
return null;
12411235
};
12421236

1237+
Code.getBlockTypes = function () {
1238+
var xml = Blockly.Xml.workspaceToDom(Code.workspace);
1239+
return slice.call(xml.querySelectorAll('block')).map(function (block) {
1240+
return block.getAttribute('type');
1241+
});
1242+
};
1243+
12431244
Blockly.JavaScript['procedures_defnoreturn'] = function (block) {
12441245
// Define a procedure with a return value.
12451246
var funcName = Blockly.JavaScript.variableDB_.getName(
@@ -1318,11 +1319,9 @@ Blockly.Xml._domToWorkspace = Blockly.Xml.domToWorkspace;
13181319

13191320
Blockly.Xml.domToWorkspace = function () {
13201321
Blockly.Xml._domToWorkspace.apply(this, arguments);
1321-
var xml = Blockly.Xml.workspaceToDom(Code.workspace);
1322-
var blocks = slice.call(xml.querySelectorAll('block'));
1323-
for (var i = 0; i < blocks.length; i++) {
1324-
var type = blocks[i].getAttribute('type');
1325-
var demo = Code.getBlockDemo(type);
1322+
var types = Code.getBlockTypes();
1323+
for (var i = 0; i < types.length; i++) {
1324+
var demo = Code.getBlockDemo(types[i]);
13261325
if (demo) {
13271326
if (demo !== Code.queryString.get('demo')) {
13281327
Code.queryString.set('demo', demo);

launcher.html

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,11 @@
2424

2525
// Load translation
2626
window.MSG = {};
27-
$('head').append('<script src="msg/' + tpl + '/' + config.lang + '.js' + '"><\/script>');
28-
29-
// Translate string in head
30-
var $head = $('<div/>', {
31-
html: data.head
32-
});
33-
$head.find('[data-translation]').each(function () {
34-
$(this).html(window.MSG[$(this).data('translation')]).removeAttr('data-translation');
35-
});
36-
data.head = $head.clone().wrap('<div/>').html();
37-
38-
// Translate string in body
39-
var $body = $('<div/>', {
40-
html: data.body
41-
});
42-
$body.find('[data-translation]').each(function () {
43-
$(this).html(window.MSG[$(this).data('translation')]).removeAttr('data-translation');
44-
});
45-
data.body = $body.clone().wrap('<div/>').html();
27+
$('head').append('<script src="msg/' + config.lang + '.js' + '"><\/script>');
28+
if (config.page !== 'index') {
29+
$('head').append('<script src="msg/' + config.page + '/' + config.lang + '.js' + '"><\/script>');
30+
}
31+
data.body = launcher.translate(data.body, window.MSG);
4632

4733
$.extend(config.data, data);
4834
launcher.jsbin(config);

launcher.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@
7272
form.submit();
7373
}
7474

75+
function translate(html, msg) {
76+
var $wrap = $('<div/>', {
77+
html: html
78+
});
79+
80+
$wrap.find('[data-translation]').each(function () {
81+
$(this).html(msg[$(this).data('translation')]).removeAttr('data-translation');
82+
});
83+
84+
return $wrap.wrap('<div/>').parent().html();
85+
}
86+
7587
var launchers = {
7688
jsfiddle: function (config) {
7789
var data = config.data;
@@ -144,6 +156,7 @@
144156

145157
window.launcher = {
146158
loadTemplate: loadTemplate,
159+
translate: translate,
147160
jsfiddle: launchers.jsfiddle,
148161
codepen: launchers.codepen,
149162
jsbin: launchers.jsbin,

0 commit comments

Comments
 (0)