From 717f6b6ed4cc96fff86919d137ab8e1a16992fc6 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Thu, 5 Dec 2024 11:27:04 -0800 Subject: [PATCH] add the ability to localize help pages for docs --- webapp/src/blocks.tsx | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/webapp/src/blocks.tsx b/webapp/src/blocks.tsx index a8bd9c96d9a1..514248e409b3 100644 --- a/webapp/src/blocks.tsx +++ b/webapp/src/blocks.tsx @@ -1034,10 +1034,9 @@ export class Editor extends toolboxeditor.ToolboxEditor { if (/^github:/.test(url)) { // strip 'github:', add '.md' file extension if necessary url = url.replace(/^github:\/?/, '') + (/\.md$/i.test(url) ? "" : ".md"); - const readme = pkg.getEditorPkg(pkg.mainPkg).lookupFile(url); - const readmeContent = readme?.content?.trim(); - if (readmeContent) { - this.parent.setSideMarkdown(readmeContent); + const content = resolveLocalizedMarkdown(url); + if (content) { + this.parent.setSideMarkdown(content); this.parent.setSideDocCollapsed(false); } } else if (/^\//.test(url)) { @@ -2003,3 +2002,38 @@ function shouldEventHideFlyout(ev: Blockly.Events.Abstract) { return true; } + +function resolveLocalizedMarkdown(url: string) { + const editorPackage = pkg.getEditorPkg(pkg.mainPkg); + + const [initialLang, baseLang, initialLangLowerCase] = pxt.Util.normalizeLanguageCode(pxt.Util.userLanguage()); + + const splitPath = url.split("/"); + const fileName = splitPath.pop(); + const dirName = splitPath.join("/"); + + let pathsToTest: string[]; + + if (initialLang && baseLang && initialLangLowerCase) { + pathsToTest = [ + `${dirName}/_locales/${initialLang}/${fileName}`, + `${dirName}/_locales/${initialLangLowerCase}/${fileName}`, + `${dirName}/_locales/${baseLang}/${fileName}`, + url + ]; + } + else { + pathsToTest = [url]; + } + + for (const path of pathsToTest) { + const file = editorPackage.lookupFile(path); + const content = file?.content?.trim(); + + if (content) { + return content; + } + } + + return undefined; +} \ No newline at end of file