Giter Site home page Giter Site logo

Comments (9)

caridy avatar caridy commented on May 2, 2024

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.

ericf avatar ericf commented on May 2, 2024

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.

vafada avatar vafada commented on May 2, 2024

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.

ericf avatar ericf commented on May 2, 2024

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.

vafada avatar vafada commented on May 2, 2024

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.

vafada avatar vafada commented on May 2, 2024

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

pselden avatar pselden commented on May 2, 2024

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

ericf avatar ericf commented on May 2, 2024

@pselden makes sense. @vafada is there a reason why you like having dots in your string message keys? :)

from formatjs.

vafada avatar vafada commented on May 2, 2024

@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)

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.