fnando / i18n-js

It's a small library to provide the I18n translations on the Javascript. It comes with Rails support.
MIT License
3.77k stars 520 forks source link

Add support for nested placeholders #624

Closed trajano closed 2 years ago

trajano commented 2 years ago

I'd like to be able to pass in a place holder as an object. For example I would want to pass with the original user profile object without having to modify it for the localization.

{
   userProfile: {
     desiredName: "Archie"
  }
}

and have it in the message as

Welcome {{userProfile.desiredName}}

At present it can't find it

Expected: "Welcome Archie"                                                                                                                                    
Received: "Welcome [missing {{userProfile.desiredName}} value]"  
fnando commented 2 years ago

Does this work with regular Rails? I tried and couldn't make it work.

I18n.t("hello")
=> "Hello, {{user.name}}!"
irb(main):005:0> I18n.t("hello", user: {name: "John"})
=> "Hello, {{user.name}}!"

I think we should keep it close to Rails' original syntax.

As an alternative, you may create a function that sets the paths for things like this? I18n.t("welcome", flat(userProfile)), then this flat function would generate a flat object out of the input. There are some packages that do that for you, like https://www.npmjs.com/package/flat.