QuiiBz / next-international

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

Separate locale file for each module or multiple locale files #267

Open 3adel-bassiony opened 11 months ago

3adel-bassiony commented 11 months ago

Is your feature request related to a problem? Please describe. When using next-international in a big project with support for multiple languages it's hard to add all locales in one file, so I think it will be better if we have an option to divide the locale file into multiple locale files, so each feature or module can have a separate locale file.

Describe the solution you'd like Divide the locale files into multiple ts files so we can use a different locale file for each module

QuiiBz commented 11 months ago

Indeed, that's currently one of the limitation of next-international.

so we can use a different locale file for each module

Could you describe what "module" means in this context? Are you referring to a JavaScript module?

irfancoder commented 11 months ago

Could you describe what "module" means in this context? Are you referring to a JavaScript module?

I think he is referring to namespaces, like in i18next. In my previous work, we group the translations by route, and load the namespaces as necessary. I agree with OP this is a very much a needed feature +1

3adel-bassiony commented 11 months ago

@irfancoder Yes that's correct, I mean something like Namespaces in i18next @QuiiBz In this case I can divide the locale file into multiple files and work on them easily

QuiiBz commented 11 months ago

Thanks for the details! This feature certainly translates to APIs breaking change/new APIs to support this behaviour. I'll take a look at how other libraries have designed this feature, so feel free to also share your thoughts!

olafurns7 commented 11 months ago

@QuiiBz just to give you a term of scope, in our use case we have ~7000 translation strings, in 14 locales :)

chungweileong94 commented 11 months ago

This is kinda related to this issue/question #180.

Wrote a simple repo myself to test out this library, and this is what I found:

aabmets commented 10 months ago

@3adel-bassiony @QuiiBz Do you mean something like this? This works.

image image image

QuiiBz commented 10 months ago

@aabmets not really, because we want to only load the necessary locale file when needed. If you split your locales per page, you only want to load the locales for this page.

aabmets commented 10 months ago

@QuiiBz is it possible to define all localizations in one place (like the translations folder i have) and then implement this feature, that next-i11l would be able to only import a subset of these localization files with const t = await getI18n(['ns1', 'ns2'])?

QuiiBz commented 10 months ago

Not right now, but that's the goal of this issue, yes - but the API would probably be different, and in order to keep loading only the necessary file, you wouldn't have any index.ts which returns every locale file.

Meierschlumpf commented 10 months ago

Not sure about the performance, but maybe something like this would already be enough: image

I've got full typesafety for the defined namespace. But of course I'm also creating a new instance of I18nClient whenever I call the useI18n method

Meierschlumpf commented 10 months ago

Nevermind it's more complicated :joy:

gustaveWPM commented 5 months ago

The more I think about it, the more I think we should be able to write the object of a locale file with both inlined translations and lazy-loaded fragments (which would be loaded just-in-time).

Unfortunately, I'm afraid this would cause problems on the client side...