# Interpolation-feature suggestion:

**URL:** <https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705>\
**Category:** Rails I18n\
**Created:** [February 1, 2010, 6:40pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705 "2010-02-01T18:40:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 1, 2010, 6:40pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/1 "2010-02-01T18:40:47Z")

</div>

Messaged to Sven directly, but maybe makes more sense to share:

After using i18n for quite some time and extending i with own  
extensions/powers I found one features that is lacking that really  
should be useful feature in the core. It has to do with  
interpolations; a feature related to José Valim’s patch seen here:  
[http://github.com/svenfuchs/i18n/commit/2c50bd209f3fc24fe9dfa694c81be64340f09b7d](http://github.com/svenfuchs/i18n/commit/2c50bd209f3fc24fe9dfa694c81be64340f09b7d).  
I’m quite experienced in Ruby, but I found it “not time efficient  
enough” to understand all the core code since I scanned it for 15  
minutes - there’s no documentation on the methods at all, so it’s not  
very easy for me right now to patch this. What I want to do is to  
support re-using translations within the document, like so:

en:  
settings:  
emails:  
support: “removed\_email\_address@domain.invalid”  
some\_strings:  
hello: “Please say hello to us at {{:settings.emails.support}}”  
goodbye: “Please say goodbye to us at  
{{:settings.emails.support}}”

Hope the sample says it all: In other words when interpolation is  
symbol - i.e. leading “:”-sign - it should be parsed as a lookup key  
instead of using a passed hash parameter/variable. It would be ideal,  
becuase many times translation editors are not the actual programmers  
so would be good if the YAML file is all there is for the translator  
guy/girl.

So I guess you see why it’s very convenient. But consider I want to  
patch this…could you give me a fast pointer on where this belongs? I  
ended up in “interpolations\_compiler.rb”, but as mentioned the code is  
not documented so It’s a maze. Would appreciate any help.

Oh, and: Do you agree that this would be awesome addition to I18n  
core?

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 3, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/2 "2010-02-03T17:00:00Z")

</div>

_knock_, _knock_

grimen

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 3, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/3 "2010-02-03T17:34:32Z")

</div>

Hi Grimen,

sorry for the late response.

Yes, I think your idea would make a great addition to I18n. But … I’m  
a bit hesitant of altering the API right away especially when it comes  
to interpolation. Also, we’d need to make sure that it works with the  
InterpolationCompiler if that’s included.

What would you think of implementing this as an optional module that  
overwrites Backend::Base#interpolate  
([i18n/lib/i18n/backend/base.rb at master · ruby-i18n/i18n · GitHub](http://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/base.rb#L179))  
so we can play with it and keep it optional for a while? I’d pull and  
include it to the I18n gem so it gets some attention.

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 3, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/4 "2010-02-03T17:39:48Z")

</div>

Ummm, btw just in case your not aware of this.

You can always avoid the repetition of that email address by injecting  
that value yourself:

t(:“some\_strings.hello”, :emai =\> t(:“settings.email”))

That obviously leaves control to the programmer though while your idea  
would translators allow to do it themselves …

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 3, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/5 "2010-02-03T20:20:47Z")

</div>

No problem, just knocking the list to be sure. 😉

Yes, understandable. Adding an experimental module would be an ideal  
start - I very much will be able to test it out in my current project  
too (which got loads of repeated interpolations for more variant  
communication to the end user). I think I’ll look at it asap then!

grimen

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 3, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/6 "2010-02-03T20:26:03Z")

</div>

Yes, I use that quite a lot but it’s starting to get messy after a  
while - and it’s static stuff so it feels wrong to recursively call it  
like so, makes sense to dynamic content such as :count =\>  
@collection.size, etc. =)

grimen

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 26, 2010, 6:17am UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/7 "2010-02-26T06:17:15Z")

</div>

Sven, got a chance to review this?

grimen

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [February 13, 2010, 7:40am UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/8 "2010-02-13T07:40:17Z")

</div>

OK, now I had some time to realize it:

> <https://github.com/grimen/i18n/commit/ff1169dc10d7db8ceca51997a72d040246706a78>
>
> Without "key interpolations":
> 
> interpolate "file {{file}} opened by \\\\{{user}}…", :file =\> 'test.txt', :user =\> 'Mr. X' # =\> "file test.txt opened by {{user}}"
> 
> With "key interpolations":
> 
> translate :"file.name" # =\> "manifesto.txt"
> 
> interpolate "file {{:file.name}} opened by \\\\{{user}}", :user =\> 'Mr. X' # =\> "file manifesto.txt opened by {{user}}"

Initial version, so up for review that is. As mentioned in a comment I  
avoided to mess with the core-ext for now that actually should be a  
better idea: String#% regex only looking for non-symbols:

> <https://github.com/grimen/i18n/blob/ff1169dc10d7db8ceca51997a72d040246706a78/lib/i18n/backend/key_interpolation.rb#L52-53>

…because of:

> <https://github.com/grimen/i18n/blob/ff1169dc10d7db8ceca51997a72d040246706a78/lib/i18n/core_ext/string/interpolate.rb#L34>

I guess that is identical to Ruby 1.9 implementation of it, so maybe  
my little way around it is okay as it’s all happen under the hood.

Reflections?

grimen

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [March 3, 2010, 12:05am UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/9 "2010-03-03T00:05:07Z")

</div>

Hey Grimen,

yes, I had a look. Sorry for not getting back to you earlier.

One thing that held me back is that I’ve been pondering to entirely pull  
our String core class patch out of the I18n library for a bunch of  
reasons. Maybe the most important one is that we’ve managed to break  
completely unrelated things (like rcov) by patching Ruby core classes.  
Obviously this is a risk every Ruby dev should know by now but somehow  
this sneaked into this now pretty widely used library.

At the point when we added that Ruby 1.9 String interpolation backport I  
think it was a great step forward for us. Today I believe that this  
rather should go into ActiveSupport (and maybe be optional anyway). As  
your usecase shows in I18n we will want to be in a position to go beyond  
that interpolation api.

Roughly, my thinking is to

- rip off the current String core extension and put it somewhere where  
people can pick it if they want it
- in I18n have a more streamlined implementation that is more open to  
usecases like yours

So, yes, I think we should support that case but maybe rethink the  
implementation.

Wdyt

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [March 7, 2010, 10:25am UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/10 "2010-03-07T10:25:56Z")

</div>

OK, so how can I go further with this from here? Sounds like it will  
be on the shelf for a while, so maybe I could make and effort to get  
there faster somehow?

grimen

---

<div class="post-metadata">

**Author:** ![grimen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/43a26b/32.png) [@grimen](https://www.ruby-forum.com/u/grimen)\
**Post date:** [March 3, 2010, 12:09am UTC](https://www.ruby-forum.com/t/interpolation-feature-suggestion/182705/11 "2010-03-03T00:09:48Z")

</div>

On Wed, Mar 3, 2010 at 00:04, Sven F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> At the point when we added that Ruby 1.9 String interpolation backport I think it was a great step forward for us. Today I believe that this rather should go into ActiveSupport (and maybe be optional anyway)

It’s there already, but I suppose you knew that :

[http://github.com/rails/rails/blob/master/activesupport/lib/active\_support/core\_ext/string/interpolation.rb](http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/interpolation.rb)
