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

.to_s should return the translation of the current locale and fall back to name if no translation present. #288

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/countries/country.rb
Expand Up @@ -92,7 +92,7 @@ def in_eu?
end

def to_s
@data['name']
translation(I18n.locale.to_s) || @data['name']
end

def translation(locale = 'en')
Expand Down Expand Up @@ -125,7 +125,7 @@ def all_translated(locale = 'en')

def all_names_with_codes(locale = 'en')
ISO3166::Country.all.map do |c|
[(c.translation(locale) || c.name ).html_safe, c.alpha2]
[(c.to_s).html_safe, c.alpha2]
end.sort_by { |d| d[0] }
end

Expand Down
14 changes: 12 additions & 2 deletions spec/country_spec.rb
Expand Up @@ -600,8 +600,18 @@
end

describe 'to_s' do
it 'should return the country name' do
expect(ISO3166::Country.new('GB').to_s).to eq('United Kingdom')
it 'should return the country first translation for locale' do
I18n.available_locales = [:en, :de]
I18n.with_locale(:de) do
expect(ISO3166::Country.new('GB').to_s).to eq('Vereinigtes Königreich')
end
end

it 'should return the country name if no translation is present' do
I18n.available_locales = [:en, :de, :derp]
I18n.with_locale(:derp) do
expect(ISO3166::Country.new('GB').to_s).to eq('United Kingdom')
end
end
end

Expand Down