Skip to content

Commit f758c27

Browse files
committed
feat: added rtl attribute to scripts
1 parent 1a203b9 commit f758c27

7 files changed

Lines changed: 40 additions & 12 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+
rtl: 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(scriptCode: string): boolean {
62+
const locale = new Intl.Locale(`und-${scriptCode}`);
63+
// getTextInfo is the standardized property; textInfo is the older name
64+
const info = locale.getTextInfo?.() ?? locale.textInfo;
65+
return info?.direction === "rtl";
66+
}

components/language-chooser/common/find-language/language-data/languageData.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { iso31661 } from "iso-3166";
22
import { iso15924 } from "iso-15924";
3-
import { ILanguage, IRegion, IScript } from "./findLanguageInterfaces";
3+
import {
4+
ILanguage,
5+
IRegion,
6+
IScript,
7+
isRTLScript,
8+
} from "./findLanguageInterfaces";
49

510
// ISO-3166-1 is a region code to region name lookup
611
export function getAllRegions(): IRegion[] {
@@ -31,6 +36,7 @@ export function getAllScripts(): IScript[] {
3136
return {
3237
name: script.name,
3338
code: script.code,
39+
rtl: isRTLScript(script.code),
3440
} as IScript;
3541
});
3642
}
@@ -54,6 +60,7 @@ export function getScriptForLanguage(
5460
return {
5561
name: scriptInfo.name,
5662
code: scriptInfo.code,
63+
rtl: isRTLScript(scriptInfo.code),
5764
} as IScript;
5865
}
5966
return undefined;

components/language-chooser/common/find-language/scripts/langtagProcessing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IScript, ILanguage } from "../findLanguageInterfaces";
1+
import { IScript, ILanguage, isRTLScript } from "../findLanguageInterfaces";
22
import {
33
getAllPossibleNames,
44
isMacrolanguage,
@@ -57,6 +57,7 @@ function addOrCombineLangtagsEntry(
5757
langs[entry.indivIsoCode].scripts[entry.script] = {
5858
code: entry.script,
5959
name: scriptNames[entry.script],
60+
rtl: isRTLScript(entry.script),
6061
languageNameInScript:
6162
(entry.localnames || [undefined])[0] ||
6263
langs[entry.indivIsoCode].scripts[entry.script]
@@ -85,6 +86,7 @@ function addOrCombineLangtagsEntry(
8586
scripts[scriptCode] = {
8687
code: scriptCode,
8788
name: scriptNames[scriptCode],
89+
rtl: isRTLScript(scriptCode),
8890
languageNameInScript:
8991
(entry.localnames || [undefined])[0] || entry.localname,
9092
} as IScript;

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const DEFAULT_EXCLUDED_SCRIPT_CODES = new Set([
3737
"Zzzz",
3838
]);
3939

40-
const latinScriptData = { code: "Latn", name: "Latin" } as IScript;
40+
const latinScriptData = { code: "Latn", name: "Latin", rtl: false } as IScript;
4141

4242
function replaceIsoCode(
4343
origIsoCode: string,
@@ -88,9 +88,14 @@ function modifyAkaLanguageResult(
8888
return {
8989
...result,
9090
scripts: [
91-
{ code: "Latn", name: "Latin", languageNameInScript: "Akan" },
92-
{ code: "Arab", name: "Arabic" },
93-
{ code: "Brai", name: "Braille" },
91+
{
92+
code: "Latn",
93+
name: "Latin",
94+
rtl: false,
95+
languageNameInScript: "Akan",
96+
},
97+
{ code: "Arab", name: "Arabic", rtl: true },
98+
{ code: "Brai", name: "Braille", rtl: false },
9499
],
95100
isMacrolanguage: false,
96101
parentMacrolanguage: undefined,
@@ -170,11 +175,13 @@ function modifyChineseResult(results: ILanguage[]): ILanguage[] {
170175
{
171176
code: "Hans",
172177
name: "Chinese (Simplified)",
178+
rtl: false,
173179
languageNameInScript: "中文",
174180
} as IScript,
175181
{
176182
code: "Hant",
177183
name: "Chinese (Traditional)",
184+
rtl: false,
178185
languageNameInScript: "中文",
179186
} as IScript,
180187
{ ...latinScriptData, languageNameInScript: "Chinese" } as IScript,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function getAllScriptOptions() {
5050
return {
5151
label: script.name,
5252
id: script.code,
53+
rtl: script.rtl,
5354
};
5455
});
5556
}
@@ -89,8 +90,7 @@ export const CustomizeLanguageDialog: React.FunctionComponent<{
8990
// name (dialect) and country (region) are required for unlisted language
9091
const isReadyToSubmit =
9192
!isUnlistedLanguageDialog ||
92-
(formatDialectCode(dialogSelectedDialect) !== "" &&
93-
!!dialogSelectedRegion);
93+
(formatDialectCode(dialogSelectedDialect) !== "" && !!dialogSelectedRegion);
9494

9595
const theme = useTheme();
9696

@@ -209,16 +209,18 @@ export const CustomizeLanguageDialog: React.FunctionComponent<{
209209
value={{
210210
label: dialogSelectedScript?.name || "",
211211
id: dialogSelectedScript?.code || "",
212+
rtl: dialogSelectedScript?.rtl || false,
212213
}}
213214
onChange={(
214215
_event,
215-
newValue: { label: string; id: string } | null
216+
newValue: { label: string; id: string; rtl: boolean } | null
216217
) => {
217218
setDialogSelectedScript(
218219
newValue
219220
? ({
220221
code: newValue.id,
221222
name: newValue.label,
223+
rtl: newValue.rtl,
222224
} as IScript)
223225
: undefined
224226
);
@@ -425,7 +427,7 @@ export const CustomizeLanguageDialog: React.FunctionComponent<{
425427
region: dialogSelectedRegion,
426428
dialect: isUnlistedLanguageDialog
427429
? normalizedDialect
428-
: dialogSelectedDialect
430+
: dialogSelectedDialect,
429431
} as ICustomizableLanguageDetails,
430432
dialogSelectedScript
431433
);

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?.rtl ? "RTL" : "LTR"})`}
128130
<br />
129131
Region: {selectedValue?.customDetails?.region?.name}
130132
<br />

0 commit comments

Comments
 (0)