Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ikaronen-relex opened this issue Oct 2, 2023 · 1 comment
Closed

Comments

@ikaronen-relex
Copy link
Contributor

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

@kou
Copy link
Member

kou commented Oct 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants