Skip to content
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

Localize dates in error messages #150

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
114 changes: 114 additions & 0 deletions lib/generators/validates_timeliness/templates/fr.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/validates_timeliness/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/validates_timeliness/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down