diff --git a/Gemfile b/Gemfile index e0d5e86..0a1b6dd 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ gemspec gem 'pry' gem 'simplecov', require: false, group: :test +gem 'i18n' diff --git a/README.md b/README.md index 5af4679..0f86efd 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/rails_param.rb b/lib/rails_param.rb index aacf237..2465511 100644 --- a/lib/rails_param.rb +++ b/lib/rails_param.rb @@ -1,4 +1,6 @@ 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 } @@ -6,3 +8,6 @@ ActiveSupport.on_load(:action_controller) do include RailsParam end + +I18n.load_path << Dir[File.expand_path("rails/locales") + "/*.yml"] +I18n.default_locale = :en diff --git a/lib/rails_param/param_evaluator.rb b/lib/rails_param/param_evaluator.rb index a619c27..11518b4 100644 --- a/lib/rails_param/param_evaluator.rb +++ b/lib/rails_param/param_evaluator.rb @@ -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 ) diff --git a/lib/rails_param/validator.rb b/lib/rails_param/validator.rb index faa580a..48997eb 100644 --- a/lib/rails_param/validator.rb +++ b/lib/rails_param/validator.rb @@ -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 diff --git a/lib/rails_param/validator/blank.rb b/lib/rails_param/validator/blank.rb index aa77284..5695117 100644 --- a/lib/rails_param/validator/blank.rb +++ b/lib/rails_param/validator/blank.rb @@ -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 diff --git a/lib/rails_param/validator/format.rb b/lib/rails_param/validator/format.rb index e40ff29..4b3ff37 100644 --- a/lib/rails_param/validator/format.rb +++ b/lib/rails_param/validator/format.rb @@ -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? diff --git a/lib/rails_param/validator/in.rb b/lib/rails_param/validator/in.rb index 068d871..827b09a 100644 --- a/lib/rails_param/validator/in.rb +++ b/lib/rails_param/validator/in.rb @@ -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 diff --git a/lib/rails_param/validator/is.rb b/lib/rails_param/validator/is.rb index d1b8c35..1ffa357 100644 --- a/lib/rails_param/validator/is.rb +++ b/lib/rails_param/validator/is.rb @@ -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 diff --git a/lib/rails_param/validator/max.rb b/lib/rails_param/validator/max.rb index 8f715a5..70fe674 100644 --- a/lib/rails_param/validator/max.rb +++ b/lib/rails_param/validator/max.rb @@ -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 diff --git a/lib/rails_param/validator/max_length.rb b/lib/rails_param/validator/max_length.rb index 6c1d834..4cb44f1 100644 --- a/lib/rails_param/validator/max_length.rb +++ b/lib/rails_param/validator/max_length.rb @@ -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 diff --git a/lib/rails_param/validator/min.rb b/lib/rails_param/validator/min.rb index 9ed71c6..1ce8e13 100644 --- a/lib/rails_param/validator/min.rb +++ b/lib/rails_param/validator/min.rb @@ -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 diff --git a/lib/rails_param/validator/min_length.rb b/lib/rails_param/validator/min_length.rb index 6625589..bf76bf0 100644 --- a/lib/rails_param/validator/min_length.rb +++ b/lib/rails_param/validator/min_length.rb @@ -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 diff --git a/lib/rails_param/validator/required.rb b/lib/rails_param/validator/required.rb index 1a18ba0..b31597d 100644 --- a/lib/rails_param/validator/required.rb +++ b/lib/rails_param/validator/required.rb @@ -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 diff --git a/rails/locales/ar.yml b/rails/locales/ar.yml new file mode 100644 index 0000000..4b92702 --- /dev/null +++ b/rails/locales/ar.yml @@ -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} مطلوب" diff --git a/rails/locales/en.yml b/rails/locales/en.yml new file mode 100644 index 0000000..1aff6b3 --- /dev/null +++ b/rails/locales/en.yml @@ -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" diff --git a/spec/rails_param/validator/blank_spec.rb b/spec/rails_param/validator/blank_spec.rb index b711376..407ccec 100644 --- a/spec/rails_param/validator/blank_spec.rb +++ b/spec/rails_param/validator/blank_spec.rb @@ -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( @@ -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" } @@ -40,6 +43,7 @@ context "is empty" do let(:value) { {} } + let(:locale) { :en } it_behaves_like "raises InvalidParameterError" end @@ -54,6 +58,7 @@ context "is empty" do let(:value) { [] } + let(:locale) { :en } it_behaves_like "raises InvalidParameterError" end @@ -70,6 +75,7 @@ context "is empty" do let(:value) { ActionController::Parameters.new() } + let(:locale) { :en } it_behaves_like "raises InvalidParameterError" end @@ -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 diff --git a/spec/rails_param/validator/custom_spec.rb b/spec/rails_param/validator/custom_spec.rb index 9efebac..eeae168 100644 --- a/spec/rails_param/validator/custom_spec.rb +++ b/spec/rails_param/validator/custom_spec.rb @@ -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, @@ -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 } @@ -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 diff --git a/spec/rails_param/validator/format_spec.rb b/spec/rails_param/validator/format_spec.rb index 978ee9f..5ecc8bd 100644 --- a/spec/rails_param/validator/format_spec.rb +++ b/spec/rails_param/validator/format_spec.rb @@ -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, @@ -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$" } @@ -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 diff --git a/spec/rails_param/validator/in_spec.rb b/spec/rails_param/validator/in_spec.rb index 8cf54f7..892ddba 100644 --- a/spec/rails_param/validator/in_spec.rb +++ b/spec/rails_param/validator/in_spec.rb @@ -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, @@ -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 } @@ -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 diff --git a/spec/rails_param/validator/is_spec.rb b/spec/rails_param/validator/is_spec.rb index 672baeb..e94f620 100644 --- a/spec/rails_param/validator/is_spec.rb +++ b/spec/rails_param/validator/is_spec.rb @@ -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, @@ -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" } @@ -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 diff --git a/spec/rails_param/validator/max_length_spec.rb b/spec/rails_param/validator/max_length_spec.rb index 29bc217..ddf5315 100644 --- a/spec/rails_param/validator/max_length_spec.rb +++ b/spec/rails_param/validator/max_length_spec.rb @@ -5,6 +5,7 @@ let(:value) { "bar" } let(:options) { { max_length: max_length } } let(:type) { String } + let(:locale) { :en } let(:parameter) do RailsParam::Parameter.new( name: name, @@ -17,6 +18,8 @@ subject { described_class.new(parameter) } describe "#validate!" do + before { I18n.locale = locale } + context "value given is valid" do let(:max_length) { 3 } diff --git a/spec/rails_param/validator/max_spec.rb b/spec/rails_param/validator/max_spec.rb index 4ea1af6..82054e7 100644 --- a/spec/rails_param/validator/max_spec.rb +++ b/spec/rails_param/validator/max_spec.rb @@ -5,6 +5,7 @@ let(:value) { 50 } let(:options) { { max: max } } let(:type) { Integer } + let(:locale) { :en } let(:parameter) do RailsParam::Parameter.new( name: name, @@ -17,6 +18,8 @@ subject { described_class.new(parameter) } describe "#validate!" do + before { I18n.locale = locale } + context "value given is valid" do let(:max) { 50 } diff --git a/spec/rails_param/validator/min_length_spec.rb b/spec/rails_param/validator/min_length_spec.rb index ef73246..2330a67 100644 --- a/spec/rails_param/validator/min_length_spec.rb +++ b/spec/rails_param/validator/min_length_spec.rb @@ -5,6 +5,7 @@ let(:value) { "bar" } let(:options) { { min_length: min_length } } let(:type) { String } + let(:locale) { :en } let(:parameter) do RailsParam::Parameter.new( name: name, @@ -17,6 +18,8 @@ subject { described_class.new(parameter) } describe "#validate!" do + before { I18n.locale = locale } + context "value given is valid" do let(:min_length) { 3 } diff --git a/spec/rails_param/validator/min_spec.rb b/spec/rails_param/validator/min_spec.rb index 1c33ed0..e7294ac 100644 --- a/spec/rails_param/validator/min_spec.rb +++ b/spec/rails_param/validator/min_spec.rb @@ -5,6 +5,7 @@ let(:value) { 50 } let(:options) { { min: min } } let(:type) { Integer } + let(:locale) { :en } let(:parameter) do RailsParam::Parameter.new( name: name, @@ -17,6 +18,8 @@ subject { described_class.new(parameter) } describe "#validate!" do + before { I18n.locale = locale } + context "value given is valid" do let(:min) { 50 } diff --git a/spec/rails_param/validator/required_spec.rb b/spec/rails_param/validator/required_spec.rb index 0d935d7..e5c2654 100644 --- a/spec/rails_param/validator/required_spec.rb +++ b/spec/rails_param/validator/required_spec.rb @@ -4,6 +4,7 @@ let(:name) { "foo" } let(:options) { { required: true } } let(:type) { String } + let(:locale) { :en } let(:parameter) do RailsParam::Parameter.new( name: name, @@ -16,6 +17,8 @@ subject { described_class.new(parameter) } describe "#validate!" do + before { I18n.locale = locale } + context "value given is present" do let(:value) { "bar" }