QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.19k stars 52 forks source link

Feedback for “Setup” #403

Open davidlewisbates1980 opened 1 month ago

davidlewisbates1980 commented 1 month ago

Firstly, I'm absolutely loving this library. Everything was very intuitive. However, I'm trying to build a menu in React from an array of objects and the strong typing of next-international is causing a problem. It doesn't like me passing non-literal string into t(...):

  const t = await getI18n();
  const strArray = [
    "menu.profileOverview.title",
    "menu.profileOverview.companyDetails",
  ];
  const translated = strArray.map((s) => t(s));
  console.log(translated);

It first of all says Expected 2 arguments, but got 1, but when I add an empty object as a second parameter, it says it can't find an overloaded version with that signature.

Is there a way around this? The best I've found is to enumerate the possible string array values:

  const strArray: (
    | "menu.profileOverview.title"
    | "menu.companyDetails.title"
  )[] = ["menu.profileOverview.title", "menu.companyDetails.title"];`

Anything neater you can suggest?

TimurKr commented 1 month ago

I am not sure this will help, but I choose one language as a primary and export the type of the translations from it. Then I use this Type to check all other translations are the same using satisfies and also I can import it elsewhere in my codebase and get the types from it. Here is a code snippet of how I used this type to infer the correct type.

export const questions = [
  'How are you?',
  'What is your height?',
  'What is your name?',
] satisfies (keyof Messages['exampleQuestions']['questions'])[];

Here Messages is the type of the object defined in my translation file.