简体   繁体   中英

How to translate validation messages?

In application.rb I change local:

config.i18n.default_locale = :ru

I install:

gem russian

I create file /config/locales/ru.yml:

ru:
  activerecord:
    errors:
      messages:
        Title: "Название"
        Description: 'Описание'

And I output in controller validation messages:

  def create
    @album = current_user.albums.build(album_params)

    if @album.save
      render json: @album, :status => 200 
    else
      @errors = Array.new
      p '----------------'
      @album.errors.full_messages.each do |msg|
        p msg
        @errors.push(msg)
      end    
      p '----------------'
      p @errors
      render json: @errors, :status => 403 
    end
  end   

This is the result I get in console:

"----------------"
"Title не может быть пустым"
"Title недостаточной длины (не может быть меньше 3 символов)"
"Description не может быть пустым"
"Description недостаточной длины (не может быть меньше 10 символов)"
"----------------"

As you can see, the names of the fields(Title, Description) remained untranslated. How should I translate them?

This file: /config/locales/ru.yml requires additional translations in order for Rails to map the default field names to your Russian strings.

It's very important to use this exact structure, or the translation for the field names will not be loaded.

activerecord:
  attributes:
   modelname:
     fieldname: translated string

So, in your example...

  activerecord:
    attributes: 
      album:    
        Description: Описание
        Title: Название           
        ...

Please try and let me know how it goes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM