Localized country select plugin for Rails
As you may know the country_select helper, giving you a dropdown menu with country names, was recently kicked out from Rails core into a "country select" plugin.
This was inevitable in a way, because the helper was storing country names in the database, making any localization or calculations kinda discursive. Neverthless, the primary reson for deprecation was a political debate, whether „Taiwan, Province of China“ is a String everybody is happy having in Rails codebase. (Read Michael Koziarski's recap if you're interested in political aspects of developing open-source software.)
On a current project, I needed a way to:
- store country codes, not names in the database,
- localize said codes in application's interface,
and developed a localized country_select Rails plugin, with source available at Github. It may solve your problem, if you have similar needs.
You can use it for rendering the <select> menu just as the original helper:
<%= localized_country_select(:user, :country) %>
When you need to translate the stored value in your application, you can do that just like this:
<%= I18n.t @user.country, :scope => 'countries' %>
The plugin code is based on Rails "country_select" plugin. Thanks to the Ruby/Rails i18n project the localization code was so simple, it almost hurts:
module LocalizedCountrySelect
class << self
def localized_countries_array
I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] }.sort
end
def priority_countries_array(country_codes=[])
countries = I18n.translate(:countries)
country_codes.map { |code| [countries[code], code.to_s.upcase] }
end
end
end
The plugin code is quite well documented and has basic suite of unit tests.
If you still didn't checked Rails' new internationalization/localization capabilities, you should stop what you're doing and do that right now.
The plugin comes with two localization dictionaries: for English (en-US) and Czech (cz). But that's not all you have.
You can download and save country codes and names for more than one hundred languages, thanks to provided Rake task, which parses Unicode.org's vast Common Locale Data Repository (CLDR) archive. It doesn't download the XML sources from CLDR, because that would be kinda overkill, wouldn't it? It's deliberately very naive & procedural, so it's easy to follow what's going on.
However, if you're interested in this, keep tabs on the Ruby/Rails i18n team CLDR project at Github. Downloading, parsing and storing data from CLDR is definitely the future of Rails aplications' automatic/automagic localization: imagine not only be able get month names for most languages, but also parse complex pluralization rules for languages like Czech into Ruby lambdas... Now that I would call nice way how to work with localization.
Update I
I've added also complementary localized_country_select_tag method for a „named“ <select> menu with countries now. You can use the helper for "anonymous" or "inconsistent" data structures like "competition_submission[data][citizenship]" (like I have to do today). Looks like we are for another one, localized_nationality_select...
Update II
Beware with stuff in Rails' i18n moving very fast. Recently the default locale was changed from en-US to en. I am keeping the plugin updated.
Join the discussion