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

Fix #localize return error message if translation data is missing #464

Merged
merged 1 commit into from
Jan 21, 2019
Merged
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
14 changes: 8 additions & 6 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,16 @@ def load_json(filename)
def translate_localization_format(locale, object, format, options)
format.to_s.gsub(/%[aAbBpP]/) do |match|
case match
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
when '%P' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
when '%a' then I18n.t!(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t!(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t!(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t!(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%p' then I18n.t!(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
when '%P' then I18n.t!(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
end
end
rescue MissingTranslationData => e
e.message
end

def pluralization_key(entry, count)
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tests/localization/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def setup
assert_equal 'Mar', I18n.l(@date, :format => '%b', :locale => :de)
end

test "localize Date: given missing translations it returns the correct error message" do
assert_equal 'translation missing: fr.date.abbr_month_names', I18n.l(@date, :format => '%b', :locale => :fr)
end

test "localize Date: given an unknown format it does not fail" do
assert_nothing_raised { I18n.l(@date, :format => '%x') }
end
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tests/localization/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def setup
assert_equal 'Mar', I18n.l(@datetime, :format => '%b', :locale => :de)
end

test "localize DateTime: given missing translations it returns the correct error message" do
assert_equal 'translation missing: fr.date.abbr_month_names', I18n.l(@datetime, :format => '%b', :locale => :fr)
end

test "localize DateTime: given a meridian indicator format it returns the correct meridian indicator" do
assert_equal 'AM', I18n.l(@datetime, :format => '%p', :locale => :de)
assert_equal 'PM', I18n.l(@other_datetime, :format => '%p', :locale => :de)
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tests/localization/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def setup
assert_equal 'Mar', I18n.l(@time, :format => '%b', :locale => :de)
end

test "localize Time: given missing translations it returns the correct error message" do
assert_equal 'translation missing: fr.date.abbr_month_names', I18n.l(@time, :format => '%b', :locale => :fr)
end

test "localize Time: given a meridian indicator format it returns the correct meridian indicator" do
assert_equal 'AM', I18n.l(@time, :format => '%p', :locale => :de)
assert_equal 'PM', I18n.l(@other_time, :format => '%p', :locale => :de)
Expand Down