Skip to content

Commit

Permalink
Merge pull request #464 from i7an/localize_with_missing_translations
Browse files Browse the repository at this point in the history
Fix #localize  return error message if translation data is missing
  • Loading branch information
radar authored Jan 21, 2019
2 parents 4fb3970 + 8fc72b0 commit 548901b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
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

0 comments on commit 548901b

Please sign in to comment.