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.

t helper does not accept a hash object directly

See original GitHub issue
  • I am on the latest ember-intl version
    • Not on the latest, but I believe this still applies to the latest version (looking at the code)
  • I have searched the issues of this repo and believe that this is not a duplicate

Environment

  • Ember Version: 3.20.5
  • Ember CLI Version: 3.15.2
  • Ember Intl Version: 5.5.0
  • Browser(s): Any
  • Node Version: 12.18.4

Steps to Reproduce

If I have a translation with multiple placeholders,

"myTranslation": "You can either {option1} or {option2}"

I need to pass them individually, like so:

{{t myTranslationPath option1='run' option2='hide'}}

It would be ideal if we could pass the options hash directly, like so:

{{t myTranslationPath optionsHash}}

where the optionsHash is

{option1="run", option2="hide"}

However - because of how helpers work, any non-named arguments are passed as an array, and those are not picked up by the ‘t’ helper (just get an error saying that I did not pass the placeholders).

In the above example, it’s just two arguments - but imagine the case where we have variable translation strings, and thus variable arguments - it’s extra legwork that should not be necessary.

Not sure if this is a bug or considered an enhancement (or if there’s a good workaround that I am missing).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
buschtoenscommented, Jan 17, 2022

Thanks a lot @jesdavpet! Your PR looks great. ✨

To answer your questions:

  1. I believe that should be sufficient.
  2. Correct, AFAIK. Didn’t get to spend a lot of time in this repo in the past months unfortunately.
  3. I added a comment to your PR regarding this.
3reactions
buschtoenscommented, Jun 3, 2021

Great idea!

Ideally we could solve this using Spreadable Arguments (aka “splarguments”), however the RFC is not even finished yet.

I think extending {{t}} or rather the underlying AbstractHelper is safe, because it currently only accepts a single value:

https://github.com/ember-intl/ember-intl/blob/d4c9d86aa809f9b8215e6cadad52278f8b5e7f89/addon/helpers/-format-base.js#L31-L43

We could change it to something like:

export default class AbstractHelper extends Helper {
  // ...

  compute([value, positionalOptions], namedOptions) {
    const options = positionalOptions
      ? Object.assign({}, positionalOptions, namedOptions)
      : namedOptions;

    if (isEmpty(value)) {
      if (options.allowEmpty ?? this.allowEmpty) {
        return;
      }

      if (typeof value === 'undefined') {
        throw new Error(`${this} helper requires value attribute.`);
      }
    }

    return this.format(value, options);
  }

  // ...
}

Then you could invoke it like so:

{{t "my.key" (hash a="hello" b="world")}}            => hello world

You can also combine it with named parameters and they override the ones from the (hash), which I think is sensible.

{{t "my.key" (hash a="hello" b="world") b="planet"}} => hello planet

Because we’re changing this in the AbstractHelper, this would also affect / improve the other formatting helpers, which I think is positive as well.

From my POV I would happily accept a PR, unless anyone steps forward with good objections.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hashing and Equality in Python - Lyft Engineering
Let's first dive into how objects work if we don't implement __hash__ and __eq__ . By default, equality and hashes are based on...
Read more >
Equivalent of .try() for a hash to avoid "undefined method ...
try is that object can be nil . Whereas in your case nil.get_deep will raise an exception. Your solution doesn't answer the question...
Read more >
hashlib — Secure hashes and message digests ... - Python Docs
The hashlib module provides a helper function for efficient hashing of a file or file-like object. ... Return a digest object that has...
Read more >
Helper T Cells and Lymphocyte Activation - NCBI - NIH
Helper T cells are arguably the most important cells in adaptive immunity, ... They not only help activate B cells to secrete antibodies...
Read more >
How to Handle Unhashable Type List Exceptions in Python
The Python TypeError: Unhashable Type: 'list' happens when a mutable list, instead of an immutable tuple, is used as a hash argument.
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