Permalink
Browse files

Initial commit

  • Loading branch information...
davec committed Jul 4, 2009
0 parents commit 7d319f151f70f4fb6beba9a1795c24b982efba86
Showing with 1,890 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +37 −0 README.rdoc
  3. +24 −0 Rakefile
  4. +5 −0 init.rb
  5. +4 −0 install.rb
  6. +98 −0 lib/localized_language_select.rb
  7. +510 −0 locale/en.rb
  8. +509 −0 locale/fr.rb
  9. +509 −0 locale/nl.rb
  10. +87 −0 tasks/localized_language_select_tasks.rake
  11. +86 −0 test/localized_language_select_test.rb
  12. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
+= LocalizedLanguageSelect
+
+Rails plugin to provide support for localized <tt><select></tt> 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, http://rails-i18n.org) 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 <tt>I18n.locale</tt> value.
+
+You can easily translate language codes in your application like this:
+
+ <%= I18n.t @user.language, :scope => 'languages' %>
+
+Comes with a Rake task <tt>rake import:language_select 'de'</tt> 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 http://github.com/karmi/localized_country_select
+
+== 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 <tt>en</tt> locale.
+
+Copyright (c) 2009 David Cato, released under the MIT license
@@ -0,0 +1,24 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+load File.join(File.dirname(__FILE__), 'tasks', 'localized_language_select_tasks.rake')
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the localized_language_select plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the localized_language_select plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'LocalizedLanguageSelect'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,5 @@
+# Require the plugin code
+require 'localized_language_select'
+
+# Load locales for languages from +locale+ directory into Rails
+I18n.load_path += Dir[ File.join(File.dirname(__FILE__), 'locale', '*.{rb,yml}') ]
@@ -0,0 +1,4 @@
+# Show the README text file
+puts "\n\n"
+puts IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
+puts "\n"
@@ -0,0 +1,98 @@
+# = LocalizedLanguageSelect
+#
+# View helper for displaying select list with languages:
+#
+# localized_language_select(:user, :language)
+#
+# Works just like the default Rails' +country_select+ plugin, but stores languages as
+# language *codes*, not *names*, in the database.
+#
+# You can easily translate language codes in your application like this:
+# <%= I18n.t @user.language, :scope => 'languages' %>
+#
+# Uses the Rails internationalization framework (I18n) for translating the names of languages.
+#
+# Use Rake task <tt>rake import:language_select 'de'</tt> for importing language names
+# from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
+#
+# The code borrows heavily from the LocalizedCountrySelect plugin.
+# See http://github.com/karmi/localized_country_select
+#
+module LocalizedLanguageSelect
+ class << self
+ # Returns array with codes and localized language names (according to <tt>I18n.locale</tt>)
+ # for <tt><option></tt> tags
+ def localized_languages_array
+ I18n.translate(:languages).map { |key, value| [value, key.to_s.downcase] }.sort
+ end
+ # Return array with codes and localized language names for array of language codes passed as argument
+ # == Example
+ # priority_languages_array([:de, :fr, :en])
+ # # => [ ['German', 'de'], ['French', 'fr'], ['English', 'en'] ]
+ def priority_languages_array(language_codes=[])
+ languages = I18n.translate(:languages)
+ language_codes.map { |code| [languages[code], code.to_s.downcase] }
+ end
+ end
+end
+
+module ActionView
+ module Helpers
+
+ module FormOptionsHelper
+
+ # Return select and option tags for the given object and method, using +localized_language_options_for_select+
+ # to generate the list of option tags. Uses <b>language code</b>, not name as option +value+.
+ # Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
+ def localized_language_select(object, method, priority_languages = nil, options = {}, html_options = {})
+ InstanceTag.new(object, method, self, options.delete(:object)).
+ to_localized_language_select_tag(priority_languages, options, html_options)
+ end
+
+ # Return "named" select and option tags according to given arguments.
+ # Use +selected_value+ for setting initial value
+ # It behaves likes older object-binded brother +localized_language_select+ otherwise
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
+ def localized_language_select_tag(name, selected_value = nil, priority_languages = nil, html_options = {})
+ content_tag :select,
+ localized_language_options_for_select(selected_value, priority_languages),
+ { "name" => name, "id" => name }.update(html_options.stringify_keys)
+ end
+
+ # Returns a string of option tags for languages according to locale. Supply the language code in lower-case ('en', 'de')
+ # as +selected+ to have it marked as the selected option tag.
+ # Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
+ def localized_language_options_for_select(selected = nil, priority_languages = nil)
+ language_options = ""
+ if priority_languages
+ language_options += options_for_select(LocalizedLanguageSelect::priority_languages_array(priority_languages), selected)
+ language_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
+ end
+ return language_options + options_for_select(LocalizedLanguageSelect::localized_languages_array, selected)
+ end
+
+ end
+
+ class InstanceTag
+ def to_localized_language_select_tag(priority_languages, options, html_options)
+ html_options = html_options.stringify_keys
+ add_default_name_and_id(html_options)
+ value = value(object)
+ content_tag("select",
+ add_options(
+ localized_language_options_for_select(value, priority_languages),
+ options, value
+ ), html_options
+ )
+ end
+ end
+
+ class FormBuilder
+ def localized_language_select(method, priority_languages = nil, options = {}, html_options = {})
+ @template.localized_language_select(@object_name, method, priority_languages, options.merge(:object => @object), html_options)
+ end
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 7d319f1

Please sign in to comment.