From 5c0d80f04826c181a18f951053c08f8d0924bdfe Mon Sep 17 00:00:00 2001 From: Yueyu Date: Sat, 31 Jul 2021 09:27:18 +0800 Subject: [PATCH] localize sample name in storyStarter mode --- src/editor/ScratchJr.js | 2 +- src/editor/ui/UI.js | 8 ++++++-- src/lobby/Samples.js | 5 +---- src/utils/Localization.js | 10 ++++++++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/editor/ScratchJr.js b/src/editor/ScratchJr.js index dda1e3c..eb02bec 100644 --- a/src/editor/ScratchJr.js +++ b/src/editor/ScratchJr.js @@ -355,7 +355,7 @@ export default class ScratchJr { if (ScratchJr.isEditable() && editmode == 'storyStarter' && storyStarted && !Project.error) { OS.analyticsEvent('samples', 'story_starter_edited', Project.metadata.name); // Localize sample project names - var sampleName = Localization.localize('SAMPLE_' + Project.metadata.name); + var sampleName = Localization.localizeSampleName(Project.metadata.name); // Get the new project name IO.uniqueProjectName({ name: sampleName diff --git a/src/editor/ui/UI.js b/src/editor/ui/UI.js index 67e159f..271c2d4 100644 --- a/src/editor/ui/UI.js +++ b/src/editor/ui/UI.js @@ -339,7 +339,7 @@ export default class UI { if (ScratchJr.isEditable() && ScratchJr.editmode == 'storyStarter' && !Project.error && ScratchJr.changed) { OS.analyticsEvent('samples', 'story_starter_edited', Project.metadata.name); // Get the new project name - var sampleName = Localization.localize('SAMPLE_' + Project.metadata.name); + var sampleName = Localization.localizeSampleName(Project.metadata.name); IO.uniqueProjectName({ name: sampleName }, function (jsonData) { @@ -423,7 +423,11 @@ export default class UI { } if (ScratchJr.isEditable()) { - (document.forms.projectname.myproject).value = Project.metadata.name; + var name = Project.metadata.name; + if (ScratchJr.editmode == 'storyStarter') { + name = Localization.localizeSampleName(name); + } + (document.forms.projectname.myproject).value = name; } else { gn('pname').textContent = Project.metadata.name; } diff --git a/src/lobby/Samples.js b/src/lobby/Samples.js index 82ae417..b4ab232 100644 --- a/src/lobby/Samples.js +++ b/src/lobby/Samples.js @@ -52,10 +52,7 @@ export default class Samples { var name = mt.childNodes[1]; // Localize sample project names - var sampleName = data.name; - sampleName = Localization.localize('SAMPLE_' + sampleName); - - name.textContent = sampleName; + name.textContent = Localization.localizeSampleName(data.name); var cnv = mt.childNodes[0].childNodes[1]; Samples.insertThumbnail(cnv, data.thumbnail); mt.onclick = function (evt) { diff --git a/src/utils/Localization.js b/src/utils/Localization.js index 6e95b81..894425f 100644 --- a/src/utils/Localization.js +++ b/src/utils/Localization.js @@ -112,6 +112,16 @@ export default class Localization { return 'String missing: ' + key; } + static localizeSampleName (key) { + var name = Localization.localizeOptional('SAMPLE_' + key); + // If the localized name is still started with 'SAMPLE_', + // it is not a preset name. + if (name.startsWith('SAMPLE_')) { + return key; + } + return name; + } + // Translate a particular message given the message key and info; // if key not found, assume it's just a raw text string without a translation, // and return that