Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rails plugin for providing a localized "language_select" helper

branch: master
README.textile

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' %>  

Generating language locale files

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

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_language_select:import de ru --file-ext yml

Rake import tasks

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

  • rake import:language_select locale=de
  • rake yaml_locales:language_select locale=de

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.

The code borrows heavily from the LocalizedCountrySelect plugin – 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']) %>  

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 :lang, :as => :language, :priority_languages => ['English'], :include_blank => 'Please choose...', :only => ['German', 'French']) %>    
  ...
<% end %>  

Copyright

Copyright © 2009 David Cato, released under the MIT license

Something went wrong with that request. Please try again.