refactor(core): Use Intl native API to get locale direction, remove rtl-detect depend… (#10798)

This commit is contained in:
Sébastien Lorber 2024-12-26 15:44:07 +01:00 committed by GitHub
parent 092238d0fa
commit e7a8c9db82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 14 deletions

View file

@ -66,7 +66,6 @@
"react-router": "^5.3.4",
"react-router-config": "^5.1.1",
"react-router-dom": "^5.3.4",
"rtl-detect": "^1.0.4",
"semver": "^7.5.4",
"serve-handler": "^6.1.6",
"shelljs": "^0.8.5",
@ -84,7 +83,6 @@
"@types/detect-port": "^1.3.3",
"@types/react-dom": "^18.2.7",
"@types/react-router-config": "^5.0.7",
"@types/rtl-detect": "^1.0.0",
"@types/serve-handler": "^6.1.4",
"@types/update-notifier": "^6.0.4",
"@types/webpack-bundle-analyzer": "^4.7.0",

View file

@ -6,7 +6,6 @@
*/
import logger from '@docusaurus/logger';
import {getLangDir} from 'rtl-detect';
import type {I18n, DocusaurusConfig, I18nLocaleConfig} from '@docusaurus/types';
import type {LoadContextParams} from './site';
@ -69,11 +68,21 @@ function getDefaultCalendar(localeStr: string) {
return 'gregory';
}
function getDefaultDirection(localeStr: string) {
const locale = new Intl.Locale(localeStr);
// see https://github.com/tc39/proposal-intl-locale-info
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo
// Node 18.0 implements a former version of the getTextInfo() proposal
// @ts-expect-error: The TC39 proposal was updated
const textInto = locale.getTextInfo?.() ?? locale.textInfo;
return textInto.direction;
}
export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
try {
return {
label: getDefaultLocaleLabel(locale),
direction: getLangDir(locale),
direction: getDefaultDirection(locale),
htmlLang: locale,
calendar: getDefaultCalendar(locale),
path: locale,