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 “Get Started” #408

Open davidlewisbates1980 opened 1 month ago

davidlewisbates1980 commented 1 month ago

Is it possible to request the subtree from the locale bundle? For example, if I had my locale bundle look like this...

{
  questions: [
    "What is your name?",
    "How old are you?",  
  ]
}

...is there a way of getting all those questions into an object so that I can randomly select one?

const questions = getScopedI18NSubtree("questions");
console.log(questions[Math.random() ... ])
davidlewisbates1980 commented 1 month ago

(I see I can do something like this t("questions.1"), but that's not helpful if I don't know how many items are in the array)

davidlewisbates1980 commented 1 month ago

I suppose a workaround for that could be:

{
    ...
    questions: {
      count: "3",
      list: [
        "What is your name?",
        "What is your quest?",
        "What is the airspeed velocity of an unladen swallow?",
      ],
    },
}

...and then:

  const questionList = useScopedI18n("entity.questions");
  const numberOfQuestions = parseInt(questionList("count"));
  const randomQuestionIndex = Math.floor(Math.random() * numberOfQuestions);
  const randomQuestion = questionList(`list.${randomQuestionIndex}` as any); // Cast is necessary to avoid type error