ruby-i18n/i18n

[FEATURE]

foonlyboy opened this issue · 1 comments

Hi Sven,
I18n is hard.
Big thanks for all your work on that.

I use I18n in the context of Rails.
I try to make it more easy, though.

We all know the the #to_s method (which should not be localized)

I'd like to suggest: #to_s_i18n
That method should return a localized String (or fallback to #to_s)

For the I18n lookup to work, we need a key and a scope:

  • #i18n_key
  • i#18n_scope

These methods can be implemented by the objects (and class objects) at will,
or we would fall back gracefully (aka back to #to_s)

something like:
{{{
def to_s_i18n
if respond_to?(:18n_key) && repsond_to?(:18n_scope)
I18n.translate(self.18n_key, scope: self.18n_scope, default: self.to_s)
else
self.to_s
end
}}}
[TODO: find at least five bugs in my naive code sample]

But it could be nice.
all of the system could than understand: #to_s_i18n
...

I digged even deeper,
should we put the translation key into the locales.yml?
something like? activerecord.models.tax.i18_key: tag

I'm awaiting your comments:

  • We have to make sure that it can not break any legacy code.
  • This would be a new api. This needs serious review and tests

For the time being,
this should not be integrated into I18n,
but should be a gem for test.

I need your help on how to integrate.
We do not monkey patch any more.

But that would be a cross cut,
to tell every Object in the House,
how to translate itself.

Thing about nil:

nil.to_s_i18n

  • en: "Not available"
  • de: "Nicht vorhanden"

This is way big,
that we can even ask nil,
to translate.

radar commented

No thank you, I would rather there be just the I18n.t and I18n.localize methods -- tracking down where exactly to_s_i18n is coming from would be difficult, and I'm loathe to add more methods to objects (ala what Active Support already does)