Skip to content

Add I18n locale for validator error message #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ gemspec

gem 'pry'
gem 'simplecov', require: false, group: :test
gem 'i18n'
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ param! :books_array, Array, required: true do |b|
end
```

### I18n translate locale

You can custome message for validator

by override method 'custom_rails_param_i18n_path'

## Example

``` ruby
EmployeeController

en:
rails_param:
validator:
employee:
blank: 'Your Message'

def custom_rails_param_i18n_path
'rails_param.validator.employee.blank'
end
```

## Thank you

Many thanks to:
Expand Down
5 changes: 5 additions & 0 deletions lib/rails_param.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
require 'rails_param/param'
require 'i18n'

Dir[File.join(__dir__, 'rails_param/validator', '*.rb')].sort.each { |file| require file }
Dir[File.join(__dir__, 'rails_param/coercion', '*.rb')].sort.reverse.each { |file| require file }
Dir[File.join(__dir__, 'rails_param', '*.rb')].sort.each { |file| require file }

ActiveSupport.on_load(:action_controller) do
include RailsParam
end

I18n.load_path << Dir[File.expand_path("rails/locales") + "/*.yml"]
I18n.default_locale = :en
2 changes: 1 addition & 1 deletion lib/rails_param/param_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def param!(name, type, options = {}, &block)
# validate presence
if params[name].nil? && options[:required]
raise InvalidParameterError.new(
"Parameter #{parameter_name} is required",
I18n.t('rails_param.validator.default.required', name: parameter_name),
param: parameter_name,
options: options
)
Expand Down
8 changes: 8 additions & 0 deletions lib/rails_param/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ def valid_value?
# Should be overwritten in subclass
false
end

def default_rails_param_i18n_path
'rails_param.validator.default'
end

def custom_rails_param_i18n_path
nil
end
end
end
5 changes: 4 additions & 1 deletion lib/rails_param/validator/blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def valid_value?
private

def error_message
"Parameter #{name} cannot be blank"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.blank",
name: name
)
end
end
end
Expand Down
16 changes: 14 additions & 2 deletions lib/rails_param/validator/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ def valid_value?
STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze

def error_message
"Parameter #{name} must be a string if using the format validation" unless matches_string_or_time_types?
"Parameter #{name} must match format #{options[:format]}" unless string_in_format?
unless matches_string_or_time_types?
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.string_format",
name: name
)
end

unless string_in_format?
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.options_format",
name: name,
options: options[:format]
)
end
end

def matches_time_types?
Expand Down
5 changes: 4 additions & 1 deletion lib/rails_param/validator/in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def valid_value?
private

def error_message
"Parameter #{parameter.name} must be within #{parameter.options[:in]}"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.in",
name: parameter.name, options: parameter.options[:in]
)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/rails_param/validator/is.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def valid_value?
private

def error_message
"Parameter #{name} must be #{options[:is]}"
I18n.t("#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.is",
name: name, options: options[:is]
)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_param/validator/max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def valid_value?
private

def error_message
"Parameter #{name} cannot be greater than #{options[:max]}"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.max",
name: name,
options: options[:max]
)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_param/validator/max_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def valid_value?
private

def error_message
"Parameter #{name} cannot have length greater than #{options[:max_length]}"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.max_length",
name: name,
options: options[:max_length]
)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_param/validator/min.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def valid_value?
private

def error_message
"Parameter #{name} cannot be less than #{options[:min]}"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.min",
name: name,
options: options[:min]
)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rails_param/validator/min_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def valid_value?
private

def error_message
"Parameter #{name} cannot have length less than #{options[:min_length]}"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.min_length",
name: name,
options: options[:min_length]
)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/rails_param/validator/required.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ def valid_value?
end

def error_message
"Parameter #{name} is required"
I18n.t(
"#{custom_rails_param_i18n_path || default_rails_param_i18n_path}.required",
name: name
)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions rails/locales/ar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ar:
rails_param:
validator:
default:
blank: "لايمكن ان يكون المتغير %{name} فارغا"
string_format: "المتغير %{name} يجب ان يكون نص في حالة التحقق من صحة التنسيق"
options_format: "المتغير %{name} يجب ان يطابق التنسيق %{options}"
in: "المتغير %{name} يجب ان يكون من ضمن %{options}"
is: "المتغير %{name} يجب ان يكون %{options}"
max_length: "لايمكن ان يكون حجم المتغير اكبر من %{options}"
max: "لايمكن ان يكون المتغير %{name} اكبر من %{options}"
min_length: "لايمكن ان يكون حجم المتغير %{name} اقل من %{options}"
min: "لايمكن ان يكون المتغير %{name} اقل من %{options}"
required: "المتغير %{name} مطلوب"
14 changes: 14 additions & 0 deletions rails/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
en:
rails_param:
validator:
default:
blank: "Parameter %{name} cannot be blank"
string_format: "Parameter %{name} must be a string if using the format validation"
options_format: "Parameter %{name} must match format %{options}"
in: "Parameter %{name} must be within %{options}"
is: "Parameter %{name} must be %{options}"
max_length: "Parameter %{name} cannot have length greater than %{options}"
max: "Parameter %{name} cannot be greater than %{options}"
min_length: "Parameter %{name} cannot have length less than %{options}"
min: "Parameter %{name} cannot be less than %{options}"
required: "Parameter %{name} is required"
15 changes: 15 additions & 0 deletions spec/rails_param/validator/blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:name) { "foo" }
let(:options) { { blank: false } }
let(:type) { String }
let(:locale) { :en }
let(:error_message) { "Parameter foo cannot be blank" }
let(:parameter) do
RailsParam::Parameter.new(
Expand All @@ -17,6 +18,8 @@
subject { described_class.new(parameter) }

describe "#validate!" do
before { I18n.locale = locale }

context "String" do
context "is not empty" do
let(:value) { "bar" }
Expand All @@ -40,6 +43,7 @@

context "is empty" do
let(:value) { {} }
let(:locale) { :en }

it_behaves_like "raises InvalidParameterError"
end
Expand All @@ -54,6 +58,7 @@

context "is empty" do
let(:value) { [] }
let(:locale) { :en }

it_behaves_like "raises InvalidParameterError"
end
Expand All @@ -70,6 +75,7 @@

context "is empty" do
let(:value) { ActionController::Parameters.new() }
let(:locale) { :en }

it_behaves_like "raises InvalidParameterError"
end
Expand All @@ -84,9 +90,18 @@

context "is empty" do
let(:value) { nil }
let(:locale) { :en }

it_behaves_like "raises InvalidParameterError"
end
end

context "is locale ar" do
let(:locale) { :ar }
let(:error_message) { "لايمكن ان يكون المتغير foo فارغا" }
let(:value) { "" }

it_behaves_like "raises InvalidParameterError"
end
end
end
4 changes: 4 additions & 0 deletions spec/rails_param/validator/custom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:name) { "foo" }
let(:options) { { custom: custom_validation } }
let(:type) { String }
let(:locale) { :en }
let(:parameter) do
RailsParam::Parameter.new(
name: name,
Expand All @@ -17,6 +18,8 @@
subject { described_class.new(parameter) }

describe "#validate!" do
before { I18n.locale = locale }

context "value given is valid" do
let(:value) { 50 }

Expand All @@ -26,6 +29,7 @@
context "value given is invalid" do
let(:value) { 51 }
let(:error_message) { "Number is not even" }
let(:locale) { :en }

it_behaves_like "raises InvalidParameterError"
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rails_param/validator/format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:name) { "foo" }
let(:options) { { format: format_validation } }
let(:type) { String }
let(:locale) { :en }
let(:parameter) do
RailsParam::Parameter.new(
name: name,
Expand All @@ -17,6 +18,8 @@
subject { described_class.new(parameter) }

describe "#validate!" do
before { I18n.locale = locale }

context "value given is valid" do
let(:value) { "50$" }

Expand All @@ -29,5 +32,13 @@

it_behaves_like "raises InvalidParameterError"
end

context "is locale ar" do
let(:locale) { :ar }
let(:error_message) { "المتغير foo يجب ان يطابق التنسيق #{format_validation}" }
let(:value) { "50" }

it_behaves_like "raises InvalidParameterError"
end
end
end
11 changes: 11 additions & 0 deletions spec/rails_param/validator/in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:name) { "foo" }
let(:options) { { in: in_validation } }
let(:type) { Integer }
let(:locale) { :en }
let(:parameter) do
RailsParam::Parameter.new(
name: name,
Expand All @@ -17,6 +18,8 @@
subject { described_class.new(parameter) }

describe "#validate!" do
before { I18n.locale = locale }

context "value given is valid" do
let(:in_validation) { 1..100 }

Expand All @@ -29,5 +32,13 @@

it_behaves_like "raises InvalidParameterError"
end

context "is locale ar" do
let(:locale) { :ar }
let(:error_message) { "المتغير foo يجب ان يكون من ضمن 51..100" }
let(:in_validation) { 51..100 }

it_behaves_like "raises InvalidParameterError"
end
end
end
10 changes: 10 additions & 0 deletions spec/rails_param/validator/is_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:name) { "foo" }
let(:options) { { is: "50" } }
let(:type) { String }
let(:locale) { :en }
let(:parameter) do
RailsParam::Parameter.new(
name: name,
Expand All @@ -16,6 +17,7 @@
subject { described_class.new(parameter) }

describe "#validate!" do
before { I18n.locale = locale }
context "value given is valid" do
let(:value) { "50" }

Expand All @@ -28,5 +30,13 @@

it_behaves_like "raises InvalidParameterError"
end

context "is locale ar" do
let(:locale) { :ar }
let(:error_message) { "المتغير foo يجب ان يكون 50" }
let(:value) { "51" }

it_behaves_like "raises InvalidParameterError"
end
end
end
Loading