Skip to content

Commit

Permalink
add the ability to localize help pages for docs (#10307)
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Dec 5, 2024
1 parent 18681ce commit 0ecd214
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 @@ -1029,10 +1029,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 @@ -1998,3 +1997,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 0ecd214

Please sign in to comment.