Skip to content

feat: right-to-left language support for the modern template and search - #11234

Open
soroshsabz wants to merge 4 commits into
dotnet:mainfrom
soroshsabz:fix/rtl-languages
Open

soroshsabz wants to merge 4 commits into
dotnet:mainfrom
soroshsabz:fix/rtl-languages

Conversation

@soroshsabz

@soroshsabz soroshsabz commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #11227, fixes #11228, fixes #11229, fixes #11230, fixes #11231, fixes #11232, fixes #11233

Summary

Pages in a right-to-left language are now rendered right-to-left, and search finds the words of every language.

Text direction (#11233)

  • The direction of a page is derived from its own _lang: pages in a right-to-left language such as Arabic, Hebrew and Persian get dir="rtl". The new _dir metadata sets the direction explicitly (rtl or ltr), e.g. for a language docfx doesn't know. _lang and _dir can also be set for a single file with file metadata or in the YAML header of a page, so an English page in a Persian docset is left-to-right. An invalid _dir is reported once per docset.
  • Only the modern template lays out right-to-left languages. The default template is not changed: it has no right-to-left layout, and giving its pages a dir attribute broke the code blocks of existing sites.
  • For these pages the modern template loads public/docfx.rtl.min.css. The template build derives it from the left-to-right stylesheet with rtlcss, as Bootstrap does for its own RTL stylesheet, and appends the few rules that must not be mirrored. Left-to-right pages load the same stylesheet as before.

Code and member names stay left-to-right (#11230)

  • Code blocks, inline code and URLs are isolated from the direction of the page.
  • API member names are laid out by their first character: dir="auto" in the templates, unicode-bidi: plaintext for links. Calculator() and object.GetType() keep their parentheses, and a name in a right-to-left script is still laid out right-to-left.

Letter joining of cursive scripts (#11231)

  • The "In this article" title no longer gets letter-spacing when it is written in a cursive script such as Arabic, whose letters are only joined without it.

Search (#11227, #11228, #11229)

  • lunr's default trimmer keeps [A-Za-z0-9_] only and turned every Persian, Cyrillic, … word into an empty string. It is replaced by a Unicode-aware trimmer. The multi-language trimmer dropped every word outside the configured languages in the same way, so it is replaced too.
  • The Arabic and Hebrew language packs keep the state of their stemmers on this. In a classic script that is the global object: in the ES module bundle, where this is undefined, they threw Cannot set properties of undefined (setting 'result'), and loading both packs made them overwrite each other's state. The template build now keeps the state in an object per pack.
  • An unsupported lunrLanguages entry such as fa is reported in the console instead of disabling search. A language pack that fails to load no longer disables the other languages.
  • Search indexes cached by an older version of docfx are rebuilt.

Translated strings (#11232)

  • "Table of Contents", "Toggle navigation", "Close", "Show table of contents", "View source" and the aria-labels of the search box, of the anchors and of the search pagination come from token.json.

Left-to-right sites are not affected: their pages get the same HTML (no dir), the same stylesheet and the same strings.

Tests

Unit tests:

  • TextDirectionTest: the direction from _dir, from _lang and from its script, and invalid values.
  • search-index.test.ts: the trimmer keeps every script, the default pipeline finds Persian, Cyrillic and CJK words, English stemming and stop words still apply, and an index can be saved and loaded.
  • search-languages.test.ts: the Arabic and Hebrew packs need the global object as their state and fail in strict mode; the patched packs work in strict mode and keep their state apart; unsupported languages are skipped with a warning; a pack that fails to load doesn't disable the other languages; the index analyzes the words of every loaded language and keeps the words of other languages.
  • helper.test.ts: right-to-left pages and cursive scripts.

System tests:

  • RtlLanguageTest drives a headless Chromium, like VersionLocaleSelectorTest does. It builds a Persian site and an English site from the same content, with an API page and a page whose file name is Persian, and checks in the browser: dir="rtl", the right stylesheet, code left-to-right with the braces in place, icons at the start of their inputs, joined letters, the bar of blockquotes, translated strings, the direction of previous/next, member names left-to-right and right-aligned, the inheritance chain and the table of contents. It also checks that the English site is unchanged. A second test searches Persian, Arabic and Hebrew in sites with different lunrLanguages.
  • TextDirectionBuildTest builds docsets for 12 language and direction combinations and checks the generated HTML, including pages that override the language or the direction, the warning for an invalid _dir, and that the default template is not changed.
  • The static file server of the selector tests is moved to BrowserTestServer to be shared. It now decodes URL-encoded paths, which pages with a Persian file name need.

Follow-ups from the review

  • The direction is now determined from the metadata of each page, not once for the docset. Before, a page that overrode _lang to a left-to-right language inside a Persian docset was still rendered right-to-left.
  • The default template no longer gets a dir attribute. Only the modern template has the styles for right-to-left pages, so the automatic direction broke code blocks on existing default-template sites.
  • The branch is rebased onto the current main.

Notes for reviewers

  • templates/modern/public is build output and is not committed.
  • rtlcss is a new devDependency of templates, the same range Bootstrap uses (^4.3.0).
  • docfx runs with invariant globalization, so the right-to-left languages are a built-in list (from ICU's culture data) instead of CultureInfo.
  • The 65 refreshed *.html.view.verified.json files are the view model snapshots of SamplesTest, which uses AutoVerify: each gains the new tokens in __global. This also adds the three tokens changeVersion, changeLocale and changeMoniker that feat: Built-in version, locale and moniker selectors for multi-version sites #11212 introduced in token.json but that were missing from the snapshots, and covers the 9 snapshots added by chore: Add samples of csharp15 syntax metadata extraction #11224. The *.pdf.verified.json snapshots are not changed.
  • I compared the seed site built with the templates of main and with this branch: all 202 pages have the same content, apart from three new loc: meta tags, the dir="auto" spans around API names and the "View Source" tooltip that now comes from token.json.

@soroshsabz

Copy link
Copy Markdown
Contributor Author

@vicancy Can you please review this PR

thanks :)

@soroshsabz

Copy link
Copy Markdown
Contributor Author

@soroshsabz

Copy link
Copy Markdown
Contributor Author

@soroshsabz

Copy link
Copy Markdown
Contributor Author

@soroshsabz

Copy link
Copy Markdown
Contributor Author

@vicancy Can you please review this PR

thanks :)

Comment thread src/Docfx.Build/DocumentBuilder.cs Resolved
Comment thread templates/default/layout/_master.tmpl Resolved
Pages in a right-to-left language such as Arabic, Hebrew or Persian are
rendered right-to-left. Docfx derives the text direction from `_lang`, or
from the new `_dir` metadata, writes `dir="rtl"` and the modern template
loads a mirrored stylesheet. Code and API member names stay left-to-right,
the directional icons point the other way, and the "In this article" title
no longer breaks the letter joining of cursive scripts.

Search finds the words of every language. lunr's ASCII-only trimmer is
replaced by a Unicode-aware one, the Arabic and Hebrew language packs are
patched so that they work in the ES module bundle and keep their state
apart, and an unsupported `lunrLanguages` entry no longer disables search.

The strings of the modern template that were hard-coded in English are
taken from token.json, and the view model snapshots follow.

Fixes dotnet#11227
Fixes dotnet#11228
Fixes dotnet#11229
Fixes dotnet#11230
Fixes dotnet#11231
Fixes dotnet#11232
Fixes dotnet#11233
The direction was determined once for the docset and written to its global
metadata as an explicit `_dir`. A page of a Persian docset that set `_lang`
to a left-to-right language was still rendered right-to-left.

The direction is now determined from the metadata of each page, after its
file metadata and YAML header are merged with the global metadata. The
global metadata is only checked for an invalid `_dir`, which is reported
once and removed.
The default template had been given the `dir` attribute too, without the
styles that keep code left-to-right on right-to-left pages: the code blocks
of existing sites with a right-to-left `_lang` were rendered incorrectly.

Only the modern template lays out right-to-left languages. The default
template is not changed.
The snapshots gain the tokens that this pull request adds to `__global`.
@soroshsabz

Copy link
Copy Markdown
Contributor Author

@vicancy Thanks for review this PR, I try to resolve all of your comments

Can you please review this PR again?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment