LocalizedLanguageSelect
Rails plugin to provide support for localized menu with language names and for storing language information as language code (eg. ‘sv’), not name (eg. ‘Swedish’), in the database.
Uses the Rails internationalization framework (I18n) for translating the names of languages. Requires Rails 2.2 (released November 21st, 2008) or later versions.
Language names are loaded from hashes in plugin directory, according to I18n.locale value.
You can easily translate language codes in your application like this:
<%= I18n.t @user.language, :scope => 'languages' %>
Comes with a Rake task rake import:language_select locale=de for importing language names from Unicode.org’s CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html) Don’t forget to restart the application when you add new locale.
The code borrows heavily from the LocalizedCountrySelect plugin. See localized_country_select
Install & Usage
Install as plugin:
$ rails plugin install https://github.com/kristianmandrup/localized_language_select.git
Install as gem:
Insert in Gemfile
gem 'localized_language_select'
Then to instal it, run:
$ bundle install
Then to install the locale files, run:
$ rails g localized_language_select:locales
This will copy the locale files to your Rails config/locales dir. The files will be called ‘language.en.yml’ and so on…
Note: you can specify which locales using the --locales
option, which takes a list of locales.
$ rails g localized_language_select:locales --locales da en fr
Usage example
<%= localized_language_select(:user, :language, [], :include_blank => 'Please choose...') %>
will become:
<select name="user[language]" id="user_language">
<option value="">Please choose...</option>
<option disabled="disabled" value="">-------------</option>
<option value="aa">Afar</option>
...
<option value="zza">ZaZa</option>
</select>
for the en locale.
Options
:exclude
option
<%= localized_language_select(:user, :language, ['English'], :include_blank => 'Please choose...', :exclude => ['Afrikaans']) %>
:only
option
<%= localized_language_select(:user, :language, ['English'], :include_blank => 'Please choose...', :only => ['German', 'French']) %>
Todo – Request
Please add translations for your language!
Would be nice to have at least: German, Spanish, Chinese, Japanese, Russian, Indian and Arabic (Persian?) in there as well…
Framework usage
This plugin should also integrate nicely with various formbuilders, including Formtastic
Formtastic example:
<%= semantic_form_for @user do |form| %>
...
<%= form.input :lang, :as => :language, :priority_languages => ['English'], :include_blank => 'Please choose...', :only => ['German', 'French']) %>
...
<% end %>
Copyright
Copyright © 2009 David Cato, released under the MIT license