T
T
TyzhSysAdmin2015-08-07 16:26:29
Ruby on Rails
TyzhSysAdmin, 2015-08-07 16:26:29

Let's play Ruby on Rails?

Yesterday, with a friend of CapeRatel , we had the following question:

Do not tell me how to display the class name translated into Russian in lower case from the @article object.

On a slightly tipsy head, I issued the first solution that came: (slightly rewrote) :
config/locales/ru.yml
ru:
  class_name_translate:
    article: 'Статьи'
    tags: 'Тэги'

article_controller.rb
def get_articles
  @article = Article.all
  @tag = Tag.all
  @article_model_ru = translate_model_name(@article).mb_chars.downcase
  @tag_model_ru = translate_model_name(@tag).mb_chars.downcase
end

private
def translate_model_name(obj)
  I18n.t(obj.model_name.i18n_key, scope: :class_name_translate)
end

Here is the actual question for people who are closely involved in RoR, how else can this problem be solved, the solution is more optimal for example :) Let's
discard the question of "why is this even needed" .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2015-08-07
@POS_troi

obj.model_name.human
And put the localization in ru.activerecord.models

T
TyzhSysAdmin, 2015-08-07
@POS_troi

I think Vsuhachev understood correctly .

ru:
  activerecord:
    models:
      article: 'Статьи'
      tags: 'Тэги'

@article = Article.all
@article_model_ru = (@article.model_name.human).mb_chars.downcase

Thank you, in fact, CapeRatel offered this option, I somehow missed it, apparently 2 am is not the best time :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question