next-international icon indicating copy to clipboard operation
next-international copied to clipboard

Separate locale file for each module or multiple locale files

Open 3adel-bassiony opened this issue 1 year ago • 13 comments

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

3adel-bassiony avatar Oct 23 '23 10:10 3adel-bassiony

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?

QuiiBz avatar Oct 23 '23 15:10 QuiiBz

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

irfancoder avatar Oct 23 '23 18:10 irfancoder

@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

3adel-bassiony avatar Oct 24 '23 08:10 3adel-bassiony

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!

QuiiBz avatar Oct 24 '23 16:10 QuiiBz

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

olafurns7 avatar Oct 25 '23 11:10 olafurns7

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:

  • Bundle size is not an issue on server component, but it might be an issue while loading huge locale files? 🤷
  • Bundle size will be an issue on client component, all the locale files for the selected language will be loaded.

chungweileong94 avatar Nov 06 '23 13:11 chungweileong94

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

image image image

aabmets avatar Nov 16 '23 23:11 aabmets

@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.

QuiiBz avatar Nov 17 '23 07:11 QuiiBz

@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'])?

aabmets avatar Nov 17 '23 07:11 aabmets

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.

QuiiBz avatar Nov 17 '23 07:11 QuiiBz

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 avatar Dec 09 '23 15:12 Meierschlumpf

Nevermind it's more complicated :joy:

Meierschlumpf avatar Dec 09 '23 16:12 Meierschlumpf

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...

gustaveWPM avatar May 08 '24 11:05 gustaveWPM