Skip to content

Commit

Permalink
Merge pull request ruby-i18n#117 from e-travel/gt-msgctxt
Browse files Browse the repository at this point in the history
Gettext pgettext and msgctxt support
  • Loading branch information
Sven Fuchs committed Nov 7, 2011
2 parents da09d52 + 28cf411 commit 89ea337
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/i18n/backend/gettext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def parse(filename)
def normalize(locale, data)
data.inject({}) do |result, (key, value)|
unless key.nil? || key.empty?
key = key.gsub(I18n::Gettext::CONTEXT_SEPARATOR, '|')
key, value = normalize_pluralization(locale, key, value) if key.index("\000")

parts = key.split('|').reverse
Expand Down
8 changes: 8 additions & 0 deletions test/gettext/backend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def test_looks_up_a_namespaced_translation
I18n.locale = :de
assert_equal 'Räderzahl', sgettext('Car|Wheels count')
assert_equal 'Räderzahl', pgettext('Car', 'Wheels count')
assert_equal 'Räderzahl!', pgettext('New car', 'Wheels count')
end

def test_uses_namespaced_default_translation
assert_equal 'Wheels count', sgettext('Car|Wheels count')
assert_equal 'Wheels count', pgettext('Car', 'Wheels count')
assert_equal 'Wheels count', pgettext('New car', 'Wheels count')
end

def test_pluralizes_entry
Expand All @@ -67,13 +69,17 @@ def test_pluralizes_namespaced_entry
assert_equal 'Räder', nsgettext('Car|wheel', 'wheels', 2)
assert_equal 'Rad', npgettext('Car', 'wheel', 'wheels', 1)
assert_equal 'Räder', npgettext('Car', 'wheel', 'wheels', 2)
assert_equal 'Rad!', npgettext('New car', 'wheel', 'wheels', 1)
assert_equal 'Räder!', npgettext('New car', 'wheel', 'wheels', 2)
end

def test_pluralizes_namespaced_default_entry
assert_equal 'wheel', nsgettext('Car|wheel', 'wheels', 1)
assert_equal 'wheels', nsgettext('Car|wheel', 'wheels', 2)
assert_equal 'wheel', npgettext('Car', 'wheel', 'wheels', 1)
assert_equal 'wheels', npgettext('Car', 'wheel', 'wheels', 2)
assert_equal 'wheel', npgettext('New car', 'wheel', 'wheels', 1)
assert_equal 'wheels', npgettext('New car', 'wheel', 'wheels', 2)
end

def test_pluralizes_namespaced_entry_with_alternative_syntax
Expand All @@ -82,6 +88,8 @@ def test_pluralizes_namespaced_entry_with_alternative_syntax
assert_equal 'Räder', nsgettext(['Car|wheel', 'wheels'], 2)
assert_equal 'Rad', npgettext('Car', ['wheel', 'wheels'], 1)
assert_equal 'Räder', npgettext('Car', ['wheel', 'wheels'], 2)
assert_equal 'Rad!', npgettext('New car', ['wheel', 'wheels'], 1)
assert_equal 'Räder!', npgettext('New car', ['wheel', 'wheels'], 2)
end

def test_ngettextpluralizes_entry_with_dots
Expand Down
10 changes: 10 additions & 0 deletions test/test_data/locales/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ msgstr "Modell"
msgid "Car|Wheels count"
msgstr "Räderzahl"

msgctxt "New car"
msgid "Wheels count"
msgstr "Räderzahl!"

#: app/views/cars/show.html.erb:7
msgid "Created"
msgstr "Erstellt"
Expand All @@ -66,6 +70,12 @@ msgid_plural "Car|wheels"
msgstr[0] "Rad"
msgstr[1] "Räder"

msgctxt "New car"
msgid "wheel"
msgid_plural "wheels"
msgstr[0] "Rad!"
msgstr[1] "Räder!"

msgid "On %{count} wheel."
msgid_plural "On %{count} wheels."
msgstr[0] "Auf %{count} Achse."
Expand Down

0 comments on commit 89ea337

Please sign in to comment.