Giter Club home page Giter Club logo

Comments (7)

joeldenning avatar joeldenning commented on September 13, 2024 1

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

from babel-plugin-i18next-extract.

joeldenning avatar joeldenning commented on September 13, 2024

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

if (parsedOptions.hasCount) {
// See https://www.i18next.com/translation-function/plurals#how-to-find-the-correct-plural-suffix
const pluralRule = i18next.services.pluralResolver.getRule(locale);
const numberOfPlurals = pluralRule.numbers.length;
if (numberOfPlurals === 1) {
keys = keys.map(k => ({
...k,
cleanKey: k.cleanKey + config.pluralSeparator + '0',
isDerivedKey: true,
}));
} else if (numberOfPlurals === 2) {
keys = keys.reduce(
(accumulator, k) => [
...accumulator,
k,
{
...k,
cleanKey: k.cleanKey + config.pluralSeparator + 'plural',
isDerivedKey: true,
},
],
Array<TranslationKey>(),
);
} else {
keys = keys.reduce(
(accumulator, k) => [
...accumulator,
...Array(numberOfPlurals)
.fill(null)
.map((_, idx) => ({
...k,
cleanKey: k.cleanKey + config.pluralSeparator + idx,
isDerivedKey: true,
})),
],
Array<TranslationKey>(),
);
}
}

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.

from babel-plugin-i18next-extract.

gilbsgilbs avatar gilbsgilbs commented on September 13, 2024

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?

from babel-plugin-i18next-extract.

joeldenning avatar joeldenning commented on September 13, 2024

I appreciate your quick response

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

  • Contexts: My understanding is that, in ICU format, contexts are represented through the select keyword (also see here). For my purposes with Transifex, the select keyword is not supported. The plural keyword is a special case of the select keyword, so if we start by supporting plural then we're most of the way there to supporting context, too. The main difference here is that there's only one translation key for contexts instead of multiple.
  • Namespaces: I don't believe that ICU really deals with namespaces - my understanding is that ICU format is only talking about the values for the strings, not the keys or namespaces.
  • Babel macro: I don't use the babel macro for the Trans component. Based on #60, it sounds like the babel macro isn't really working right anyway with babel-plugin-i18next-extract? The babel macro is something that would be related to extracting the keys and values, not related to exporting them. My suggestion is solely related to exporting, so I don't think it would have any impact on the babel macro.

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?

from babel-plugin-i18next-extract.

gilbsgilbs avatar gilbsgilbs commented on September 13, 2024

Thanks for all the insights on how ICU works with i18next. 👍

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.

from babel-plugin-i18next-extract.

joeldenning avatar joeldenning commented on September 13, 2024

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?

from babel-plugin-i18next-extract.

gilbsgilbs avatar gilbsgilbs commented on September 13, 2024

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

My rationale on this is the following:

  • If you can make a very simple change (i.e. a few lines) that saves you from a lot of hassle, then do a PR and I'll look into it. Maybe we could decide to let the option undocumented for now (until we have proper support for middlewares) or even expose it with a visible warning in docs stating that the API may change or even be dropped in future versions.
  • In the meantime, I have opened a very minimalistic RFC about middlewares (#110) since it popped up way too often in the few issues this project had. Feel free to contribute to it if you have strong opinions on how it should or should not look. Unfortunately, I don't think I'll be able to spend a lot of efforts implementing such API in the next upcoming weeks, but if anybody feels like tackling it I'll definitely review the PR or the draft 😄 .

from babel-plugin-i18next-extract.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.