GithubHelp home page GithubHelp logo

Array Interpolation about i18next HOT 19 CLOSED

robross0606 avatar robross0606 commented on June 3, 2024
Array Interpolation

from i18next.

Comments (19)

adrai avatar adrai commented on June 3, 2024 1

You can define your own format function like this: https://www.i18next.com/translation-function/formatting#adding-custom-format-function

i18next.services.formatter.add('duplicate', (value, lng, options) => {
    return value.map(v => v * 2).join(' | ');
});

from i18next.

adrai avatar adrai commented on June 3, 2024 1

codesandbox

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Is if (format === 'duplicate') block still needed if you're already declaring the formatter with the name "duplicate"?

from i18next.

adrai avatar adrai commented on June 3, 2024

sorry, copy/paste error

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

No worries. Thanks! 🙂

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Sorry to belabor this, but when I debug the use of services, it appears the values of value and lng are backwards in the function calls. I see the language (e.g. "en") passed in on the value parameter and my interpolation values showing up on the lng parameter. Is the documentation backwards?

from i18next.

adrai avatar adrai commented on June 3, 2024

Don't think so: https://github.com/i18next/i18next/blob/master/test/runtime/i18next.translation.formatting.test.js#L68

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Sorry, my bad. I was accidentally using formatter.addCached() instead of formatter.add(). 😊

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

I'm sorry to keep bothering, but the documentation here is a really a bit confusing. I'm trying to pass in the separator as an option to my interpolation like this:

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Sorry to keep bothering, but the documentation is a bit limited/confusing here. I'm trying to pass in the separator as an interpolation option like this:

      "no-selected-terms": "Schedule has no selected Terms on Activity Item: {{termGroupPath, join(separator:' ► ')}}"

If I don't pass in options to my custom formatting function, it works and uses the default separator:

      "no-selected-terms": "Schedule has no selected Terms on Activity Item: {{termGroupPath, join}}"

However, the second I try to pass in options, it appears to skip my custom formatter completely. What am I doing wrong here?

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Okay, I think I figured out the syntax, but this leads to what feels like an actual limitation in the design. If I remove the ' from my syntax like this:

"no-selected-terms": "Schedule has no selected Terms on Activity Item: {{termGroupPath, join(separator: ► )}}"

I am able to get the formatter called and the separator option is there. However, because I had to remove the quotes, the spaces are trimmed from the option and I end up with '►' instead of ' ► ' which is what I need. Is this a limitation/bug? Is it possible to pass whitespace as a format parameter?

from i18next.

adrai avatar adrai commented on June 3, 2024

Why are you passing the separator like that, and not hardcode it in the formatter function?

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Because I needed the formatter to be flexible to different separators based on the specific translation/interpolation. Are you saying I would need to set up a different custom formatter for every "join" delimiter?

from i18next.

adrai avatar adrai commented on June 3, 2024

The values are trimmed, while parsing...
But you can pass additional options when calling the t function, something like this:

t('no-selected-terms', { termGroupPath: '...', separator: ' ► ' })

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Hmmm... The problem with this approach is that it violates separation of concerns between data presentation and code. The separator used for a translation needs to be part of the translation data which is managed by a documentation expert -- part of the render layer. Meanwhile, code is code where we supply nothing but the key and the raw data. The documentation expert may decide to use a different separator in his declaration JSON file. We cannot hard code that.

from i18next.

adrai avatar adrai commented on June 3, 2024

if your separators always have whitespace, you can just add the whitespace in the format function?

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

It doesn't always have it, but I'm toying with working around this by adding a pad: true parameter. It isn't great, but this is a definite limitation of the library. Trimming all passed in parameters definitely limits flexibility.

from i18next.

adrai avatar adrai commented on June 3, 2024

btw: I just tested, with whitespace like this and it seems to work: {{termGroupPath, join(separator: ' ► ')}}

from i18next.

robross0606 avatar robross0606 commented on June 3, 2024

Hmmm... You're right! I wonder why it was skipping my function before. Okay, then! No problems at all. 😄 🚀

from i18next.

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.