diff --git a/cli/crowdin.ts b/cli/crowdin.ts index 6a4d79a8b9e5..2097fa7357ab 100644 --- a/cli/crowdin.ts +++ b/cli/crowdin.ts @@ -153,6 +153,7 @@ export async function buildAllTranslationsAsync(fetchFileTranslationAsync: (file await buildTranslationFilesAsync(["target-strings.json"], "target-strings.json"); await buildTranslationFilesAsync(["strings.json"], "strings.json", true); await buildTranslationFilesAsync(["skillmap-strings.json"], "skillmap-strings.json", true); + await buildTranslationFilesAsync(["tutorialtool-strings.json"], "tutorialtool-strings.json", true); await buildTranslationFilesAsync(["webstrings.json"], "webstrings.json", true); const files: string[] = []; diff --git a/docs/static/tutorial-tool/tutorial.js b/docs/static/tutorial-tool/tutorial.js index 9fbe11a12bcd..f767512a9e16 100644 --- a/docs/static/tutorial-tool/tutorial.js +++ b/docs/static/tutorial-tool/tutorial.js @@ -299,7 +299,7 @@ function loadIframe(selected) { for (var _a = 0, _b = target.endpoints; _a < _b.length; _a++) { var endpoint = _b[_a]; if (!selected || selected === target.name + "-" + endpoint.name) { - iframe.setAttribute("src", endpoint.url); + iframe.setAttribute("src", withLocale(endpoint.url)); selectedEndpoint = target.name + "-" + endpoint.name; selectedId = target.id; selectedTarget = target; @@ -310,3 +310,19 @@ function loadIframe(selected) { // Load first target loadIframe(null); } +// The legacy tool does not load the PXT localization runtime, so mirror only +// explicit language requests when it embeds an editor. The editor keeps its +// own saved-language and target-default fallback otherwise. +function withLocale(endpoint) { + var url = new URL(endpoint, window.location.href); + if (url.searchParams.has("lang") || url.searchParams.has("forcelang")) + return url.toString(); + var hostUrl = new URL(window.location.href); + var hostParams = hostUrl.searchParams; + var hashParams = new URLSearchParams(hostUrl.hash.slice(1)); + var forcedLanguage = hostParams.get("forcelang") || hashParams.get("forcelang"); + var requestedLanguage = forcedLanguage || hostParams.get("lang") || hashParams.get("lang"); + if (requestedLanguage) + url.searchParams.set(forcedLanguage ? "forcelang" : "lang", requestedLanguage); + return url.toString(); +} diff --git a/docs/static/tutorial-tool/tutorial.ts b/docs/static/tutorial-tool/tutorial.ts index c6f68311e816..b742fe753ad4 100644 --- a/docs/static/tutorial-tool/tutorial.ts +++ b/docs/static/tutorial-tool/tutorial.ts @@ -332,7 +332,7 @@ function loadIframe(selected: string) { for (const target of targets) { for (const endpoint of target.endpoints) { if (!selected || selected === `${target.name}-${endpoint.name}`) { - iframe.setAttribute("src", endpoint.url); + iframe.setAttribute("src", withLocale(endpoint.url)); selectedEndpoint = `${target.name}-${endpoint.name}`; selectedId = target.id; selectedTarget = target; @@ -343,4 +343,21 @@ function loadIframe(selected: string) { // Load first target loadIframe(null); -} \ No newline at end of file +} + +// The legacy tool does not load the PXT localization runtime, so mirror its +// language selection inputs when it embeds an editor. +function withLocale(endpoint: string): string { + const url = new URL(endpoint, window.location.href); + if (url.searchParams.has("lang") || url.searchParams.has("forcelang")) return url.toString(); + + const hostUrl = new URL(window.location.href); + const hostParams = hostUrl.searchParams; + const hashParams = new URLSearchParams(hostUrl.hash.slice(1)); + const forcedLanguage = hostParams.get("forcelang") || hashParams.get("forcelang"); + const requestedLanguage = forcedLanguage || hostParams.get("lang") || hashParams.get("lang"); + if (requestedLanguage) { + url.searchParams.set(forcedLanguage ? "forcelang" : "lang", requestedLanguage); + } + return url.toString(); +} diff --git a/gulpfile.js b/gulpfile.js index 318bce678253..d579afcc9c86 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -718,6 +718,13 @@ const teacherTool = createWebappTasks("teachertool"); *********************************************************/ const tutorialTool = createWebappTasks("tutorialtool"); +const buildTutorialToolTests = () => compileTsProject("tutorialtool/tests", "built/tests", true); +const runTutorialToolTests = () => exec(`${getMochaExecutable()} built/tests/tests/iframeUrl.spec.js built/tests/tests/legacyIframeUrl.spec.js built/tests/tests/localizationExtraction.spec.js --reporter dot`, true); +const testTutorialTool = gulp.series( + () => updateWebappStrings("tutorialtool"), + buildTutorialToolTests, + runTutorialToolTests +); /******************************************************** Webapp build wrappers @@ -816,6 +823,7 @@ const testAll = gulp.series( testlanguageservice, karma, testSkillmap, + testTutorialTool, testpxteditor, testprojecttools, testcli @@ -927,7 +935,9 @@ exports.pxtrunner = gulp.series( exports.pxtweb = pxtweb; exports.pxtlib = pxtlib; exports.skillmapTest = testSkillmap; +exports.tutorialtoolTest = testTutorialTool; exports.updatestrings = updatestrings; +exports.tutorialtoolstrings = () => updateWebappStrings("tutorialtool"); exports.lint = lint exports.testdecompiler = testdecompiler; exports.testlang = testlang; diff --git a/pxtlib/util.ts b/pxtlib/util.ts index aefd800fed46..a1505ff94ec6 100644 --- a/pxtlib/util.ts +++ b/pxtlib/util.ts @@ -1197,6 +1197,7 @@ namespace ts.pxtc.Util { baseUrl: string; code: string; force?: boolean; + translationKind?: TranslationsKind; } export function updateLocalizationAsync(opts: LocalizationUpdateOptions): Promise { @@ -1218,7 +1219,7 @@ namespace ts.pxtc.Util { const liveUpdateStrings = pxt.Util.liveLocalizationEnabled() return downloadTranslationsAsync(targetId, baseUrl, code, liveUpdateStrings, - ts.pxtc.Util.TranslationsKind.Editor) + opts.translationKind || ts.pxtc.Util.TranslationsKind.Editor) .then((translations) => { if (translations) { setUserLanguage(code); @@ -1226,6 +1227,12 @@ namespace ts.pxtc.Util { setLocalizedStrings(translations); } + // Tutorial Tool has no API surface, so avoid delaying its startup + // with an unused API catalog request. + if (opts.translationKind === ts.pxtc.Util.TranslationsKind.TutorialTool) { + return Promise.resolve(); + } + // Download api translations return ts.pxtc.Util.downloadTranslationsAsync( targetId, baseUrl, code, liveUpdateStrings, @@ -1243,7 +1250,8 @@ namespace ts.pxtc.Util { Editor, Sim, Apis, - SkillMap + SkillMap, + TutorialTool } export function downloadTranslationsAsync(targetId: string, baseUrl: string, code: string, live: boolean, translationKind?: TranslationsKind): Promise> { @@ -1278,6 +1286,13 @@ namespace ts.pxtc.Util { { staticName: "skillmap-strings.json", path: "/skillmap-strings.json" }, ]; break; + case TranslationsKind.TutorialTool: + stringFiles = [ + { staticName: "strings.json", path: "strings.json" }, + { staticName: "target-strings.json", path: targetId + "/target-strings.json" }, + { staticName: "tutorialtool-strings.json", path: "/tutorialtool-strings.json" }, + ]; + break; } let translations: pxt.Map; function mergeTranslations(tr: pxt.Map) { diff --git a/tests/pxt-editor-test/editorrunner.ts b/tests/pxt-editor-test/editorrunner.ts index 6728eb5ce505..381e78214df6 100644 --- a/tests/pxt-editor-test/editorrunner.ts +++ b/tests/pxt-editor-test/editorrunner.ts @@ -680,6 +680,33 @@ describe("simulator themes", () => { ]); }); + it("loads shared, target, and Tutorial Tool translations", async () => { + const requestedUrls: string[] = []; + const originalHttpGetJsonAsync = pxt.Util.httpGetJsonAsync; + pxt.Util.httpGetJsonAsync = (url: string) => { + requestedUrls.push(url); + return Promise.resolve({} as T); + }; + + try { + await pxt.Util.downloadTranslationsAsync( + "arcade", + "https://example.com/", + "de", + false, + ts.pxtc.Util.TranslationsKind.TutorialTool + ); + } finally { + pxt.Util.httpGetJsonAsync = originalHttpGetJsonAsync; + } + + chai.expect(requestedUrls).to.include.members([ + "https://example.com/locales/de/strings.json", + "https://example.com/locales/de/target-strings.json", + "https://example.com/locales/de/tutorialtool-strings.json", + ]); + }); + it("initializes an empty cloud-synced simulator theme map", () => { chai.expect(pxt.auth.DEFAULT_USER_PREFERENCES().simulatorThemes).deep.equals({}); }); diff --git a/tutorialtool/src/components/MakeCodeFrame.tsx b/tutorialtool/src/components/MakeCodeFrame.tsx index 4e00820774d4..91b6c7c1f8d2 100644 --- a/tutorialtool/src/components/MakeCodeFrame.tsx +++ b/tutorialtool/src/components/MakeCodeFrame.tsx @@ -2,6 +2,8 @@ import * as React from "react"; import css from "./styling/MakeCodeFrame.module.scss"; import { setEditorRef } from "../services/makecodeEditorService"; import { getEditorUrl } from "../utils"; +import { createTutorialToolEditorUrl } from "../utils/iframeUrl"; +import { getLocaleParameter, getRequestedLocale } from "../utils/localization"; export interface MakeCodeFrameProps { } @@ -16,8 +18,7 @@ export const MakeCodeFrame = (props: MakeCodeFrameProps) => { if (editorUrl.charAt(editorUrl.length - 1) === "/" && !pxt.BrowserUtils.isLocalHost()) { url = editorUrl.substr(0, editorUrl.length - 1); } - url += `?controller=1&teachertool=1&ws=mem&nocookiebanner=1`; - return url; + return createTutorialToolEditorUrl(url, window.location.href, getRequestedLocale() || pxt.Util.userLanguage(), getLocaleParameter()); } const handleIframeRef = React.useCallback((ref: HTMLIFrameElement) => { @@ -30,6 +31,7 @@ export const MakeCodeFrame = (props: MakeCodeFrameProps) => {