gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
159 stars 37 forks source link

Support ICU format for plurals #98

Closed joeldenning closed 4 years ago

joeldenning commented 4 years ago

Is your feature request related to a problem? Please describe.

I am using Transifex for storing all of my translations, which only supports ICU Format for pluralization.

I would love if babel-plugin-18next-extract supported an option to generate the ICU format for plurals instead of i18next's default format.

Describe the solution you'd like

What this means is that instead of generating this:

{
  "itemCount": "{{count}} item",
  "itemCount_plural": "{{count}} items"
}

I want this to be generated:

{
  "itemCount": "{count, plural, one {{count} item} other {count} items}}"
}

I suggest that a new boolean option, icuPlurals, be added to support this.

Describe alternatives you've considered

I've considered just living with the i18next format that has multiple keys instead of the one key with ICU format. This is not ideal because the Transifex translation UI does not know how to handle those keys as a plural string, but it DOES know how to handle the ICU string as plurals. Transifex presents a special UI to translators when the ICU format is correctly presented.

Additional context

joeldenning commented 4 years ago

After some searching through the code, I think that the following code is what would be need to be modified:

https://github.com/gilbsgilbs/babel-plugin-i18next-extract/blob/81addcfbb9241477a1403093f7d8f6c73d9a297b/src/keys.ts#L118-L157

To generate the correct string, we could either write the code right into this babel plugin, or perhaps use a package such as https://github.com/messageformat/messageformat that already deals with the ICU format.

gilbsgilbs commented 4 years ago

Sorry about the misclick 😥.

This issue may be related to #60.

I'd like to have proper support for ICU somehow because it definitely seems to have some traction, yet I know nothing about this format and I have the feeling that plurals aren't the only requirement to have a good-enough support for ICU (and again, I'm quite ignorant on this format so I could be wrong). Would contexts, namespaces and the Babel macro work properly with your suggestion? Or do they already work?

To be honest, I'm not sure I really want to maintain the ICU part myself as I don't want to miss fundamental things that would require breaking changes everytime, and I don't want to handle issues and PRs related to it while having very poor knowledge about it. I'd be much more comfortable should this was possible through some kind of external plug-in that is not maintained by me.

Any thoughts?

joeldenning commented 4 years ago

I appreciate your quick response

Would contexts, namespaces and the Babel macro work properly with your suggestion? Or do they already work?

To be honest, I'm not sure I really want to maintain the ICU part myself as I don't want to miss fundamental things that would require breaking changes everytime, and I don't want to handle issues and PRs related to it while having very poor knowledge about it.

I totally get this. Maintaining ICU stuff would probably be hard.

I'd be much more comfortable should this was possible through some kind of external plug-in that is not maintained by me.

Is there a way to extend/override the extractor and exporter? I see that the published code is all in one file - I can't think of a way to do this. Do you know of a way?

gilbsgilbs commented 4 years ago

Thanks for all the insights on how ICU works with i18next. :+1:

Is there a way to extend/override the extractor and exporter? I see that the published code is all in one file - I can't think of a way to do this. Do you know of a way?

You're right, it's not possible yet unfortunately. Such interface is quite demanded actually, but I'm not sure I'd get it right first try.

joeldenning commented 4 years ago

Such interface is quite demanded actually, but I'm not sure I'd get it right first try.

Hmm well I'm very keen on trying to get ICU support for plurals working with this. I'm open to making a PR to add direct support or discussing the creation of a middleware or extension api for people wanting to create their own babel plugin or modify yours to do so.

How should we proceed?

gilbsgilbs commented 4 years ago

I sincerely apologize for the delay @joeldenning . I'm kind of overwhelmed by work and personal things right now, but I should have responded to you earlier :cold_sweat: .

My rationale on this is the following:

joeldenning commented 4 years ago

👍 great I will explore putting a PR together for this today and see how much it would take to support ICU plurals in this plugin. If it turns out to be too intensive, I'll turn my attention to #110 and look to implement this as a middleware.

cc @fatmali, @brandones, and @jonathandick - this is an important issue for OpenMRS to easily and correctly support pluralization with Transifex. I'm going to look through it and see what I can do with it, but ultimately it might fall on you to push it forward if I cannot.