{lang.i18n === "ta" ? (
[
- x,
- Langs[x as keyof typeof Langs],
- ]) as Key[];
+/**
+ * Component providing the language selection menu.
+ */
+export const Languages = observer(() => {
+ const locale = useApplicationState().locale;
+ const language = locale.getLanguage();
- // Get the user's system language. Check for exact
- // matches first, otherwise check for partial matches
- const preferredLanguage =
- navigator.languages.filter((lang) =>
- languages.find((l) => l[0].replace(/_/g, "-") == lang),
- )?.[0] ||
- navigator.languages
- ?.map((x) => x.split("-")[0])
- ?.filter((lang) => languages.find((l) => l[0] == lang))?.[0]
- ?.split("-")[0];
+ // Generate languages array.
+ const languages = useMemo(() => {
+ const languages = Object.keys(Langs).map((x) => [
+ x,
+ Langs[x as keyof typeof Langs],
+ ]) as Key[];
- if (preferredLanguage) {
- // This moves the user's system language to the top of the language list
- const prefLangKey = languages.find(
- (lang) => lang[0].replace(/_/g, "-") == preferredLanguage,
- );
- if (prefLangKey) {
- languages.splice(
- 0,
- 0,
- languages.splice(languages.indexOf(prefLangKey), 1)[0],
+ // Get the user's system language. Check for exact
+ // matches first, otherwise check for partial matches
+ const preferredLanguage =
+ navigator.languages.filter((lang) =>
+ languages.find((l) => l[0].replace(/_/g, "-") == lang),
+ )?.[0] ||
+ navigator.languages
+ ?.map((x) => x.split("-")[0])
+ ?.filter((lang) => languages.find((l) => l[0] == lang))?.[0]
+ ?.split("-")[0];
+
+ if (preferredLanguage) {
+ // This moves the user's system language to the top of the language list
+ const prefLangKey = languages.find(
+ (lang) => lang[0].replace(/_/g, "-") == preferredLanguage,
);
+
+ if (prefLangKey) {
+ languages.splice(
+ 0,
+ 0,
+ languages.splice(languages.indexOf(prefLangKey), 1)[0],
+ );
+ }
}
- }
+
+ return languages;
+ }, []);
+
+ // Creates entries with given key.
+ const EntryFactory = ([x, lang]: Key) => (
+
locale.setLanguage(x)}
+ />
+ );
return (
@@ -98,11 +126,7 @@ export function Component(props: Props) {
- {languages
- .filter(([, lang]) => !lang.cat)
- .map(([x, lang]) => (
-
- ))}
+ {languages.filter(([, lang]) => !lang.cat).map(EntryFactory)}
@@ -110,9 +134,7 @@ export function Component(props: Props) {
{languages
.filter(([, lang]) => lang.cat === "const")
- .map(([x, lang]) => (
-
- ))}
+ .map(EntryFactory)}
@@ -120,9 +142,7 @@ export function Component(props: Props) {
{languages
.filter(([, lang]) => lang.cat === "alt")
- .map(([x, lang]) => (
-
- ))}
+ .map(EntryFactory)}
@@ -137,10 +157,4 @@ export function Component(props: Props) {
);
-}
-
-export const Languages = connectState(Component, (state) => {
- return {
- locale: state.locale,
- };
});