ruby-gettext / gettext

Gettext gem is a pure Ruby Localization(L10n) library and tool which is modeled after the GNU gettext package.
https://ruby-gettext.github.io/
69 stars 28 forks source link

Documentation bug in README.md "Interpolating translations" section? #102

Closed ikaronen-relex closed 1 year ago

ikaronen-relex commented 1 year ago

I just looked at the README for this gem and happened to notice the following section (introduced 5 years ago in https://github.com/ruby-gettext/gettext/commit/bd68db6d1884f500388f2fd9c37c816571269fce):

### Interpolating translations

This is not a feature of gettext but worth noting. You can interpolate translated strings without the ruby String `%` operator.

```ruby
N_("active"); N_("inactive"); N_("paused") # possible value of status for parser to find.
_("Your account is #{account_state}.") % { account_state: _(status) }
```

Despite the apparent typo ("with" → "without"), this looked very wonderful and magical to me, so of course I tried it out immediately:

irb(main):001:0> require 'gettext'
=> true
irb(main):002:0> include GetText
=> Object
irb(main):003:0> status = 'active'
=> "active"
irb(main):004:0> N_("active"); N_("inactive"); N_("paused") # possible value of status for parser to find.
=> "paused"
irb(main):005:0> _("Your account is #{account_state}.") % { account_state: _(status) }
(irb):4:in `<main>': undefined local variable or method `account_state' for main:Object (NameError)
        from ~/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'                                  
        from ~/.rbenv/versions/3.2.2/bin/irb:25:in `load'                                                                                 
        from ~/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'    

So, yeah… that doesn't seem to work. And I don't see how it could work.

The following does work (although I'm not 100% sure how well it interacts with rxgettext):

irb(main):006:0> _("Your account is %{account_state}.") % { account_state: _(status) }
=> "Your account is active."

So I guess my suggestion is to replace "without" with "with" in the text and # with % in the code. 🤷

kou commented 1 year ago

You're right. Could you open a PR for it?