Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ export interface IOrthography {
// isRtl setting to match its IScript in every case, which can accomplish
// with und-{script}.
export function isRTLScript(scriptCode: string): boolean {
const locale = new Intl.Locale(`und-${scriptCode}`);
// getTextInfo is the standardized property; textInfo is the older name
const info = locale.getTextInfo?.() ?? (locale as any).textInfo;
return info?.direction === "rtl";
try {
const locale = new Intl.Locale(`und-${scriptCode}`);
// getTextInfo is the standardized property; textInfo is the older name
const info = locale.getTextInfo?.() ?? (locale as any).textInfo;
return info?.direction === "rtl";
} catch {
// An unrecognized/malformed script code makes Intl.Locale throw. Such a
// script has no known RTL direction, so treat it as not RTL.
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type IRegion,
type IScript,
isManuallyEnteredTagLanguage,
isRTLScript,
isUnlistedLanguage,
isValidBcp47Tag,
languageForManuallyEnteredTag,
Expand Down Expand Up @@ -118,7 +119,7 @@ export function useLanguageChooserViewModel(
if (selectedLang.scripts.length === 1) {
// Automatically select a language's only script
_setScriptList([]);
selectedScript.value = selectedLang.scripts[0];
selectedScript.value = scriptWithReadingDirection(selectedLang.scripts[0]);
} else {
_setScriptList(selectedLang.scripts);
}
Expand All @@ -135,7 +136,9 @@ export function useLanguageChooserViewModel(

function _onScriptSelected(index: number) {
selectItem(index, listedScripts.value);
selectedScript.value = listedScripts.value[index].script;
selectedScript.value = scriptWithReadingDirection(
listedScripts.value[index].script
);
_onOrthographyChanged();
}

Expand Down Expand Up @@ -271,7 +274,9 @@ export function useLanguageChooserViewModel(
region?: IRegion;
dialect?: string;
}) {
selectedScript.requestUpdate(script);
selectedScript.requestUpdate(
script ? scriptWithReadingDirection(script) : script
);
customizations.requestUpdate({
region,
dialect,
Expand Down Expand Up @@ -308,6 +313,13 @@ export function useLanguageChooserViewModel(
};
}

// Returns a copy of the script with its reading direction (isRtl) populated,
// so consumers receive the direction as part of the selected orthography.
// Mirrors the behavior of the React useLanguageChooser hook.
function scriptWithReadingDirection(script: IScript): IScript {
return { ...script, isRtl: isRTLScript(script.code) };
}

function hasValidDisplayName(selection: IOrthography) {
if (!selection.language) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ describe("selected script", () => {
test.viewModel.listedLanguages.value[0].isSelected.requestUpdate(true);
test.viewModel.listedScripts.value[0].isSelected.requestUpdate(true);

expect(test.viewModel.selectedScript.value).toEqual(
NorthernUzbekLanguage.scripts[0]
);
expect(test.viewModel.selectedScript.value).toEqual({
...NorthernUzbekLanguage.scripts[0],
isRtl: false,
});
});

it("should be undefined after script deselected", () => {
Expand Down Expand Up @@ -366,9 +367,20 @@ describe("selected script", () => {

test.viewModel.listedLanguages.value[0].isSelected.requestUpdate(true);

expect(test.viewModel.selectedScript.value).toEqual(
WaataLanguage.scripts[0]
);
expect(test.viewModel.selectedScript.value).toEqual({
...WaataLanguage.scripts[0],
isRtl: false,
});
});

it("should populate isRtl for a right-to-left script", () => {
const test = new TestHelper({ initialLanguages: [NorthernUzbekLanguage] });

test.viewModel.listedLanguages.value[0].isSelected.requestUpdate(true);
// NorthernUzbekLanguage.scripts[1] is Arabic, a right-to-left script
test.viewModel.listedScripts.value[1].isSelected.requestUpdate(true);

expect(test.viewModel.selectedScript.value?.isRtl).toBe(true);
});
});

Expand Down Expand Up @@ -626,7 +638,9 @@ describe("customize language modal", () => {
scriptViewModel.isSelected.requestUpdate(true);
t.viewModel.onCustomizeButtonClicked();

expect(spy).toHaveBeenCalledWith({ script: scriptViewModel.script });
expect(spy).toHaveBeenCalledWith({
script: { ...scriptViewModel.script, isRtl: false },
});
});

it("populates with dialect when custom dialect was selected", () => {
Expand Down Expand Up @@ -706,6 +720,7 @@ describe("customize language modal", () => {
expect(t.viewModel.selectedScript.value).toEqual({
code: "abc",
name: "ABC Script",
isRtl: false,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@

<div>
<div class="text-base-content/60">Script</div>
<div>{orthography.script?.name || "-"}</div>
<div>
{#if orthography.script}
{orthography.script.name} ({orthography.script.isRtl
? "RTL"
: "LTR"})
{:else}
-
{/if}
</div>
</div>

<div>
Expand Down
Loading