localized_language_select 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "rails", ">= 2.3.5"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec", ">= 2.3.0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.5.2"
14
+ gem "rcov", ">= 0"
15
+ end
@@ -0,0 +1,90 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.3)
6
+ actionpack (= 3.0.3)
7
+ mail (~> 2.2.9)
8
+ actionpack (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.6)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.3)
23
+ activemodel (= 3.0.3)
24
+ activesupport (= 3.0.3)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ activesupport (3.0.3)
31
+ arel (2.0.6)
32
+ builder (2.1.2)
33
+ diff-lcs (1.1.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ git (1.2.5)
37
+ i18n (0.5.0)
38
+ jeweler (1.5.2)
39
+ bundler (~> 1.0.0)
40
+ git (>= 1.2.5)
41
+ rake
42
+ mail (2.2.14)
43
+ activesupport (>= 2.3.6)
44
+ i18n (>= 0.4.0)
45
+ mime-types (~> 1.16)
46
+ treetop (~> 1.4.8)
47
+ mime-types (1.16)
48
+ polyglot (0.3.1)
49
+ rack (1.2.1)
50
+ rack-mount (0.6.13)
51
+ rack (>= 1.0.0)
52
+ rack-test (0.5.7)
53
+ rack (>= 1.0)
54
+ rails (3.0.3)
55
+ actionmailer (= 3.0.3)
56
+ actionpack (= 3.0.3)
57
+ activerecord (= 3.0.3)
58
+ activeresource (= 3.0.3)
59
+ activesupport (= 3.0.3)
60
+ bundler (~> 1.0)
61
+ railties (= 3.0.3)
62
+ railties (3.0.3)
63
+ actionpack (= 3.0.3)
64
+ activesupport (= 3.0.3)
65
+ rake (>= 0.8.7)
66
+ thor (~> 0.14.4)
67
+ rake (0.8.7)
68
+ rcov (0.9.9)
69
+ rspec (2.4.0)
70
+ rspec-core (~> 2.4.0)
71
+ rspec-expectations (~> 2.4.0)
72
+ rspec-mocks (~> 2.4.0)
73
+ rspec-core (2.4.0)
74
+ rspec-expectations (2.4.0)
75
+ diff-lcs (~> 1.1.2)
76
+ rspec-mocks (2.4.0)
77
+ thor (0.14.6)
78
+ treetop (1.4.9)
79
+ polyglot (>= 0.3.1)
80
+ tzinfo (0.3.23)
81
+
82
+ PLATFORMS
83
+ ruby
84
+
85
+ DEPENDENCIES
86
+ bundler (~> 1.0.0)
87
+ jeweler (~> 1.5.2)
88
+ rails (>= 2.3.5)
89
+ rcov
90
+ rspec (>= 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,107 @@
1
+ h1. LocalizedLanguageSelect
2
+
3
+ Rails plugin to provide support for localized <tt><select></tt> menu with language names and for
4
+ storing language information as language _code_ (eg. 'sv'), not _name_ (eg. 'Swedish'), in the database.
5
+
6
+ Uses the Rails internationalization framework ("I18n":http://rails-i18n.org) for translating the names of languages.
7
+ Requires Rails 2.2 (released November 21st, 2008) or later versions.
8
+
9
+ Language names are loaded from hashes in plugin directory, according to <tt>I18n.locale</tt> value.
10
+
11
+ You can easily translate language codes in your application like this:
12
+
13
+ <pre>
14
+ <%= I18n.t @user.language, :scope => 'languages' %>
15
+ </pre>
16
+
17
+ Comes with a Rake task <tt>rake import:language_select locale=de</tt> for importing language names
18
+ from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
19
+ Don't forget to restart the application when you add new locale.
20
+
21
+ The code borrows heavily from the LocalizedCountrySelect plugin.
22
+ See "localized_country_select":http://github.com/karmi/localized_country_select
23
+
24
+ h2. Install & Usage
25
+
26
+ Install as plugin:
27
+
28
+ <pre>$ rails plugin install https://github.com/kristianmandrup/localized_language_select.git</pre>
29
+
30
+ Install as gem:
31
+
32
+ Insert in Gemfile
33
+
34
+ <pre>gem 'localized_language_select'</pre>
35
+
36
+ Then to instal it, run:
37
+
38
+ <pre>$ bundle install</pre>
39
+
40
+ Then to install the locale files, run:
41
+
42
+ <pre>$ rails g localized_language_select:locales</pre>
43
+
44
+ This will copy the locale files to your Rails config/locales dir. The files will be called 'language.en.yml' and so on...
45
+
46
+ Note: you can specify which locales using the @--locales@ option, which takes a list of locales.
47
+
48
+ <pre>$ rails g localized_language_select:locales --locales da en fr</pre>
49
+
50
+ h2. Usage example
51
+
52
+ <pre>
53
+ <%= localized_language_select(:user, :language, [], :include_blank => 'Please choose...') %>
54
+ </pre>
55
+
56
+
57
+ will become:
58
+
59
+ <pre>
60
+ <select name="user[language]" id="user_language">
61
+ <option value="">Please choose...</option>
62
+ <option disabled="disabled" value="">-------------</option>
63
+ <option value="aa">Afar</option>
64
+ ...
65
+ <option value="zza">ZaZa</option>
66
+ </select>
67
+ </pre>
68
+
69
+ for the <tt>en</tt> locale.
70
+
71
+ h2. Options
72
+
73
+ @:exclude@ option
74
+
75
+ <pre>
76
+ <%= localized_language_select(:user, :language, ['English'], :include_blank => 'Please choose...', :exclude => ['Afrikaans']) %>
77
+ </pre>
78
+
79
+ @:only@ option
80
+
81
+ <pre>
82
+ <%= localized_language_select(:user, :language, ['English'], :include_blank => 'Please choose...', :only => ['German', 'French']) %>
83
+ </pre>
84
+
85
+ h2. Todo - Request
86
+
87
+ Please add translations for your language!
88
+
89
+ Would be nice to have at least: German, Spanish, Chinese, Japanese, Russian, Indian and Arabic (Persian?) in there as well...
90
+
91
+ h2. Framework usage
92
+
93
+ This plugin should also integrate nicely with various formbuilders, including Formtastic
94
+
95
+ Formtastic example:
96
+
97
+ <pre>
98
+ <%= semantic_form_for @user do |form| %>
99
+ ...
100
+ <%= form.input :lang, :as => :language, :priority_languages => ['English'], :include_blank => 'Please choose...', :only => ['German', 'French']) %>
101
+ ...
102
+ <% end %>
103
+ </pre>
104
+
105
+ h2. Copyright
106
+
107
+ Copyright (c) 2009 David Cato, released under the MIT license
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "localized_language_select"
16
+ gem.homepage = "http://github.com/kristianmandrup/localized_language_select"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Localized language select for Rails 2.3+}
19
+ gem.description = %Q{Localized language select for Rails 2.3+ with options to control languages to display}
20
+ gem.email = "kmandrup@gmail.com"
21
+ gem.authors = ["Kristian Mandrup"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+
25
+ gem.add_runtime_dependency 'rails', '>= 2.3.5'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "localized_language_select #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # Require the plugin code
2
+ require 'localized_language_select'
3
+
4
+ # Load locales for languages from +locale+ directory into Rails
5
+ I18n.load_path += Dir[ File.join(File.dirname(__FILE__), 'locale', '*.{rb,yml}') ]
@@ -0,0 +1,4 @@
1
+ # Show the README text file
2
+ puts "\n\n"
3
+ puts IO.read(File.join(File.dirname(__FILE__), 'README.textile'))
4
+ puts "\n"
@@ -0,0 +1,38 @@
1
+ # require 'rails/generators/base'
2
+ # require 'sugar-high/array'
3
+ # require 'active_support/inflector'
4
+ # require 'rails3_artifactor'
5
+ # require 'logging_assist'
6
+
7
+ module LocalizedLanguageSelect
8
+ module Generators
9
+ class LocaleGenerator < Rails::Generators::Base
10
+ desc "Copy locale files"
11
+
12
+ class_option :locales, :type => :array, :default => ['all'], :desc => 'locales to generate for'
13
+
14
+ source_root File.dirname(__FILE__)
15
+
16
+ def main_flow
17
+ copy_locales
18
+ end
19
+
20
+ protected
21
+
22
+ def supported_locales
23
+ [:en, :da]
24
+ end
25
+
26
+ def locales
27
+ return supported_locales if options[:locales].include? 'all'
28
+ options[:locales].map(&:to_sym) & supported_locales
29
+ end
30
+
31
+ def copy_locales
32
+ locales.each do |locale|
33
+ copy_file "../../../../locale/#{locale}.yml", "config/locales/languages.#{locale}.yml"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,117 @@
1
+ # = LocalizedLanguageSelect
2
+ #
3
+ # View helper for displaying select list with languages:
4
+ #
5
+ # localized_language_select(:user, :language)
6
+ #
7
+ # Works just like the default Rails' +country_select+ plugin, but stores languages as
8
+ # language *codes*, not *names*, in the database.
9
+ #
10
+ # You can easily translate language codes in your application like this:
11
+ # <%= I18n.t @user.language, :scope => 'languages' %>
12
+ #
13
+ # Uses the Rails internationalization framework (I18n) for translating the names of languages.
14
+ #
15
+ # Use Rake task <tt>rake import:language_select 'de'</tt> for importing language names
16
+ # from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
17
+ #
18
+ # The code borrows heavily from the LocalizedCountrySelect plugin.
19
+ # See http://github.com/karmi/localized_country_select
20
+ #
21
+
22
+ require 'localized_language_select/i18n'
23
+
24
+ module LocalizedLanguageSelect
25
+ class << self
26
+ # Returns array with codes and localized language names (according to <tt>I18n.locale</tt>)
27
+ # for <tt><option></tt> tags
28
+ def localized_languages_array options = {}
29
+ res = []
30
+ list = I18n.translate(:languages).each do |key, value|
31
+ res << [value, key.to_s.upcase] if include_language?(key.to_s, options)
32
+ end
33
+ res.sort_by { |country| country.first.parameterize }
34
+ end
35
+
36
+ def include_language?(key, options)
37
+ if options[:only]
38
+ return options[:only].include?(key)
39
+ end
40
+ if options[:except]
41
+ return !options[:except].include?(key)
42
+ end
43
+ true
44
+ end
45
+
46
+
47
+ # Return array with codes and localized language names for array of language codes passed as argument
48
+ # == Example
49
+ # priority_languages_array([:de, :fr, :en])
50
+ # # => [ ['German', 'de'], ['French', 'fr'], ['English', 'en'] ]
51
+ def priority_languages_array(language_codes=[])
52
+ languages = I18n.translate(:languages)
53
+ language_codes.map { |code| [languages[code], code.to_s.downcase] }
54
+ end
55
+ end
56
+ end
57
+
58
+ module ActionView
59
+ module Helpers
60
+
61
+ module FormOptionsHelper
62
+
63
+ # Return select and option tags for the given object and method, using +localized_language_options_for_select+
64
+ # to generate the list of option tags. Uses <b>language code</b>, not name as option +value+.
65
+ # Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
66
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
67
+ def localized_language_select(object, method, priority_languages = nil, options = {}, html_options = {})
68
+ InstanceTag.new(object, method, self, options.delete(:object)).
69
+ to_localized_language_select_tag(priority_languages, options, html_options)
70
+ end
71
+
72
+ # Return "named" select and option tags according to given arguments.
73
+ # Use +selected_value+ for setting initial value
74
+ # It behaves likes older object-binded brother +localized_language_select+ otherwise
75
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
76
+ def localized_language_select_tag(name, selected_value = nil, priority_languages = nil, html_options = {})
77
+ content_tag :select,
78
+ localized_language_options_for_select(selected_value, priority_languages),
79
+ { "name" => name, "id" => name }.update(html_options.stringify_keys)
80
+ end
81
+
82
+ # Returns a string of option tags for languages according to locale. Supply the language code in lower-case ('en', 'de')
83
+ # as +selected+ to have it marked as the selected option tag.
84
+ # Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
85
+ def localized_language_options_for_select(selected = nil, priority_languages = nil, options = {})
86
+ language_options = ""
87
+ if priority_languages
88
+ language_options += options_for_select(LocalizedLanguageSelect::priority_languages_array(priority_languages), selected)
89
+ language_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
90
+ end
91
+ return language_options + options_for_select(LocalizedLanguageSelect::localized_languages_array(options), selected)
92
+ end
93
+
94
+ end
95
+
96
+ class InstanceTag
97
+ def to_localized_language_select_tag(priority_languages, options, html_options)
98
+ html_options = html_options.stringify_keys
99
+ add_default_name_and_id(html_options)
100
+ value = value(object)
101
+ content_tag("select",
102
+ add_options(
103
+ localized_language_options_for_select(value, priority_languages, options),
104
+ options, value
105
+ ), html_options
106
+ )
107
+ end
108
+ end
109
+
110
+ class FormBuilder
111
+ def localized_language_select(method, priority_languages = nil, options = {}, html_options = {})
112
+ @template.localized_language_select(@object_name, method, priority_languages, options.merge(:object => @object), html_options)
113
+ end
114
+ end
115
+
116
+ end
117
+ end