Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merged

  • Loading branch information...
commit 5c155aab2e359142f8f9f8e4b72762ce458cd5b4 2 parents 51cb552 + d4b3e88
@kristianmandrup authored
View
21 README.textile
@@ -116,6 +116,27 @@ Formtastic example:
<% end %>
</pre>
+h2. Formbuilder configuration
+
+* ActionView
+* Formtastic
+* Simpleform (please!)
+
+h3. ActionView
+
+<pre>
+require 'localized_language/action_views_select'
+</pre>
+
+h3. Formtastic
+
+Will auto detect formtastic major version and load the appropriate select helper.
+Should work with Formtastic 2.0
+
+<pre>
+require 'localized_language/formtastic_select'
+</pre>
+
h2. Copyright
Copyright (c) 2009 David Cato, released under the MIT license
View
113 lib/localized_language_select.rb
@@ -1,20 +1,20 @@
# = 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
#
@@ -27,23 +27,23 @@ class << self
# for <tt><option></tt> tags
def localized_languages_array options = {}
res = []
- list = I18n.translate(:languages).each do |key, value|
+ list = I18n.translate(:languages).each do |key, value|
res << [value, key.to_s] if include_language?(key.to_s, options)
end
res.sort_by { |country| country.first.parameterize }
end
-
- def include_language?(key, options)
- if options[:only]
+
+ def include_language?(key, options)
+ if options[:only]
return options[:only].include?(key)
- end
- if options[:except]
+ end
+ if options[:except]
return !options[:except].include?(key)
end
- true
- end
-
-
+ true
+ end
+
+
# Return array with codes and localized language names for array of language codes passed as argument
# == Example
# priority_languages_array([:de, :fr, :en])
@@ -54,82 +54,3 @@ def priority_languages_array(language_codes=[])
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
- alias_method :language_select, :localized_language_select
-
- # 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).html_safe,
- { "name" => name, "id" => name }.update(html_options.stringify_keys)
- end
- alias_method :language_select_tag, :localized_language_select_tag
-
- # 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, options = {})
- 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(options), selected)
- end
- alias_method :language_options_for_select, :localized_language_options_for_select
-
- 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).html_safe,
- 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
- alias_method :language_select, :localized_language_select
- end
-
- end
-end
-
-
-class LanguageInput < Formtastic::Inputs::SelectInput
-
- def collection
- result = Array.new
- priority_languages = input_options.delete(:priority_languages) || nil
- if priority_languages
- result += LocalizedLanguageSelect::priority_languages_array(priority_languages)
- result << ["----------", ""]
- end
- result += LocalizedLanguageSelect::localized_languages_array(options)
- end
-end
View
21 lib/localized_language_select/i18n.rb
@@ -1,21 +0,0 @@
-# encoding: utf-8
-
-# TODO !!! ("stolen" from Formtastic, but can't seem to make it work... waiting for engines to do this magic OOTB !)
-
-# module LocalizedLanguageSelect
-# module I18n
-# DEFAULT_SCOPE = [:languages].freeze
-# DEFAULT_VALUES = YAML.load_file(File.expand_path("../../../locale/en.yml", __FILE__))["en"]["languages"].freeze
-#
-# class << self
-# def translate(*args)
-# key = args.shift.to_sym
-# options = args.extract_options!
-# options.reverse_merge!(:default => DEFAULT_VALUES[key])
-# options[:scope] = [DEFAULT_SCOPE, options[:scope]].flatten.compact
-# ::I18n.translate(key, *(args << options))
-# end
-# alias :t :translate
-# end
-# end
-# end
Please sign in to comment.
Something went wrong with that request. Please try again.