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

Improve locale text direction detection mechanism #1987

Merged
merged 3 commits into from
Jun 9, 2024

Conversation

HiDeoo
Copy link
Member

@HiDeoo HiDeoo commented Jun 9, 2024

Description

The newly locale text direction detection mechanism introduced in Starlight 0.24.0 is based on the v8 implementation of getTextInfo() where it is implemented as an accessor property (textInfo).

#1961 and #1986 surfaced that this broke in Bun which respects the spec and implements getTextInfo() as a method.

This is also an issue when running Starlight in a WebContainers, e.g. StackBlitz, where the implementation can vary between browsers.

  • v8 based browsers do not follow the spec and implement getTextInfo() as an accessor property.
  • Safari properly uses getTextInfo() as a method.
  • Firefox does not support getTextInfo() or textInfo.

To handle with all these cases, the new version will use the following mechanism to detect the text direction:

  1. Try to access textInfo as an accessor property.
  2. If it fails, fallback to getTextInfo() as a method.
  3. If it fails, fallback to a well-known list of RTL locales.

Here are some screenshots of the checks in the Node.js and Bun REPLs:

SCR-20240609-lhsm SCR-20240609-libc

Regarding the list of well-known RTL locales, I picked all the RTL languages from this list of common primary language subtags.

Copy link

changeset-bot bot commented Jun 9, 2024

🦋 Changeset detected

Latest commit: 53860e4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jun 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
starlight ✅ Ready (Inspect) Visit Preview Jun 9, 2024 11:14am

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Jun 9, 2024
Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for diving into this @HiDeoo. Left a couple of comments but should be good to merge with or without addressing these if you want.

*/
function getLocaleDir(locale: Intl.Locale): 'ltr' | 'rtl' {
if ('textInfo' in locale) {
// @ts-expect-error - `textInfo` is typed but is available in v8 based environments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing word in this comment I think:

Suggested change
// @ts-expect-error - `textInfo` is typed but is available in v8 based environments.
// @ts-expect-error - `textInfo` is typed but is only available in v8 based environments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I think I removed a negation while trying to "simplify" the comment. Just added it back and I think this makes sense without the only but let me know if it's still confusing.

// @ts-expect-error - `textInfo` is typed but is available in v8 based environments.
return locale.textInfo.direction;
} else if ('getTextInfo' in locale) {
// @ts-expect-error - `getTextInfo` is typed but is available in some non-v8 based environments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too? Now I’m doubting what you meant, but I think it’s this?

Suggested change
// @ts-expect-error - `getTextInfo` is typed but is available in some non-v8 based environments.
// @ts-expect-error - `getTextInfo` is typed but is only available in some non-v8 based environments.

packages/starlight/utils/i18n.ts Show resolved Hide resolved
@delucis delucis added the 🌟 patch Change that triggers a patch release label Jun 9, 2024
Co-authored-by: Chris Swithinbank <[email protected]>
Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments make sense to me now! Thanks again @HiDeoo 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to get locale informations
2 participants