Skip to content

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

@ikaronen-relex

Description

@ikaronen-relex

I just looked at the README for this gem and happened to notice the following section (introduced 5 years ago in bd68db6):

### 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. 🤷

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions