Module: LocalizedCountrySelect
- Defined in:
- lib/localized_country_select.rb,
lib/localized_country_select/railtie.rb,
lib/localized_country_select/version.rb
Overview
LocalizedCountrySelect
View helper for displaying select list with countries:
localized_country_select(:user, :country)
Works just like the default Rails’ country_select
plugin, but stores countries as country codes, not names, in the database.
You can easily translate country codes in your application like this:
<%= I18n.t @user.country, :scope => 'countries' %>
Uses the Rails internationalization framework (I18n) for translating the names of countries.
Use Rake task rake import:country_select 'de'
for importing country names from Unicode.org’s CLDR repository (www.unicode.org/cldr/data/charts/summary/root.html)
Code adapted from Rails’ default country_select
plugin (previously in core) See github.com/rails/country_select/tree/master/lib/country_select.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
'0.9.11'
Class Method Summary collapse
-
.localized_countries_array(options = {}) ⇒ Object
Returns array with codes and localized country names (according to
I18n.locale
) for<option>
tags. -
.priority_countries_array(country_codes = [], options = {}) ⇒ Object
Return array with codes and localized country names for array of country codes passed as argument == Example priority_countries_array([:TW, :CN]) # => [ [‘Taiwan’, ‘TW’], [‘China’, ‘CN’] ].
Class Method Details
.localized_countries_array(options = {}) ⇒ Object
Returns array with codes and localized country names (according to I18n.locale
) for <option>
tags
26 27 28 29 30 31 32 33 34 |
# File 'lib/localized_country_select.rb', line 26 def localized_countries_array(={}) exclude = Array([:exclude]).map {|code| code.to_s.upcase } if([:description]==:abbreviated) I18n.translate(:countries).map { |key, value| [key.to_s.upcase] if !exclude.include?(key.to_s.upcase) } else I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] if !exclude.include?(key.to_s.upcase) } end.compact.sort_by { |country| country.first.parameterize } end |
.priority_countries_array(country_codes = [], options = {}) ⇒ Object
Return array with codes and localized country names for array of country codes passed as argument
Example
priority_countries_array([:TW, :CN])
# => [ ['Taiwan', 'TW'], ['China', 'CN'] ]
39 40 41 42 43 44 45 46 |
# File 'lib/localized_country_select.rb', line 39 def priority_countries_array(country_codes=[],={}) if([:description]==:abbreviated) country_codes.map { |code| [code.to_s.upcase] } else countries = I18n.translate(:countries) country_codes.map { |code| [countries[code.to_s.upcase.to_sym], code.to_s.upcase] } end end |