All files / client/src/utils i18n.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 0/0
100% Lines 4/4

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 42 43 44 45 46 47 48 49 50 51 52 53            360x   360x                   360x                                                             360x      
import i18next from "i18next";
import languageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";
import { translationEN } from "client/locales/en";
import { translationFI } from "client/locales/fi";
 
export const defaultNS = "ns1";
 
export const resources = {
  en: {
    ns1: translationEN,
  },
  fi: {
    ns1: translationFI,
  },
};
 
// eslint-disable-next-line @typescript-eslint/no-floating-promises
i18next
  .use(languageDetector)
  .use(initReactI18next)
  .init({
    resources,
    fallbackLng: "en", // use if detected lng is not available
    interpolation: {
      escapeValue: false, // react escapes by default -> safe from xss
    },
    detection: {
      order: ["localStorage"],
    },
    defaultNS,
    returnNull: false,
  });
 
// https://dev.to/pffigueiredo/typescript-utility-keyof-nested-object-2pa3
type NestedKeyOf<ObjectType extends object> = {
  [Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
    ? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}`
    : `${Key}`;
}[keyof ObjectType & (string | number)];
 
type Equals<X, Y> = [X, Y] extends [Y, X] ? true : false;
 
/* eslint-disable @typescript-eslint/no-unused-vars */
 
// If following gives type error, EN and FI language files don't match
const expectLocalesToMatch: Equals<
  NestedKeyOf<typeof translationEN>,
  NestedKeyOf<typeof translationFI>
> = true;
 
/* eslint-enable @typescript-eslint/no-unused-vars */