Skip to content

Commit 3202403

Browse files
committed
feat: added rtl attribute to scripts
2 parents 392e516 + f758c27 commit 3202403

5 files changed

Lines changed: 24 additions & 9 deletions

File tree

components/language-chooser/common/find-language/findLanguageInterfaces.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface IRegion {
66
export interface IScript {
77
code: string;
88
name: string;
9+
isRtl?: boolean;
910
languageNameInScript?: string;
1011
}
1112

@@ -56,3 +57,10 @@ export interface IOrthography {
5657
script?: IScript;
5758
customDetails?: ICustomizableLanguageDetails;
5859
}
60+
61+
export function isRTLScript(bcp47: string): boolean {
62+
const locale = new Intl.Locale(bcp47).maximize();
63+
// getTextInfo is the standardized property; textInfo is the older name
64+
const info = locale.getTextInfo?.() ?? (locale as any).textInfo;
65+
return info?.direction === "rtl";
66+
}

components/language-chooser/common/find-language/searchResultModifiers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ function modifyAkaLanguageResult(
8888
return {
8989
...result,
9090
scripts: [
91-
{ code: "Latn", name: "Latin", languageNameInScript: "Akan" },
91+
{
92+
code: "Latn",
93+
name: "Latin",
94+
languageNameInScript: "Akan",
95+
},
9296
{ code: "Arab", name: "Arabic" },
9397
{ code: "Brai", name: "Braille" },
9498
],

components/language-chooser/react/common/language-chooser-react-hook/useLanguageChooser.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ILanguage,
33
IScript,
4+
isRTLScript,
45
asyncSearchForLanguage,
56
ICustomizableLanguageDetails,
67
deepStripDemarcation,
@@ -238,10 +239,11 @@ export const useLanguageChooser = (
238239
script: selectedScript,
239240
customDetails: customizableLanguageDetails,
240241
}) as IOrthography;
241-
onSelectionChange(
242-
resultingOrthography,
243-
createTagFromOrthography(resultingOrthography)
244-
);
242+
const tag = createTagFromOrthography(resultingOrthography);
243+
if (resultingOrthography.script) {
244+
resultingOrthography.script.isRtl = isRTLScript(tag);
245+
}
246+
onSelectionChange(resultingOrthography, tag);
245247
setPreviousStateWasValidSelection(true);
246248
} else if (previousStateWasValidSelection) {
247249
onSelectionChange(undefined, undefined);

components/language-chooser/react/language-chooser-react-mui/src/CustomizeLanguageDialog.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ export const CustomizeLanguageDialog: React.FunctionComponent<{
8989
// name (dialect) and country (region) are required for unlisted language
9090
const isReadyToSubmit =
9191
!isUnlistedLanguageDialog ||
92-
(formatDialectCode(dialogSelectedDialect) !== "" &&
93-
!!dialogSelectedRegion);
92+
(formatDialectCode(dialogSelectedDialect) !== "" && !!dialogSelectedRegion);
9493

9594
const theme = useTheme();
9695

@@ -425,7 +424,7 @@ export const CustomizeLanguageDialog: React.FunctionComponent<{
425424
region: dialogSelectedRegion,
426425
dialect: isUnlistedLanguageDialog
427426
? normalizedDialect
428-
: dialogSelectedDialect
427+
: dialogSelectedDialect,
429428
} as ICustomizableLanguageDetails,
430429
dialogSelectedScript
431430
);

components/language-chooser/react/language-chooser-react-mui/src/demos/DialogDemo.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ export const DialogDemo: React.FunctionComponent<{
124124
<br />
125125
Language Code: {selectedValue?.language?.languageSubtag}
126126
<br />
127-
Script: {selectedValue?.script?.name}
127+
Script:
128+
{selectedValue?.script &&
129+
` ${selectedValue?.script?.name} (${selectedValue?.script?.isRtl ? "RTL" : "LTR"})`}
128130
<br />
129131
Region: {selectedValue?.customDetails?.region?.name}
130132
<br />

0 commit comments

Comments
 (0)