Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to localize help pages for docs #10307

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading