QuiiBz / next-international

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

When no translation in zh-TW, want to use translation in zh.ts, not en (defaultLocale) #278

Open mengxi-ream opened 1 year ago

mengxi-ream commented 1 year ago

Is your feature request related to a problem? Please describe. I have a website which has three languages en.ts, zh.ts and zh-TW.ts. When translation is not available in zh-TW, actually the best way for users is to show them translation in zh. but now when I set

const I18nMiddleware = createI18nMiddleware({
  locales: ["en", "zh", "zh-TW"],
  defaultLocale: "en",
});

When translation is not available in zh-TW.ts, it always give me the word from en.ts

Describe the solution you'd like When translation is not available in zh-TW, show user translation in zh

For example:

// en.ts
export default {
  hello: "Hello",
  "hello.world": "Hello world!",
  welcome: "Hello {name}!",
} as const;
// zh.ts
export default {
  hello: "你好",
  "hello.world": "你好,世界!",
  welcome: "你好 {name}!",
} as const;
// zh-TW.ts
export default {
  // hello: "妳好",
  "hello.world": "妳好,世界!",
  welcome: "妳好 {name}!",
} as const;

When I have this component

// [local]/page.tsx
import { getI18n, getScopedI18n } from "@/locales/server";

export default async function Page() {
  const t = await getI18n();
  const scopedT = await getScopedI18n("hello");
  return (
    <div>
      <p>{t("hello")}</p>
    </div>
  );
}

in url http://localhost:3000/zh-TWit should return me 你好, not Hello. (Now it returns me Hello)

Describe alternatives you've considered No

Additional context No

mengxi-ream commented 1 year ago

I implement the logic in another lib next-intl https://github.com/amannn/next-intl/issues/88#issuecomment-1791936419 maybe can be a inspiration

QuiiBz commented 1 year ago

Does the fallbackLocale option in createI18nClient / createI18nServer helps in this situation?

https://github.com/QuiiBz/next-international/blob/68bc9b147c85ce29a9e7d717cfa3ae52ddb1db90/examples/next-app/locales/client.ts#L22

Or do you wish to be able to define the fallback locale per locale, instead of having a single fallback locale for all defined locales?

mengxi-ream commented 1 year ago

Does the fallbackLocale option in createI18nClient / createI18nServer helps in this situation?

https://github.com/QuiiBz/next-international/blob/68bc9b147c85ce29a9e7d717cfa3ae52ddb1db90/examples/next-app/locales/client.ts#L22

Or do you wish to be able to define the fallback locale per locale, instead of having a single fallback locale for all defined locales?

https://github.com/amannn/next-intl/issues/88#issuecomment-1791936419

Is using this way now, looks like fine?

QuiiBz commented 1 year ago

It looks fine but should be unnecessary since that's exactly what fallbackLocale is made for.