Reflexive Interpolation (Feature Request / Discussion) #129

Open
leehambley opened this Issue Dec 15, 2011 · 0 comments

Projects

None yet

1 participant

@leehambley

Note: Moved from the rails-i18n project #169.

Further to a very brief twitter exchange I just had with @svenfuchs on Twitter, I thought I would write this here, that there might be a discussion about the merits of this option.

Given the following YAML file:

de:
  virtual_currency:
    one: "Jelly Bean"
    other: "Jelly Beans"
  products:
    index:
      title: "Earn Jelly Beans by retweeting @svenfuchs"
    show:
      title: "Earn Jelly Beans by retweeting @svenfuchs, spend your Jelly Beans on %{product_name}"

A naturally stupid and contrived example, not the frequent duplication of Jelly Beans.

Whilst it's clear that I18n isn't DRY, the name of our virtual currency is re-re-reused in a multitude of places, and it's always the .other subkey. There's one edge-case when it's only one Jelly Bean.

Our solution so far, then looks like this:

de:
  virtual_currency:
    one: "Jelly Bean"
    other: "Jelly Beans"
  products:
    index:
      title: "Earn %{virtual_currency} by retweeting @svenfuchs"
    show:
      title: "Earn %{virtual_currency} by retweeting @svenfuchs, spend your %{virtual_currency} on %{product_name}"

And in every place it's used, t(:'.title', virtual_currency: t('virtual_currency'), product_name: @product.name). This is a little tedious.

To that end, I have a modification to I18n::Backend::Simple (WIP, will probably become a Defaultnterpolations backend in it's own right) that looks something like this:

module I18n
  module Backend
  class Simple
    def interpolate_with_defaults(locale, string, values = {})
      defaults = {virtual_currency: lookup(locale, "virtual_currency.other")}
      interpolate_without_defaults(locale, string, values.merge(defaults))
    end
    alias :interpolate_without_defaults :interpolate
    alias :interpolate :interpolate_with_defaults
  end
end

Would it be possible, advisable, or well received to implement an alternative interpolation syntax, perhaps the immortal {{…}} which would do what I want to call reflexive interpolation, here's an example, only YAML, there's no code changes:

de:
  virtual_currency:
    one: "Jelly Bean"
    other: "Jelly Beans"
  products:
    index:
      title: "Earn {{virtual_currency.other}} by retweeting @svenfuchs"
    show:
      title: "Earn {{virtual_currency.other}} by retweeting @svenfuchs, spend your {{virtual_currency.other}} on %{product_name}"

There's some flaws, I am sure, and one broken, yet seemingly similarly targeted implementation here. From which I drew the original idea for --the solution-- my hack.

I haven't yet explored this enough to know if I18n backends can modify, and pass-on the string in question, to the next backend in the chain, that's some investigation I need to do before I decided if this can be simply a chainable backend, or whether it requires a change to an existing backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment