lauriahlfors / kieli-nextjs-i18n

Next.js 14 project with internationalization (i18n) using the app router, without i18n libraries.
27 stars 5 forks source link

Not working #4

Open kb-0912 opened 8 months ago

kb-0912 commented 8 months ago
Unhandled Runtime Error
Error: translations[locale] is not a function

Source
src/lib/i18n/loadTranslation.ts (32:29) @ loadTranslation

  30 |
  31 |
> 32 | return translations[locale]();
     |                           ^
  33 | }
  34 |
  35 |
Call Stack
locale
src/lib/i18n/getTranslation.ts (15:44)
params
src/app/layout.tsx (24:43)

Can you help me explain why?

ObaidQatan commented 7 months ago

I encountered the same thing. For some reason, that translations object in loadTranslation util doesn't load in order, returning undefined instead of the corresponding loader function.

To solve this you need to check if the loader is undefined, if so, return the default loader function:

// /path/to/loadTranslation.ts
export default async function loadTranslation(
  locale: Locale
): Promise<Translation> {
  // Invoke a call to translations corresponding to a given locale key.
  if (translations[locale]) {
    return translations[locale]();
  }
  return import("@/translations/ar.json").then((module) => module.default);
}

Instead of:

export default async function loadTranslation(
  locale: Locale
): Promise<Translation> {
  // Invoke a call to translations corresponding to a given locale key.
  return translations[locale]();
}

Let me know if this helps.

ObaidQatan commented 6 months ago

@kb-0912 Hi, did that work for you? if yes, you may mark this issue as closed.

kb-0912 commented 2 months ago

@kb-0912 Hi, did that work for you? if yes, you may mark this issue as closed.

Hi, with your suggestion it always return default locale

ObaidQatan commented 2 months ago

@kb-0912 Hi, did that work for you? if yes, you may mark this issue as closed.

Hi, with your suggestion it always return default locale

I've raised a pr on this repo, could u try pulling it and let me know afterwards? I wrote the exact code to be added/modified there.

lauriahlfors commented 2 months ago

Hi, @kb-0912 and @ObaidQatan! Sorry it has taken me so long to respond to your messages! I wanted to let you know that I am going to update this project up to date with Next.js 14 very soon. This should fix most of the bugs and simplify the code.

Cheers!

ObaidQatan commented 2 months ago

I came across the issue in Next.js 14, in case the issue persisted in your case even after the update, you may consider this pr.

kb-0912 commented 2 months ago

@kb-0912 Hi, did that work for you? if yes, you may mark this issue as closed.

Hi, with your suggestion it always return default locale

I've raised a pr on this repo, could u try pulling it and let me know afterwards? I wrote the exact code to be added/modified there.

Thank you let me check

kb-0912 commented 2 months ago

Hi, @kb-0912 and @ObaidQatan! Sorry it has taken me so long to respond to your messages! I wanted to let you know that I am going to update this project up to date with Next.js 14 very soon. This should fix most of the bugs and simplify the code.

Cheers!

Thanks for your reply. May I know the expected date?