<img src=“https://secure.travis-ci.org/magiclabs/localized_country_select.png?branch=master” alt=“Build Status” /> <img src=“http://stillmaintained.com/magiclabs/localized_country_select.png” alt=“Development Status” />

LocalizedCountrySelect

Rails gem to provide support for localized <select> menu with country names and for storing country information as country code (eg. ‘es’), not name (eg. ‘Spain’), in the database.

Installation

Put this line into you Gemfile:

gem "magic-localized_country_select", "~> 0.2.0"

Usage

Uses the Rails internationalization framework (I18n, rails-i18n.org) for translating the names of countries. Requires Rails 3 or later versions. Country names are loaded from hashes in config/locale directory, according to I18n.locale value.

You can easily translate country codes in your application like this:

<%= I18n.t @user.country, :scope => 'countries' %>

Comes with a 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) Don’t forget to restart the application when you add new locale.

ActionView helper code is adapted from Rails’ default country_select plugin (previously in core). See github.com/rails/country_select/tree/master/lib/country_select.rb

Examples

Show the complete country name:

<%= country_select(:user, :country, {}, :include_blank => 'Please choose...') %>

will become:

<select name="user[country]" id="user_country">
<option value="">Please choose...</option>
<option disabled="disabled" value="">-------------</option>
<option value="AF">Afghanistan</option>
...
<option value="ZW">Zimbabwe</option>
</select>

Show only the country codes:

<%= country_select(:user, :country, {:description => :abbreviated}, :include_blank => 'Please choose...') %>

will become:

<select name="user[country]" id="user_country">
<option value="">Please choose...</option>
<option disabled="disabled" value="">-------------</option>
<option value="AF">AF</option>
...
<option value="ZW">ZW</option>
</select>

for the en locale.

Other resources

Copyright © 2008 Karel Minarik (www.karmi.cz), released under the MIT license