Skip to content

Commit d0ac647

Browse files
authored
Merge pull request #70 from sillsdev/BL-14355_fix_preloaded_langs
fix: BL-14355 fix preloaded langs inconsistency (#70)
2 parents e763d84 + f6f9974 commit d0ac647

File tree

6 files changed

+101
-38
lines changed

6 files changed

+101
-38
lines changed

components/language-chooser/common/find-language/searchForLanguage.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,15 @@ describe("getLanguageBySubtag", () => {
189189
expect(getLanguageBySubtag("aaa")?.exonym).toEqual("Ghotuo");
190190
expect(getLanguageBySubtag("ab")?.exonym).toEqual("Abkhaz");
191191
});
192+
it("should use searchResultModifier if provided", () => {
193+
const foobar = "foobar";
194+
const modifier = (
195+
results: FuseResult<ILanguage>[],
196+
_searchString: string
197+
) =>
198+
results.map((result) => {
199+
return { ...result.item, exonym: foobar };
200+
});
201+
expect(getLanguageBySubtag("en", modifier)?.exonym).toEqual(foobar);
202+
});
192203
});

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,19 @@ export function searchForLanguage(
9999
}
100100

101101
// get language (not macrolanguage) with exact match on subtag
102-
export function getLanguageBySubtag(code: string): ILanguage | undefined {
102+
export function getLanguageBySubtag(
103+
code: string,
104+
searchResultModifier?: (
105+
results: FuseResult<ILanguage>[],
106+
searchString: string
107+
) => ILanguage[]
108+
): ILanguage | undefined {
103109
const fuse = new Fuse(languages as ILanguage[], {
104110
keys: ["languageSubtag", "iso639_3_code"],
105111
threshold: 0, // exact matches only
106112
});
107-
const results = fuse.search(code);
108-
return results[0]?.item;
113+
const rawResults = fuse.search(code);
114+
return searchResultModifier
115+
? searchResultModifier(rawResults, code)[0]
116+
: rawResults[0]?.item;
109117
}

components/language-chooser/react/common/language-chooser-react-hook/languageTagHandling.spec.ts

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
UNLISTED_LANGUAGE,
1010
isUnlistedLanguage,
1111
} from "./languageTagHandling";
12-
import { getRegionBySubtag, LanguageType } from "@ethnolib/find-language";
12+
import {
13+
getRegionBySubtag,
14+
ILanguage,
15+
LanguageType,
16+
} from "@ethnolib/find-language";
17+
import { FuseResult } from "fuse.js";
1318
describe("Tag parsing", () => {
1419
it("should find a language by 2 letter language subtag", () => {
1520
expect(parseLangtagFromLangChooser("ja")?.language?.exonym).toEqual(
@@ -120,27 +125,43 @@ describe("Tag parsing", () => {
120125
ssh_Arab_x_foobar_result?.customDetails?.region?.name
121126
).toBeUndefined();
122127
expect(ssh_Arab_x_foobar_result?.customDetails?.dialect).toEqual("foobar");
123-
});
124128

125-
const ssh_AE_x_foobar_result = parseLangtagFromLangChooser("ssh-AE-x-foobar");
126-
expect(ssh_AE_x_foobar_result?.language?.exonym).toEqual("Shihhi Arabic");
127-
expect(ssh_AE_x_foobar_result?.script?.name).toEqual("Arabic");
128-
expect(ssh_AE_x_foobar_result?.customDetails?.region?.name).toEqual(
129-
"United Arab Emirates"
130-
);
131-
expect(ssh_AE_x_foobar_result?.customDetails?.dialect).toEqual("foobar");
129+
const ssh_AE_x_foobar_result =
130+
parseLangtagFromLangChooser("ssh-AE-x-foobar");
131+
expect(ssh_AE_x_foobar_result?.language?.exonym).toEqual("Shihhi Arabic");
132+
expect(ssh_AE_x_foobar_result?.script?.name).toEqual("Arabic");
133+
expect(ssh_AE_x_foobar_result?.customDetails?.region?.name).toEqual(
134+
"United Arab Emirates"
135+
);
136+
expect(ssh_AE_x_foobar_result?.customDetails?.dialect).toEqual("foobar");
132137

133-
const ssh_Arab_AE_x_foobar_result = parseLangtagFromLangChooser(
134-
"ssh-Arab-AE-x-foobar"
135-
);
136-
expect(ssh_Arab_AE_x_foobar_result?.language?.exonym).toEqual(
137-
"Shihhi Arabic"
138-
);
139-
expect(ssh_Arab_AE_x_foobar_result?.script?.name).toEqual("Arabic");
140-
expect(ssh_Arab_AE_x_foobar_result?.customDetails?.region?.name).toEqual(
141-
"United Arab Emirates"
142-
);
143-
expect(ssh_Arab_AE_x_foobar_result?.customDetails?.dialect).toEqual("foobar");
138+
const ssh_Arab_AE_x_foobar_result = parseLangtagFromLangChooser(
139+
"ssh-Arab-AE-x-foobar"
140+
);
141+
expect(ssh_Arab_AE_x_foobar_result?.language?.exonym).toEqual(
142+
"Shihhi Arabic"
143+
);
144+
expect(ssh_Arab_AE_x_foobar_result?.script?.name).toEqual("Arabic");
145+
expect(ssh_Arab_AE_x_foobar_result?.customDetails?.region?.name).toEqual(
146+
"United Arab Emirates"
147+
);
148+
expect(ssh_Arab_AE_x_foobar_result?.customDetails?.dialect).toEqual(
149+
"foobar"
150+
);
151+
});
152+
it("uses searchResultModifier if provided", () => {
153+
const foobar = "foobar";
154+
const modifier = (
155+
results: FuseResult<ILanguage>[],
156+
_searchString: string
157+
) =>
158+
results.map((result) => {
159+
return { ...result.item, exonym: foobar };
160+
});
161+
expect(
162+
parseLangtagFromLangChooser("en", modifier)?.language?.exonym
163+
).toEqual(foobar);
164+
});
144165
});
145166

146167
describe("defaultRegionForLangTag", () => {
@@ -162,6 +183,18 @@ describe("defaultRegionForLangTag", () => {
162183
"Uzbekistan"
163184
);
164185
});
186+
187+
it("uses searchResultModifier if provided", () => {
188+
// If using a search result modifier that filters out all results, we should no longer find a region
189+
const searchResultModifier = (
190+
_results: FuseResult<ILanguage>[],
191+
_searchString: string
192+
): ILanguage[] => {
193+
return [];
194+
};
195+
expect(defaultRegionForLangTag("uz")).not.toBeUndefined();
196+
expect(defaultRegionForLangTag("uz", searchResultModifier)).toBeUndefined();
197+
});
165198
});
166199

167200
describe("createTagFromOrthography", () => {

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
IScript,
1212
LanguageType,
1313
} from "@ethnolib/find-language";
14+
import { FuseResult } from "fuse.js";
1415

1516
const UNLISTED_LANGUAGE_CODE = "qaa";
1617
export const UNLISTED_LANGUAGE = {
@@ -100,7 +101,11 @@ export function createTagFromOrthography(orthography: IOrthography): string {
100101
// undefined if it encounters any of these, e.g. in cases where a langtag was manually
101102
// entered in the language chooser.
102103
export function parseLangtagFromLangChooser(
103-
languageTag: string // must be the default language subtag for the language
104+
languageTag: string, // must be the default language subtag for the language
105+
searchResultModifier?: (
106+
results: FuseResult<ILanguage>[],
107+
searchString: string
108+
) => ILanguage[]
104109
): IOrthography | undefined {
105110
const parts = languageTag.split(/-[xX]-/);
106111
const privateUseSubtag = parts[1];
@@ -115,7 +120,7 @@ export function parseLangtagFromLangChooser(
115120
if (isUnlistedLanguage) {
116121
language = UNLISTED_LANGUAGE;
117122
} else {
118-
language = getLanguageBySubtag(languageSubtag || "");
123+
language = getLanguageBySubtag(languageSubtag || "", searchResultModifier);
119124
}
120125
if (!language) {
121126
console.log(
@@ -195,9 +200,18 @@ export function parseLangtagFromLangChooser(
195200
} as IOrthography;
196201
}
197202

198-
export function defaultRegionForLangTag(languageTag: string) {
203+
export function defaultRegionForLangTag(
204+
languageTag: string,
205+
searchResultModifier?: (
206+
results: FuseResult<ILanguage>[],
207+
searchString: string
208+
) => ILanguage[]
209+
): IRegion | undefined {
199210
// if languageTag already has a region tag in it, use that
200-
const orthography = parseLangtagFromLangChooser(languageTag);
211+
const orthography = parseLangtagFromLangChooser(
212+
languageTag,
213+
searchResultModifier
214+
);
201215
if (orthography?.customDetails?.region) {
202216
return orthography.customDetails.region;
203217
}
@@ -212,6 +226,9 @@ export function defaultRegionForLangTag(languageTag: string) {
212226
getMaximalLangtag(`${languageSubtag}-${scriptSubtag}`) ||
213227
getMaximalLangtag(`${languageSubtag}`) ||
214228
"";
215-
const maximalTagOrthography = parseLangtagFromLangChooser(maximalTag);
229+
const maximalTagOrthography = parseLangtagFromLangChooser(
230+
maximalTag,
231+
searchResultModifier
232+
);
216233
return maximalTagOrthography?.customDetails?.region;
217234
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ export const useLanguageChooser = (
9494
if (!selectionLanguageTag) return;
9595

9696
let initialSelections = parseLangtagFromLangChooser(
97-
selectionLanguageTag || ""
97+
selectionLanguageTag || "",
98+
searchResultModifier
9899
);
99100
if (selectionLanguageTag && !initialSelections) {
100101
// we failed to parse the tag, meaning this is a langtag requiring manual entry

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import {
1212

1313
import "../styles.css";
1414
import { ILanguageChooserProps, LanguageChooser } from "../LanguageChooser";
15-
import {
16-
IOrthography,
17-
parseLangtagFromLangChooser,
18-
} from "@ethnolib/language-chooser-react-hook";
15+
import { IOrthography } from "@ethnolib/language-chooser-react-hook";
1916
import React from "react";
2017

2118
export const LanguageChooserDialog: React.FunctionComponent<
@@ -34,12 +31,8 @@ export const LanguageChooserDialog: React.FunctionComponent<
3431
dialogHeight,
3532
...languageChooserProps
3633
}) => {
37-
const initialSelection: IOrthography | undefined =
38-
parseLangtagFromLangChooser(
39-
languageChooserProps.initialSelectionLanguageTag || ""
40-
);
4134
const [pendingSelection, setPendingSelection] = React.useState(
42-
initialSelection || ({} as IOrthography)
35+
{} as IOrthography
4336
);
4437
const [pendingLanguageTag, setPendingLanguageTag] = React.useState(
4538
languageChooserProps.initialSelectionLanguageTag || ""

0 commit comments

Comments
 (0)