mrateb
mrateb

Reputation: 2499

Custom translation function in Rails

I have an application that I'm currently localizing to Arabic but Im facing a small problem.

In the English version of the website, I display some sizes as follows 10x15 15x18 etc, and they are stored in the database as such.

In my Arabic version I want to display instead 10*15 15*18 etc.

Currently, I have a string replace function in one of the models as follows:

if I18n.locale == :ar
   size.sub! 'x', '*'
end

but I'm sure that I shouldn't have code like that in a model. How can I do this using the usual I18n ways?

Upvotes: 1

Views: 90

Answers (1)

Vasfed
Vasfed

Reputation: 18444

x in database is internal for your storage means, so strictly speaking for english you also should have size.sub! 'x', 'x' (which does nothing, because symbol is the same, but it changes it's meaning).

You're correct that symbol belongs to locale. Locales support variables in translations:

en:
  some_size: "%{width}x%{height}"
ar:
  some_size: "%{width}*%{height}"
I18n.t('some_size', width: size.split('x').first, height: size.split('x').second)

(to make the last expression more tidy - you can have width/height or size_hash and use splats)

Upvotes: 1

Related Questions