Skip to content

Commit

Permalink
I18N support
Browse files Browse the repository at this point in the history
  • Loading branch information
rderoldan1 committed Aug 5, 2013
1 parent 2e52efc commit 73c78b8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
V0.2.2
========
* Country Name Locales

V.0.1.0
========
* Helper method in order to get country info.
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ Assuming that your User model store country cod.
<p>Country Apha3s: <%= country(:num => @user.country_num)[3] %><p>
```

### Locales

By default all county names are in english, but you can change this behavior by include country names in
your locale file under `countries:` scope

```yml
es:
countries:
AC: "Isla de la Ascensión"
AD: "Andorra"
AE: "Emiratos Árabes Unidos"
AF: "Afganistán"
AG: "Antigua y Barbuda"
AI: "Anguila"
AL: "Albania"
AM: "Armenia"
```
Please look at [@onomojo](https://github.com/onomojo) [repo](https://github.com/onomojo/i18n-country-translations/tree/master/rails/locale) for all translations
## Contributing
1. Fork it
Expand Down
2 changes: 1 addition & 1 deletion lib/country_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
'Portugal',
'Puerto Rico',
'Qatar',
'Réunion',
'Reunion',
'Romania',
'Russian Federation',
'Rwanda',
Expand Down
17 changes: 12 additions & 5 deletions lib/rails_country_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def country_select(object, method, options = {}, html_options = {})
options[:values] = :nums unless options.has_key?(:values)

potential = {
:names => COUNTRY_NAMES,
:names => translate_countries,
:nums => COUNTRY_NUMS,
:alpha2s => COUNTRY_ALPHA2S,
:alpha3s => COUNTRY_ALPHA3S
}

select_options = potential[options[:keys]].zip(potential[options[:values]])
InstanceTag.new(object, method, self, options.delete(:object)).to_select_tag(select_options, options, html_options)
end
Expand All @@ -32,13 +32,20 @@ def country_select(method, options = {}, html_options = {})
# get country info given some params
def country(options = {:name => "", :num =>"", :alpha2s => "", :alpha3s => ""})
result = []
COUNTRY_NUMS.zip(COUNTRY_NAMES, COUNTRY_ALPHA2S, COUNTRY_ALPHA3S).each do |country|
if options[:nums].to_i.eql?(country[0]) || options[:name].eql?(country[1]) || options[:alpha2s].eql?(country[2]) || options[:alpha3s].eql?(country[3])
result = country
COUNTRY_NUMS.zip(translate_countries, COUNTRY_ALPHA2S, COUNTRY_ALPHA3S).each do |country|
if options[:num].to_i.eql?(country[0]) || options[:name].eql?(country[1]) || options[:alpha2s].eql?(country[2]) || options[:alpha3s].eql?(country[3])
result = country
end
end
result
end

private
def translate_countries
COUNTRY_ALPHA2S.zip(COUNTRY_NAMES).map do |code, name|
I18n.t(code, :scope => :countries, :default => name)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rails_country_select.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.email = ["[email protected]"]
spec.description = %q{Select tag for countries in rails form, based in LukeCarrier rails 2 plugin }
spec.summary = %q{Select tag for countries in rails form}
spec.homepage = ""
spec.homepage = "https://github.com/rderoldan1/rails_country_select"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
Expand Down

0 comments on commit 73c78b8

Please sign in to comment.