Skip to content

Commit

Permalink
add the ability to localize help pages for docs
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Dec 5, 2024
1 parent 405808c commit 717f6b6
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions webapp/src/blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

0 comments on commit 717f6b6

Please sign in to comment.