QuiiBz / next-international

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

ensure all the locale files are of the same type #384

Closed addlistener closed 3 months ago

addlistener commented 3 months ago

I want to check no newly added i18n messages are missing in all locale files before publish.

In the typescript sense, aka, ensure all the locale files are of the same type. ( And I really like the idea of leveraging typescript to do the type-lifting. Kudos to @QuiiBz 💯 )

// en.ts
export {
  a: 1,
  // may add sth new here like
  // newStr: 'aaa'
} as const;
// fr.ts
export {
  a: 2
} as const;

typeof en results { a: 1 } as const will also consider the 1 as enum. and it's not the same as typeof fr

And I don't want to do { "a": 1 as number } as const because there's a lot of unnecessary typing.

QuiiBz commented 3 months ago

We have a utility function defineLocale that comes from createI18n that you might be able to use here - though it's known that it is not perfect:

// locales/index.ts
export const { defineLocale, ... } = createI18n(...)

// locales/fr.ts
export default defineLocale({
  hello: 'world',
})
addlistener commented 3 months ago
image

TS2353: Object literal may only specify known properties, and '"product"' does not exist in type '{ "product.slogan": LocaleValue; "dash.leftnav.sites": LocaleValue; "dash.leftnav.account": LocaleValue; "dash.sites.Untitled": LocaleValue; ... 102 more ...; "spinner.Uploading assets": LocaleValue; }'.

This is what I get. "fr" is like the same structure with "en" but missing some property

image
addlistener commented 3 months ago

I found this is already resolved when you enforce strict type check like tsc --noEmit before building. LGTM!