question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

translate returns an object instead of a string

See original GitHub issue

when used outside the return statement of the React class render function, the react-translate-component’s translate function returns an object where a string should be returned. code snippet within a React.creatClass: render:function() { var menusLns = this.getFooterMenu(); //getting a json file that describes my menus var i, j; var menus = []; var content;

    for (i = 0; i < menusLns.length; ++i) {
        var Menu = {};
        Menu.title = menusLns[i].id;
       Menu.items = "";
        for (j = 0; j < menusLns[i].menuitem.length; ++j) {
            var tagTitle = menusLns[i].menuitem[j].value;
            console.log("item tagTitle = " + tagTitle); // Ok
            var text = this._e("gui", tagTitle); // we are not within the return statement...

// this._e comes from a mixin lib in which we have _e: require(‘react-translate-component’).translate, // this syntax works nicely in another react component, but within its return statement console.log(“item text = " + text); // KO: object Object var link = menusLns[i].menuitem[j].link; var item = '\u003cli\u003e\u003ca title=”’ + tagTitle + ‘" href="’ + link + ‘"\u003e’ + text + ‘\u003c/a\u003e\u003c/li\u003e\n’; console.log("menu item: " + item); Menu.items += item; } menus.push(Menu); }

    return (
        <  ....  >
    );
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
martinandertcommented, Jul 18, 2014

How can I update counterpart translations when user select locale in switcher component?

You could listen to Counterpart’s locale change event in your component’s componentDidMount lifecycle method. Example (untestet):

var MyComponent = React.createClass({
  /* ... */

  componentDidMount: function() {
    counterpart.onLocaleChange(this.localeChanged);
  },

  componentWillUnmount: function() {
    counterpart.offLocaleChange(this.localeChanged);
  },

  localeChanged: function(newLocale) {
    this.setState({ 
      placeholder: counterpart.translate('foo.email.placeholder')
    });
  },

  render: function () {
    var i18n_placeholder = this.state.placeholder;

    return (
      <div>
        <input type="email" placeholder={i18n_placeholder} />
      </div>
    );
  },
});
0reactions
neemzycommented, Dec 1, 2015

Thanks for the tip! Might be useful to put that somewhere in the README (FAQ or so), as it is a quite common use case.

In my case, I built on top of your tip to have Counterpart.onLocaleChange set the current locale in the state of my root component. When the locale changes, it rerenders itself and all its children, thus triggering Counterpart’s native translate function again with the current locale. Any thoughts? Would you fear a performance pitfall with such a global solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

t() returns a TranslatableMarkup object instead of a string
The actual translation will only be performed whenever the object is casted to string or whenever the TranslatableMarkup object is rendered.
Read more >
angular $translate.instant returns object instead of string
Inside my controller, I need to pass a translated string value to the $http service. But below code is returning me TrustedValueHolderType ...
Read more >
Objects and Arrays - i18next documentation
You can return objects or arrays to be used by third party modules localization: · keys · sample · The returned value supports...
Read more >
eval() - JavaScript - MDN Web Docs - Mozilla
The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
Read more >
5 Ways to Convert a Value to String in JavaScript
"" + val: simply cast number to string - let's say inside of the .map() · JSON.stringify(val): need to convert small non-nested object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found