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.

`formatMessage` with deep paths

See original GitHub issue
  • I am on the latest ember-intl version
  • I have searched the issues of this repo and believe that this is not a duplicate

Environment

  • Ember Version: 3.16.8
  • Ember CLI Version: 3.16.2
  • Ember Intl Version: 5.3.1
  • Browser(s): Chrome / Firefox
  • Node Version: 12.18.1

Steps to Reproduce

Calling intel.formatMessage referencing properties on nested objects using dots doesn’t work. I’m not sure if this is intended to work or not but:

// This works:
this.intl.formatMessage(`Hello {foo}!`, { foo: 'world' }); // Hello world!
// But this doesn't
this.intl.formatMessage(`Hello {foo.bar}!`, { foo: { bar: 'world' } }); // SyntaxError: Expected "," but "." found.

Using t allows me to reference a nested object:

this.intl.t('foo.bar', { foo: { bar: 'Hello world!' } }); // Hello world!

But I want to take a complete string and substitute out the dynamic portions (which I guess is what formatMessage is for?).

I’ll dig a bit deeper but I wanted to check if this was expected behavior or if there was a better way to achieve what I’m trying to do?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vitchcommented, Jul 28, 2020

Thanks for the reply.

I’ve had a look around and haven’t found another implementation which supports deep objects. Even things which support yaml seem to show flat objects. This kind of surprises me but makes me think that trying to use nested objects will involve swimming upstream.

I could pre-process my data (I was using this for dynamic string interpolation with support for pluralisation etc rather than straight up translation) and templates to e.g. flatten the objects and replace relevant .s with _s but I will first think about what it would be like to only use flat data.

Thanks again - we can close this if you’d like since I don’t think there is much that can be done about it in this repo…

0reactions
jasonmitcommented, Jul 27, 2020

Was it an intentional addition to the behaviour of t in this repo?

To support nested translations? Yes, this was intentional but also in a different scope than the intl-messageformat compiler. The parser/compiler never deals with translation keys just translation messages.

I’m guessing that the addition of . in property names will be a hard sell upstream

I’d look into other messageformat implementations (most languages have a library) and see if any accept argument types referring to a deep object. If so, it might help the argument. I’m also unsure if even the ICU msg format spec supports it - I’d also investigate that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kiwi Syslog Server - FormatMessage failed with error 1815
This article describes an issue where you receive the message "FormatMessage failed with error 1815 using Solarwinds log forwarder."
Read more >
Use React-intl translated messages in Redux middleware
Go deeper into InjectedIntl interface. You can see why intl instance has formatMessage method. interface InjectedIntl { formatDate(value: DateSource, ...
Read more >
to MAX_PATH and beyond (programmer's deep path cookbook)
How to break through the MAX_PATH directory/folder name barrier · 1. Create a deep path for testing. This is easier said than done....
Read more >
Class FormatMessage (0.1.5) | Python client library
Represents a message with parameters. Attributes. Name, Description. format_, str. Format template for the message. The ``format`` ...
Read more >
gatsby-plugin-react-intl
ignoredPaths : paths that you don't want to genereate locale pages, ... Support multi-language url routes in a single page component.
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