All files / client/src/components LanguageSelector.tsx

90.9% Statements 10/11
100% Branches 0/0
100% Functions 3/3
90.9% Lines 10/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                608x 841x 841x   841x 12x   841x       12x     12x 12x     841x                            
import { TFunction } from "i18next";
import { ChangeEvent, ReactElement } from "react";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { Locale } from "shared/types/locale";
import { setLocale } from "shared/utils/setLocale";
import { Dropdown } from "client/components/Dropdown";
 
export const LanguageSelector = (): ReactElement => {
  const { t, i18n } = useTranslation();
  const language = i18n.language;
 
  const toggle = async (lng: string): Promise<TFunction> =>
    await i18n.changeLanguage(lng);
 
  const setLanguage = (event: ChangeEvent<HTMLSelectElement>): void => {
    // The dropdown only offers the two options below, so anything else falls
    // back to English rather than reaching the formatters as a language they
    // have no locale for
    const locale = z.enum(Locale).catch(Locale.EN).parse(event.target.value);
 
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    toggle(locale);
    setLocale(locale);
  };
 
  const options = [
    { value: Locale.EN, title: t("language.englishShort") },
    { value: Locale.FI, title: t("language.finnishShort") },
  ];
 
  return (
    <Dropdown
      id="language"
      selectedValue={language}
      onChange={setLanguage}
      options={options}
    />
  );
};