Comments (19)
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.
from i18next.
Is if (format === 'duplicate')
block still needed if you're already declaring the formatter with the name "duplicate"?
from i18next.
sorry, copy/paste error
from i18next.
No worries. Thanks! 🙂
from i18next.
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.
Don't think so: https://github.com/i18next/i18next/blob/master/test/runtime/i18next.translation.formatting.test.js#L68
from i18next.
Sorry, my bad. I was accidentally using formatter.addCached()
instead of formatter.add()
. 😊
from i18next.
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.
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.
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.
Why are you passing the separator like that, and not hardcode it in the formatter function?
from i18next.
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.
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.
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.
if your separators always have whitespace, you can just add the whitespace in the format function?
from i18next.
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.
btw: I just tested, with whitespace like this and it seems to work: {{termGroupPath, join(separator: ' ► ')}}
from i18next.
Hmmm... You're right! I wonder why it was skipping my function before. Okay, then! No problems at all. 😄 🚀
from i18next.
Related Issues (20)
- A bunch of TS errors at compilation time HOT 1
- Regression report: 22->23 broke TypeScript autocomplete in our project HOT 8
- Change typescript test system HOT 1
- Typechecking performance regression in 23.7.12 with typescript v4 types HOT 9
- Events no longer firing when changing language to cimode HOT 8
- The `keyPrefix` options is not resolved correctly as of 23.7.14 HOT 8
- Clarify formatting usage HOT 1
- Unable to use import statement - only require works HOT 5
- Variables Interpolation not working properly when testing with Jest HOT 1
- Join an array with "," and "and" HOT 1
- Global t does not handle arrays as defaultNS well HOT 3
- i18n_t does not support choice format well HOT 5
- Deault values in t function do not work HOT 10
- i18next.getResource() does not return all plurials variants HOT 2
- Suspected memory leak HOT 1
- Alphabetical ordering of keys that can contain each other breaks i18next HOT 3
- Add Documentation for default translation file load HOT 7
- 使用方法addResources动态添加数据格式错误,Using method addResources to dynamically add data format error HOT 6
- Typescript performance regression HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from i18next.