blackst0ne
blackst0ne

Reputation: 721

Ruby on Rails: validation depending on locale

I have a multilingual project with, say, English and Russian locales. I want such a validation:

example.com/en/book/new <- in a text field here it's only ASCII symbols available.

example.com/ru/book/new <- in a text field here it's only cyrillic symbols available.

What's the best way to do that?

I have an idea to use 'validates ... if ...' in models.

But what am I going to do if I have many models and I want to add just another locale?

Maybe there're some ways to keep such validations in a single file like i18n *.yml's?

Upvotes: 2

Views: 622

Answers (1)

HansCz
HansCz

Reputation: 284

You could write a custom validator and include it in the models you wish to validate

I18n.locale will return your current locale

Use it in a case statement to inside your custom validator to make language-specific checks

    case I18n.locale
    when :en
      # check for latin characters
    when :ru
      # check for cyrillic characters
    else
      return true
    end

Upvotes: 1

Related Questions