ruby on rails - I18n to call different Model Attribute depending on Locale? -


so building on-line shop , want 2 language options, english , spanish.

i using i18n static text , headings ect.

but, have products model can have new products created listing on site. has fields :name_en , :name_es, :description_en , :description_es ect.

when admin uploads new product need add english , spanish text.

because have 2 locales think call like

    <%= product.name_"#{i18n.locale.downcase}" %> 

but not work. how can i, or can i, interpolate method or attribute?

have missed obvious here , going wrong way or there way along lines of thinking?

any massively appreciated.

thanks

you can use send method. try like:

<%= product.send("name_#{i18n.locale.downcase}") %> 

just word of explanation, following equal:

string = "hello"  string.upcase # => "hello"  string.send("upcase") # => "hello" 

hope puts in proper direction!

good luck!


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -