From d9acfe6156d8c10e477e7df534f3aed1dc197f6a Mon Sep 17 00:00:00 2001 From: Florent Vaucelle Date: Fri, 3 Feb 2017 18:13:24 +0100 Subject: [PATCH] =?UTF-8?q?[Localization]=C2=A0Use=20localized=20dates=20i?= =?UTF-8?q?n=20error=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validates_timeliness/templates/fr.yml | 114 ++++++++++++++++++ lib/validates_timeliness/validator.rb | 2 +- spec/spec_helper.rb | 4 +- spec/validates_timeliness/validator_spec.rb | 11 ++ 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 lib/generators/validates_timeliness/templates/fr.yml diff --git a/lib/generators/validates_timeliness/templates/fr.yml b/lib/generators/validates_timeliness/templates/fr.yml new file mode 100644 index 00000000..e1cf8ab8 --- /dev/null +++ b/lib/generators/validates_timeliness/templates/fr.yml @@ -0,0 +1,114 @@ +fr: + errors: + messages: + invalid_date: "n'est pas une date valide" + invalid_time: "n'est pas une heure valide" + invalid_datetime: "n'est pas une date valide" + is_at: "doit être un(e) %{restriction}" + before: "doit être avant %{restriction}" + on_or_before: "doit être le ou avant le %{restriction}" + after: "doit être après %{restriction}" + on_or_after: "doit être le ou après le %{restriction}" + validates_timeliness: + error_value_formats: + date: '%d %B %Y' + time: '%H:%M:%S' + datetime: '%Y-%m-%d %H:%M:%S' + date: + abbr_day_names: + - dim + - lun + - mar + - mer + - jeu + - ven + - sam + abbr_month_names: + - + - jan. + - fév. + - mar. + - avr. + - mai + - juin + - juil. + - août + - sept. + - oct. + - nov. + - déc. + day_names: + - dimanche + - lundi + - mardi + - mercredi + - jeudi + - vendredi + - samedi + formats: + default: "%d/%m/%Y" + short: "%e %b" + long: "%e %B %Y" + month_names: + - + - janvier + - février + - mars + - avril + - mai + - juin + - juillet + - août + - septembre + - octobre + - novembre + - décembre + order: + - :day + - :month + - :year + datetime: + distance_in_words: + about_x_hours: + one: environ une heure + other: environ %{count} heures + about_x_months: + one: environ un mois + other: environ %{count} mois + about_x_years: + one: environ un an + other: environ %{count} ans + almost_x_years: + one: presqu'un an + other: presque %{count} ans + half_a_minute: une demi-minute + less_than_x_minutes: + zero: moins d'une minute + one: moins d'une minute + other: moins de %{count} minutes + less_than_x_seconds: + zero: moins d'une seconde + one: moins d'une seconde + other: moins de %{count} secondes + over_x_years: + one: plus d'un an + other: plus de %{count} ans + x_days: + one: 1 jour + other: "%{count} jours" + x_minutes: + one: 1 minute + other: "%{count} minutes" + x_months: + one: 1 mois + other: "%{count} mois" + x_seconds: + one: 1 seconde + other: "%{count} secondes" + prompts: + day: Jour + hour: Heure + minute: Minute + month: Mois + second: Seconde + year: Année diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 0e2c8b44..523e39ff 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -97,7 +97,7 @@ def add_error(record, attr_name, message, value=nil) def format_error_value(value) format = I18n.t(@type, :default => DEFAULT_ERROR_VALUE_FORMATS[@type], :scope => 'validates_timeliness.error_value_formats') - value.strftime(format) + I18n.localize(value, format: format) end def attribute_raw_value(record, attr_name) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a1fc8bcd..f3ca580b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,8 +24,10 @@ Time.zone = 'Australia/Melbourne' LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/../lib/generators/validates_timeliness/templates/en.yml') +LOCALE_PATH_FR = File.expand_path(File.dirname(__FILE__) + '/../lib/generators/validates_timeliness/templates/fr.yml') I18n.load_path.unshift(LOCALE_PATH) -I18n.available_locales = ['en', 'es'] +I18n.load_path.unshift(LOCALE_PATH_FR) +I18n.available_locales = ['en', 'es', 'fr'] # Extend TestModel as you would another ORM/ODM module module TestModelShim diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 69ec35a4..79fbdb7e 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -212,6 +212,17 @@ class PersonWithFormatOption validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_datetime], :type => :datetime) expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('2010-01-01 12:34:56') end + + it 'should format date error value as %d %B %Y in the right locale' do + locale = I18n.locale + begin + I18n.locale = :fr + validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date) + expect(validator.format_error_value(Date.new(2010,1,1))).to eq('01 janvier 2010') + ensure + I18n.locale = locale + end + end end describe "with missing translation" do