Comments (9)
hmm, this is going to be problematic to achieve because it is ambiguous, and supporting namespacing is paramount for complex apps (like those we have here at yahoo). give us some time to think about this.
from formatjs.
Using getIntlMessage()
is completely optional; it's simply for convenience. As @caridy mentioned this is ambiguous so there's no way for us to cater to both use cases.
If you have message keys that contain dots, then you could access the message like this:
var messages = this.props.messages || this.context.messages;
var message = messages['foo.bar'];
from formatjs.
I understand i can do that and that's what exactly i did. The problem I have to create my own mixin, extend react-intl and shim it instead of just working out of the box 😢
from formatjs.
Can you simply create a mixin that overrides getIntlMessage()
that's provided by ReactIntlMixin
by simply adding yours after in the list of mixins
? I don't see why you need to extend react-intl
.
We've documented getIntlMessage()
as taking a string path/namespace (although we could make it more clear.) Since we have the ambiguity issue, what could we do to help make it easier for you to access your messages?
from formatjs.
Can you simply create a mixin that overrides getIntlMessage() that's provided by ReactIntlMixin by simply adding yours after in the list of mixins? I don't see why you need to extend react-intl.
Oh didn't knew you can do that. I can try it out and just document in our project that the ReactIntlMixin
shoould come before our own mixin in the React.mixins
properties. Anyways, thanks for help.
from formatjs.
@ericf I know i'm beating a dead horse but React doesn't allow to override mixin methods.
Uncaught Error: Invariant Violation: ReactCompositeComponentInterface: You are attempting to define
getIntlMessage on your component more than once. This conflict may be due to a mixin.
I'll probably add a new method on react-inl, something like:
var reactIntl = require('react-intl');
reactIntl.getValue = function() {
var messages = this.props.messages || this.context.messages;
return messages['foo.bar'];
}
module.exports = reactIntl;
is there a better way to do this besides the code above?
from formatjs.
@vafada I'd probably go for the extending the mixin route instead of shimming the react intl mixin itself.
I think you could do something like this:
// MyIntlMixin.js
var assign = require('object-assign');
var ReactIntlMixin = require('react-intl');
var MyIntlMixin = assign(ReactIntlMixin, {
getIntlMessage: function(path) { /* my specific implementation, overriding the old one */ },
getLocalizedMessage: function(path) { /* my specific implementation, adding a new method */ }
});
module.exports = MyIntlMixin;
Then use MyIntlMixin instead of ReactIntlMixin.
from formatjs.
@pselden makes sense. @vafada is there a reason why you like having dots in your string message keys? :)
from formatjs.
@pselden looks goods! thanks!
@ericf leveraging existing api / legacy api support. I actually like the namespace/nested keys but need to work with existing api. 😢
from formatjs.
Related Issues (20)
- Importing intl-displaynames polyfill/locale-data dynamically in Next app bloats bundle size HOT 1
- @formatjs/intl-numberformat @ 8.9.0 -> 8.10.0 breaks Chrome 104 HOT 1
- cli-lib: onMsgExtracted never gets called HOT 1
- Update translation of 'last year' for Swedish locale HOT 1
- @formatjs/cli-lib is not an ESM module HOT 1
- formatMessage with ICU behave different then formatNumber HOT 2
- [@formatjs/intl Error MISSING_DATA] should be a warning not an error HOT 3
- [@formatjs/intl-durationformat] package.json deprecation warning HOT 2
- @formatjs/intl-messageformat, 10.5.0: Plural rules aren't applied correctly HOT 2
- @formatjs/intl-numberformat/polyfill: roundingIncrement seems to be a no-op HOT 3
- [docs] Rich Text Formatting example doesn't work HOT 2
- Polyfill.io has been sold to a weird Chinese company and should no longer be recommended HOT 1
- chore: update tzdata to 2024a
- INVALID_TAG when parsing regular HTML tag with attribute HOT 1
- Can't catch error HOT 1
- Feature request: allow units greater than `day` in `FormattedRelativeTime` with `updateIntervalInSeconds` to be picked HOT 2
- It's incredibly slow on lower end Android devices HOT 2
- Intl.Segmenter: isWordLike does not match Node.js and browsers HOT 2
- @formatjs/intl-locale/polyfill-force TypeError: Error uninitialized locale at Locale2.getWeekInfo HOT 3
- .formatMessage is not working when provided messages has object with `type: 1` HOT 8
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 formatjs.