Globalize Validations validates your translated attributes for each given locales.
The errors are added to the globalize attributes accessor names (example title_en
).
globalize-validations
is compatible with Rails 3.x, Rails 4.0.x and Rails 4.1.x.
Globalize >= 3.0.0
, Globalize-Accessors >= 0.1.3
and Ruby >= 1.9.3
.
API documentation is available at rdoc.info.
Add this line to your application's Gemfile:
gem 'globalize-validations'
And then execute:
$ bundle
Default
class Page < ActiveRecord::Base
translates :title, :body
globalize_accessors locales: [:en, :es, :fr, :pl], attributes: [:title]
globalize_validations # Will use Model.globalize_locales by default
# Add all your validations before
validate :validates_globalized_attributes
end
With custom locales
class Page < ActiveRecord::Base
translates :title, :body
globalize_accessors locales: [:en, :es, :fr, :pl], attributes: [:title]
globalize_validations locales: [:en, :es] # Validates only `:en` and `:es` locales
# Add all your validations before
validate :validates_globalized_attributes
end
With custom locales as Proc
class Page < ActiveRecord::Base
translates :title, :body
globalize_accessors locales: [:en, :es, :fr, :pl], attributes: [:title]
globalize_validations locales: Proc.new { |page| page.available_locales }
# Add all your validations before
validate :validates_globalized_attributes
end
Copyright (c) 2014 BookingSync released under the MIT license