localized_language_select 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{localized_language_select}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = %q{2011-01-14}
13
+ s.description = %q{Localized language select for Rails 2.3+ with options to control languages to display}
14
+ s.email = %q{kmandrup@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "MIT-LICENSE",
22
+ "README.textile",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "init.rb",
26
+ "install.rb",
27
+ "lib/generators/localized_language_select/locales/locales_generator.rb",
28
+ "lib/localized_language_select.rb",
29
+ "lib/localized_language_select/i18n.rb",
30
+ "locale/da.yml",
31
+ "locale/de.yml",
32
+ "locale/en.yml",
33
+ "locale/fr.yml",
34
+ "locale/nl.yml",
35
+ "localized_language_select.gemspec",
36
+ "tasks/localized_language_select_tasks.rake",
37
+ "test/localized_language_select_test.rb",
38
+ "uninstall.rb"
39
+ ]
40
+ s.homepage = %q{http://github.com/kristianmandrup/localized_language_select}
41
+ s.licenses = ["MIT"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.7}
44
+ s.summary = %q{Localized language select for Rails 2.3+}
45
+ s.test_files = [
46
+ "test/localized_language_select_test.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<rails>, [">= 2.3.5"])
55
+ s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
56
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_development_dependency(%q<rcov>, [">= 0"])
59
+ s.add_runtime_dependency(%q<rails>, [">= 2.3.5"])
60
+ else
61
+ s.add_dependency(%q<rails>, [">= 2.3.5"])
62
+ s.add_dependency(%q<rspec>, [">= 2.3.0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ s.add_dependency(%q<rails>, [">= 2.3.5"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<rails>, [">= 2.3.5"])
70
+ s.add_dependency(%q<rspec>, [">= 2.3.0"])
71
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
72
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
73
+ s.add_dependency(%q<rcov>, [">= 0"])
74
+ s.add_dependency(%q<rails>, [">= 2.3.5"])
75
+ end
76
+ end
77
+
@@ -0,0 +1,87 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+ require 'open-uri'
4
+
5
+ # Rake task for importing language names from Unicode.org's CLDR repository
6
+ # (http://www.unicode.org/cldr/data/charts/summary/root.html).
7
+ #
8
+ # It parses a HTML file from Unicode.org for given locale and saves the
9
+ # Rails' I18n hash in the plugin +locale+ directory
10
+ #
11
+ # Don't forget to restart the application when you add new locale to load it into Rails!
12
+ #
13
+ # == Example
14
+ # rake import:language_select locale=de
15
+ #
16
+ # The code is deliberately procedural and simple, so it's easily
17
+ # understandable by beginners as an introduction to Rake tasks power.
18
+ # See http://github.com/joshmh/cldr/tree/master/converter.rb for much more robust solution
19
+
20
+ namespace :import do
21
+
22
+ desc "Import language codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
23
+ task :language_select do
24
+
25
+ # TODO : Implement locale import chooser from CLDR root via Highline
26
+
27
+ # Setup variables
28
+ locale = ENV['locale']
29
+ unless locale
30
+ puts "\n[!] Usage: rake import:language_select locale=de\n\n"
31
+ exit 0
32
+ end
33
+
34
+ # ----- Get the CLDR HTML --------------------------------------------------
35
+ begin
36
+ puts "... getting the HTML file for locale '#{locale}'"
37
+ doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{locale}.html") )
38
+ rescue => e
39
+ puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
40
+ exit 0
41
+ end
42
+
43
+ require 'ruby-debug'
44
+
45
+ # ----- Parse the HTML with Hpricot ----------------------------------------
46
+ puts "... parsing the HTML file"
47
+ languages = []
48
+ doc.search("//tr").each do |row|
49
+ if row.search("td[@class='n']") &&
50
+ row.search("td[@class='n']").inner_html =~ /^nameslanguage$/ &&
51
+ row.search("td[@class='g']").inner_html =~ /^[a-z]{2,3}(?:_([A-Z][a-z]{3}))?(?:_([A-Z]{2}))?$/
52
+ code = row.search("td[@class='g']").inner_text
53
+ code.sub!('_','-')
54
+ #debugger if code =~ /-/
55
+ name = row.search("td[@class='v']").first.inner_text
56
+ languages << { :code => code.to_sym, :name => name.to_s }
57
+ print " ... #{name}"
58
+ end
59
+ end
60
+
61
+
62
+ # ----- Prepare the output format ------------------------------------------
63
+ output =<<HEAD
64
+ {
65
+ :#{locale} => {
66
+ :languages => {
67
+ HEAD
68
+ languages.each do |language|
69
+ output << " #{language[:code].inspect} => \"#{language[:name]}\",\n"
70
+ end
71
+ output <<<<TAIL
72
+ }
73
+ }
74
+ }
75
+ TAIL
76
+
77
+
78
+ # ----- Write the parsed values into file ---------------------------------
79
+ puts "\n... writing the output"
80
+ filename = File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb")
81
+ filename += '.NEW' if File.exists?(filename) # Append 'NEW' if file exists
82
+ File.open(filename, 'w+') { |f| f << output }
83
+ puts "\n---\nWritten values for the '#{locale}' into file: #{filename}\n"
84
+ # ------------------------------------------------------------------------------
85
+ end
86
+
87
+ end
@@ -0,0 +1,113 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test/unit'
3
+
4
+ require 'rubygems'
5
+ require 'active_support'
6
+ require 'action_controller'
7
+ # require 'action_controller/test_process'
8
+ require 'action_view'
9
+ require 'action_view/helpers'
10
+ require 'action_view/helpers/tag_helper'
11
+ require 'i18n'
12
+
13
+ require 'localized_language_select'
14
+
15
+ class LocalizedLanguageSelectTest < Test::Unit::TestCase
16
+
17
+ include ActionView::Helpers::FormOptionsHelper
18
+ include ActionView::Helpers::TagHelper
19
+
20
+ def test_action_view_should_include_helper_for_object
21
+ assert ActionView::Helpers::FormBuilder.instance_methods.include?('localized_language_select')
22
+ assert ActionView::Helpers::FormOptionsHelper.instance_methods.include?('localized_language_select')
23
+ end
24
+
25
+ def test_action_view_should_include_helper_tag
26
+ assert ActionView::Helpers::FormOptionsHelper.instance_methods.include?('localized_language_select_tag')
27
+ end
28
+
29
+ def test_should_return_select_tag_with_proper_name_for_object
30
+ assert localized_language_select(:user, :language) =~
31
+ Regexp.new(Regexp.escape('<select id="user_language" name="user[language]">')),
32
+ "Should have proper name for object"
33
+ end
34
+
35
+ def test_should_return_select_tag_with_proper_name
36
+ assert localized_language_select_tag( "competition_submission[data][language]", nil) =~
37
+ Regexp.new(
38
+ Regexp.escape('<select id="competition_submission[data][language]" name="competition_submission[data][language]">') ),
39
+ "Should have proper name"
40
+ end
41
+
42
+ def test_should_return_option_tags
43
+ assert localized_language_select(:user, :language) =~ Regexp.new(Regexp.escape('<option value="es">Spanish</option>'))
44
+ end
45
+
46
+ def test_should_return_localized_option_tags
47
+ I18n.locale = 'fr'
48
+ assert localized_language_select(:user, :language) =~ Regexp.new(Regexp.escape('<option value="es">espagnol</option>'))
49
+ end
50
+
51
+ def test_should_return_localized_country_option_tags
52
+ I18n.locale = 'fr'
53
+ assert localized_language_select(:user, :language) =~ Regexp.new(Regexp.escape('<option value="es-ES">espagnol ibérique</option>'))
54
+ end
55
+
56
+ def test_should_return_priority_languages_first
57
+ assert localized_language_options_for_select(nil, [:es, :fr]) =~ Regexp.new(
58
+ Regexp.escape("<option value=\"es\">Spanish</option>\n<option value=\"fr\">French</option><option value=\"\" disabled=\"disabled\">-------------</option>\n<option value=\"ab\">Abkhazian</option>\n"))
59
+ end
60
+
61
+ def test_i18n_should_know_about_languages
62
+ assert_equal 'Spanish', I18n.t('es', :scope => 'languages')
63
+ I18n.locale = 'fr'
64
+ assert_equal 'espagnol', I18n.t('es', :scope => 'languages')
65
+ end
66
+
67
+ def test_localized_languages_array_returns_correctly
68
+ assert_nothing_raised { LocalizedLanguageSelect::localized_languages_array() }
69
+ I18n.locale = 'en'
70
+ assert_equal 504, LocalizedLanguageSelect::localized_languages_array.size
71
+ assert_equal 'Abkhazian', LocalizedLanguageSelect::localized_languages_array.first[0]
72
+ I18n.locale = 'fr'
73
+ assert_equal 503, LocalizedLanguageSelect::localized_languages_array.size
74
+ assert_equal 'abkhaze', LocalizedLanguageSelect::localized_languages_array.first[0]
75
+ end
76
+
77
+ def test_priority_languages_returns_correctly_and_in_correct_order
78
+ assert_nothing_raised { LocalizedLanguageSelect::priority_languages_array([:nl, :fr]) }
79
+ I18n.locale = 'en'
80
+ assert_equal [ ['Dutch', 'nl'], ['French', 'fr'] ], LocalizedLanguageSelect::priority_languages_array([:nl, :fr])
81
+ end
82
+
83
+ def test_include_country_rejects_on_except_option
84
+ res = !LocalizedLanguageSelect::include_language?('af', except: ['af'])
85
+ puts "should not include_language? '' except af : #{res}"
86
+ end
87
+
88
+ def test_include_country_accept_on_only_option
89
+ res = LocalizedLanguageSelect::include_language?('af', only: ['af'])
90
+ puts "should include_language? 'af' : #{res}"
91
+
92
+ res = !LocalizedLanguageSelect::include_language?('af', only: ['ad'])
93
+ puts "should not include_language? 'af' : #{res}"
94
+ end
95
+
96
+ def test_localized_countries_array_rejects_on_except_option
97
+ list = LocalizedLanguageSelect::localized_languages_array(:except => ['af'])
98
+ res = !list.include?(['Afrikaans', 'af'])
99
+ puts "rejected 'af' from array? #{res}"
100
+ end
101
+
102
+
103
+ # private
104
+
105
+ def setup
106
+ ['fr', 'en'].each do |locale|
107
+ # I18n.load_translations( File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb") ) # <-- Old style! :)
108
+ I18n.load_path += Dir[ File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb") ]
109
+ end
110
+ I18n.locale = 'en'
111
+ end
112
+
113
+ end
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localized_language_select
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Kristian Mandrup
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-14 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 5
31
+ version: 2.3.5
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 3
45
+ - 0
46
+ version: 2.3.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: bundler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 0
60
+ - 0
61
+ version: 1.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: jeweler
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
74
+ - 5
75
+ - 2
76
+ version: 1.5.2
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: rcov
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ name: rails
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 2
102
+ - 3
103
+ - 5
104
+ version: 2.3.5
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: *id006
108
+ description: Localized language select for Rails 2.3+ with options to control languages to display
109
+ email: kmandrup@gmail.com
110
+ executables: []
111
+
112
+ extensions: []
113
+
114
+ extra_rdoc_files:
115
+ - README.textile
116
+ files:
117
+ - Gemfile
118
+ - Gemfile.lock
119
+ - MIT-LICENSE
120
+ - README.textile
121
+ - Rakefile
122
+ - VERSION
123
+ - init.rb
124
+ - install.rb
125
+ - lib/generators/localized_language_select/locales/locales_generator.rb
126
+ - lib/localized_language_select.rb
127
+ - lib/localized_language_select/i18n.rb
128
+ - locale/da.yml
129
+ - locale/de.yml
130
+ - locale/en.yml
131
+ - locale/fr.yml
132
+ - locale/nl.yml
133
+ - localized_language_select.gemspec
134
+ - tasks/localized_language_select_tasks.rake
135
+ - test/localized_language_select_test.rb
136
+ - uninstall.rb
137
+ has_rdoc: true
138
+ homepage: http://github.com/kristianmandrup/localized_language_select
139
+ licenses:
140
+ - MIT
141
+ post_install_message:
142
+ rdoc_options: []
143
+
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ hash: -833898733757377216
152
+ segments:
153
+ - 0
154
+ version: "0"
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ segments:
161
+ - 0
162
+ version: "0"
163
+ requirements: []
164
+
165
+ rubyforge_project:
166
+ rubygems_version: 1.3.7
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: Localized language select for Rails 2.3+
170
+ test_files:
171
+ - test/localized_language_select_test.rb