开发者

Operations inside Rails I18n locales' strings

I am trying to put operations inside the locales to adapt to different 开发者_Go百科languages. For example, in English a billion is 1,000,000,000, however in Spanish a billion is 1,000,000,000,000 so I would like to be able to have the following:

en:
  billion: "You have %{money} billions"
es:
  billion: "Tienes %{money/1000.0} billones"

In order to be able to write:

I18n.t :billion, :money => whatever

And be right for whatever language.

However, it seems that I cannot put operations inside the locales' strings.

Any hint on how should I be doing this? Maybe my approach is just wrong "philosophically" talking?

Thanks all!


Based on investigation, here's the solution. Sorry for the multiple edits.

I18n.interpolate("%{money} billion", :money => Proc.new{|x| x[:vals] / 1000}, :vals => 12121)

So you could change your translation file to use :vals in :en and :money in :es.

en:
  billion: "You have %{money} billions"
es:
  billion: "Tienes %{money_proc} billones"

I18n.t :billion, {:money => 12121, :money_proc => Proc.new{|x| x[:money]/1000.0}}


Can you just change 'billiones' to 'mil millones' in the translation?


I18n (with good judgements) allows no Ruby interpolations, the YAML files would be a mess. The Rails helper number_to_human seems to support what you want.

[edit] Units are fixed in a frozen hash (ActionView::Helpers::DECIMAL_UNITS), so number_to_human won't be useful unless you do some tweaking before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜