Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rails plugin for localized "country_select" helper with Rake task for downloading locales from Unicode.org's CLDR

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.textile

LocalizedCountrySelect

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

Uses the Rails internationalization framework (I18n) for translating the names of countries.
Requires Rails 2.2 (released November 21st, 2008) or later versions.
Country names are loaded from hashes in plugin directory, according to I18n.locale value.

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

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

Generating country locale files

The project comes with both rake tasks and a generator to generate and import locale files for countries:

Rails 3 generator

The project now comes with a Rails 3 generator to import locale files and place them in config/locales in your Rails 3 app.

rails g localized_country_select:import de ru --file-ext yml

Rake import tasks

The following Rake tasks can be used for importing country names from Unicode.org’s CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)

  • rake import:country_select:lang
  • rake yaml_locales:country_select:lang

Note: You need to replace lang variable with lang you need.

The import one generates ruby files, the yaml_locales task generates yml files.

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 http://github.com/rails/country_select/tree/master/lib/country_select.rb

Install & Usage

$ rails plugin install https://github.com/kristianmandrup/localized_language_select.git

It should be converted into a gem…

Example

  <%= localized_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>  

for the en locale.

Options

:exclude option

  <%= localized_country_select(:user, :country, ['English'], :include_blank => 'Please choose...', :exclude => ['Afrikaans']) %>  

:only option

  <%= localized_country_select(:user, :country, ['English'], :include_blank => 'Please choose...', :only => ['German', 'French']) %>  

Important!

Add translations for your language requirements using the rake tasks supplied, then use the generator to copy them into the Rails locales folder.

Framework usage

This plugin should also integrate nicely with various formbuilders, including Formtastic

Formtastic example:

<%= semantic_form_for @user do |form| %>  
  ...
  <%= form.input :home_country, :as => :country, :priority_countries => ['USA'], :include_blank => 'Please choose country...', :only => ['USA', 'Germany', 'France']) %>    
  ...
<% end %>  

Copyright

Other resources

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

Something went wrong with that request. Please try again.