magic-localized_country_select 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -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.
data/README.rdoc ADDED
@@ -0,0 +1,59 @@
1
+ = LocalizedCountrySelect
2
+
3
+ Rails plugin to provide support for localized <tt><select></tt> menu with country names and for
4
+ storing country information as country _code_ (eg. 'es'), not _name_ (eg. 'Spain'), in the database.
5
+
6
+ Uses the Rails internationalization framework (I18n, http://rails-i18n.org) for translating the names of countries.
7
+ Requires Rails 2.2 (released November 21st, 2008) or later versions.
8
+ Country names are loaded from hashes in plugin directory, according to <tt>I18n.locale</tt> value.
9
+
10
+ You can easily translate country codes in your application like this:
11
+
12
+ <%= I18n.t @user.country, :scope => 'countries' %>
13
+
14
+ Comes with a Rake task <tt>rake import:country_select 'de'</tt> for importing country names
15
+ from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
16
+ Don't forget to restart the application when you add new locale.
17
+
18
+ ActionView helper code is adapted from Rails' default +country_select+ plugin (previously in core).
19
+ See http://github.com/rails/country_select/tree/master/lib/country_select.rb
20
+
21
+ == Examples
22
+
23
+ Show the complete country name:
24
+
25
+ <%= country_select(:user, :country, {}, :include_blank => 'Please choose...') %>
26
+
27
+ will become:
28
+
29
+ <select name="user[country]" id="user_country">
30
+ <option value="">Please choose...</option>
31
+ <option disabled="disabled" value="">-------------</option>
32
+ <option value="AF">Afghanistan</option>
33
+ ...
34
+ <option value="ZW">Zimbabwe</option>
35
+ </select>
36
+
37
+
38
+ Show only the country codes:
39
+
40
+ <%= country_select(:user, :country, {:description => :abbreviated}, :include_blank => 'Please choose...') %>
41
+
42
+ will become:
43
+
44
+ <select name="user[country]" id="user_country">
45
+ <option value="">Please choose...</option>
46
+ <option disabled="disabled" value="">-------------</option>
47
+ <option value="AF">AF</option>
48
+ ...
49
+ <option value="ZW">ZW</option>
50
+ </select>
51
+ for the <tt>en</tt> locale.
52
+
53
+ == Other resources
54
+
55
+ * http://github.com/rails/country_select (Default Rails plugin)
56
+ * http://github.com/russ/country_code_select (Stores country code, not name)
57
+
58
+
59
+ Copyright (c) 2008 Karel Minarik (www.karmi.cz), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+
7
+ load File.join(File.dirname(__FILE__), 'lib', 'tasks', 'localized_country_select_tasks.rake')
8
+
9
+ desc 'Default: run unit tests.'
10
+ task :default => :test
11
+
12
+ desc 'Test the localized_country_select plugin.'
13
+ Rake::TestTask.new(:test) do |t|
14
+ t.libs << 'lib'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+
19
+ desc 'Generate documentation for the localized_country_select plugin.'
20
+ Rake::RDocTask.new(:rdoc) do |rdoc|
21
+ rdoc.rdoc_dir = 'rdoc'
22
+ rdoc.title = 'LocalizedCountrySelect'
23
+ rdoc.options << '--line-numbers' << '--inline-source'
24
+ rdoc.rdoc_files.include('README.rdoc')
25
+ rdoc.rdoc_files.include('lib/**/*.rb')
26
+ end
27
+
28
+ #
29
+ # Jeweler
30
+ #
31
+ begin
32
+ require 'jeweler'
33
+ Jeweler::Tasks.new do |gemspec|
34
+ gemspec.name = "magic-localized_country_select"
35
+ gemspec.summary = "Localized country select list"
36
+ gemspec.description = "Localized country select list"
37
+ gemspec.email = "mail@magiclabs.de"
38
+ gemspec.homepage = "https://github.com/magiclabs/localized_country_select"
39
+ gemspec.authors = ["LIM SAS", "Damien MATHIEU", "Julien SANCHEZ", "Hervé GAUCHER"]
40
+ end
41
+ rescue LoadError
42
+ puts "Jeweler not available. Install it with: gem install jeweler"
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # Require the plugin code
2
+ require 'localized_country_select'
3
+
4
+ # Load locales for countries from +locale+ directory into Rails
5
+ I18n.load_path += Dir[ File.join(File.dirname(__FILE__), 'locale', '*.{rb,yml}') ]
data/install.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Show the README text file
2
+ puts "\n\n"
3
+ puts IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
4
+ puts "\n"
@@ -0,0 +1,109 @@
1
+ require 'rake'
2
+ load 'tasks/localized_country_select_tasks.rake'
3
+
4
+ # = LocalizedCountrySelect
5
+ #
6
+ # View helper for displaying select list with countries:
7
+ #
8
+ # country_select(:user, :country)
9
+ #
10
+ # Works just like the default Rails' +country_select+ plugin, but stores countries as
11
+ # country *codes*, not *names*, in the database.
12
+ #
13
+ # You can easily translate country codes in your application like this:
14
+ # <%= I18n.t @user.country, :scope => 'countries' %>
15
+ #
16
+ # Uses the Rails internationalization framework (I18n) for translating the names of countries.
17
+ #
18
+ # Use Rake task <tt>rake import:country_select 'de'</tt> for importing country names
19
+ # from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
20
+ #
21
+ # Code adapted from Rails' default +country_select+ plugin (previously in core)
22
+ # See http://github.com/rails/country_select/tree/master/lib/country_select.rb
23
+ #
24
+ module LocalizedCountrySelect
25
+ class << self
26
+ # Returns array with codes and localized country names (according to <tt>I18n.locale</tt>)
27
+ # for <tt><option></tt> tags
28
+ def localized_countries_array(options={})
29
+ if(options[:description]==:abbreviated)
30
+ I18n.translate(:countries).map { |key, value| [key.to_s.upcase] }.
31
+ sort_by { |country| country.first }
32
+ else
33
+ I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] }.
34
+ sort_by { |country| country.first }
35
+ end
36
+ end
37
+ # Return array with codes and localized country names for array of country codes passed as argument
38
+ # == Example
39
+ # priority_countries_array([:TW, :CN])
40
+ # # => [ ['Taiwan', 'TW'], ['China', 'CN'] ]
41
+ def priority_countries_array(country_codes=[],options={})
42
+ if(options[:description]==:abbreviated)
43
+ country_codes.map { |code| [code.to_s.upcase] }
44
+ else
45
+ countries = I18n.translate(:countries)
46
+ country_codes.map { |code| [countries[code.to_s.upcase.to_sym], code.to_s.upcase] }
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ module ActionView
53
+ module Helpers
54
+
55
+ module FormOptionsHelper
56
+
57
+ # Return select and option tags for the given object and method, using +localized_country_options_for_select+
58
+ # to generate the list of option tags. Uses <b>country code</b>, not name as option +value+.
59
+ # Country codes listed as an array of symbols in +priority_countries+ argument will be listed first
60
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
61
+ def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
62
+ InstanceTag.new(object, method, self, options.delete(:object)).
63
+ to_localized_country_select_tag(priority_countries, options, html_options)
64
+ end
65
+
66
+ # Return "named" select and option tags according to given arguments.
67
+ # Use +selected_value+ for setting initial value
68
+ # It behaves likes older object-binded brother +localized_country_select+ otherwise
69
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
70
+ def country_select_tag(name, selected_value = nil, priority_countries = nil, html_options = {})
71
+ select_tag name.to_sym, country_options_for_select(selected_value, priority_countries), html_options.stringify_keys
72
+ end
73
+
74
+ # Returns a string of option tags for countries according to locale. Supply the country code in upper-case ('US', 'DE')
75
+ # as +selected+ to have it marked as the selected option tag.
76
+ # Country codes listed as an array of symbols in +priority_countries+ argument will be listed first
77
+ def country_options_for_select(selected = nil, priority_countries = nil,options={})
78
+ country_options = ""
79
+ if priority_countries
80
+ country_options += options_for_select(LocalizedCountrySelect::priority_countries_array(priority_countries,options), selected)
81
+ country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
82
+ end
83
+ return country_options + options_for_select(LocalizedCountrySelect::localized_countries_array(options), selected)
84
+ end
85
+
86
+ end
87
+
88
+ class InstanceTag
89
+ def to_localized_country_select_tag(priority_countries, options, html_options)
90
+ html_options = html_options.stringify_keys
91
+ add_default_name_and_id(html_options)
92
+ value = value(object)
93
+ content_tag("select",
94
+ add_options(
95
+ country_options_for_select(value, priority_countries,options),
96
+ options, value
97
+ ), html_options
98
+ )
99
+ end
100
+ end
101
+
102
+ class FormBuilder
103
+ def country_select(method, priority_countries = nil, options = {}, html_options = {})
104
+ @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
105
+ end
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,92 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+
4
+ # Rake task for importing country names from Unicode.org's CLDR repository
5
+ # (http://www.unicode.org/cldr/data/charts/summary/root.html).
6
+ #
7
+ # It parses a HTML file from Unicode.org for given locale and saves the
8
+ # Rails' I18n hash in the plugin +locale+ directory
9
+ #
10
+ # Don't forget to restart the application when you add new locale to load it into Rails!
11
+ #
12
+ # == Example
13
+ # rake import:country_select LOCALE=de
14
+ #
15
+ # The code is deliberately procedural and simple, so it's easily
16
+ # understandable by beginners as an introduction to Rake tasks power.
17
+ # See http://github.com/joshmh/cldr/tree/master/converter.rb for much more robust solution
18
+
19
+ namespace :import do
20
+
21
+ desc "Import country codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
22
+ task :country_select do
23
+ begin
24
+ require 'hpricot'
25
+ rescue LoadError
26
+ puts "Error: Hpricot library required to use this task (import:country_select)"
27
+ exit
28
+ end
29
+
30
+ # TODO : Implement locale import chooser from CLDR root via Highline
31
+
32
+ # Setup variables
33
+ locale = ENV['LOCALE']
34
+ unless locale
35
+ puts "\n[!] Usage: rake import:country_select LOCALE=de\n\n"
36
+ exit 0
37
+ end
38
+
39
+ # ----- Get the CLDR HTML --------------------------------------------------
40
+ begin
41
+ puts "... getting the HTML file for locale '#{locale}'"
42
+ doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{locale}.html") )
43
+ rescue => e
44
+ puts "[!] Invalid locale name '#{locale}'! Not found in CLDR (#{e})"
45
+ exit 0
46
+ end
47
+
48
+
49
+ # ----- Parse the HTML with Hpricot ----------------------------------------
50
+ puts "... parsing the HTML file"
51
+ countries = []
52
+ doc.search("//tr").each do |row|
53
+ if row.search("td[@class='n']") &&
54
+ row.search("td[@class='n']").inner_html =~ /^namesterritory$/ &&
55
+ row.search("td[@class='g']").inner_html =~ /^[A-Z]{2}$/
56
+ code = row.search("td[@class='g']").inner_text
57
+ code = code[-code.size, 2]
58
+ name = row.search("td[@class='v']").inner_text
59
+ countries << { :code => code.to_sym, :name => name.to_s }
60
+ print " ... #{name}"
61
+ end
62
+ end
63
+
64
+
65
+ # ----- Prepare the output format ------------------------------------------
66
+ output = "#encoding: UTF-8\n"
67
+ output <<<<HEAD
68
+ { :#{locale} => {
69
+
70
+ :countries => {
71
+ HEAD
72
+ countries.each do |country|
73
+ output << "\t\t\t:#{country[:code]} => \"#{country[:name]}\",\n"
74
+ end
75
+ output <<<<TAIL
76
+ }
77
+
78
+ }
79
+ }
80
+ TAIL
81
+
82
+
83
+ # ----- Write the parsed values into file ---------------------------------
84
+ puts "\n... writing the output"
85
+ filename = File.join(Rails.root, 'config', 'locales', "localized_country_select.#{locale}.rb")
86
+ filename += '.NEW' if File.exists?(filename) # Append 'NEW' if file exists
87
+ File.open(filename, 'w+') { |f| f << output }
88
+ puts "\n---\nWritten values for the locale '#{locale}' into file: #{filename}\n"
89
+ # ------------------------------------------------------------------------------
90
+ end
91
+
92
+ end
data/locale/en.rb ADDED
@@ -0,0 +1,250 @@
1
+ { :en => {
2
+
3
+ :countries => {
4
+ :AD => "Andorra",
5
+ :AE => "United Arab Emirates",
6
+ :AF => "Afghanistan",
7
+ :AG => "Antigua and Barbuda",
8
+ :AI => "Anguilla",
9
+ :AL => "Albania",
10
+ :AM => "Armenia",
11
+ :AN => "Netherlands Antilles",
12
+ :AO => "Angola",
13
+ :AQ => "Antarctica",
14
+ :AR => "Argentina",
15
+ :AS => "American Samoa",
16
+ :AT => "Austria",
17
+ :AU => "Australia",
18
+ :AW => "Aruba",
19
+ :AX => "Aland Islands",
20
+ :AZ => "Azerbaijan",
21
+ :BA => "Bosnia and Herzegovina",
22
+ :BB => "Barbados",
23
+ :BD => "Bangladesh",
24
+ :BE => "Belgium",
25
+ :BF => "Burkina Faso",
26
+ :BG => "Bulgaria",
27
+ :BH => "Bahrain",
28
+ :BI => "Burundi",
29
+ :BJ => "Benin",
30
+ :BL => "Saint Barthélemy",
31
+ :BM => "Bermuda",
32
+ :BN => "Brunei",
33
+ :BO => "Bolivia",
34
+ :BR => "Brazil",
35
+ :BS => "Bahamas",
36
+ :BT => "Bhutan",
37
+ :BV => "Bouvet Island",
38
+ :BW => "Botswana",
39
+ :BY => "Belarus",
40
+ :BZ => "Belize",
41
+ :CA => "Canada",
42
+ :CC => "Cocos Islands",
43
+ :CD => "Congo - Kinshasa",
44
+ :CF => "Central African Republic",
45
+ :CG => "Congo - Brazzaville",
46
+ :CH => "Switzerland",
47
+ :CI => "Cote d'Ivoire",
48
+ :CK => "Cook Islands",
49
+ :CL => "Chile",
50
+ :CM => "Cameroon",
51
+ :CN => "China",
52
+ :CO => "Colombia",
53
+ :CR => "Costa Rica",
54
+ :CU => "Cuba",
55
+ :CV => "Cape Verde",
56
+ :CX => "Christmas Island",
57
+ :CY => "Cyprus",
58
+ :CZ => "Czech Republic",
59
+ :DE => "Germany",
60
+ :DJ => "Djibouti",
61
+ :DK => "Denmark",
62
+ :DM => "Dominica",
63
+ :DO => "Dominican Republic",
64
+ :DZ => "Algeria",
65
+ :EC => "Ecuador",
66
+ :EE => "Estonia",
67
+ :EG => "Egypt",
68
+ :EH => "Western Sahara",
69
+ :ER => "Eritrea",
70
+ :ES => "Spain",
71
+ :ET => "Ethiopia",
72
+ :FI => "Finland",
73
+ :FJ => "Fiji",
74
+ :FK => "Falkland Islands",
75
+ :FM => "Micronesia",
76
+ :FO => "Faroe Islands",
77
+ :FR => "France",
78
+ :GA => "Gabon",
79
+ :GB => "United Kingdom",
80
+ :GD => "Grenada",
81
+ :GE => "Georgia",
82
+ :GF => "French Guiana",
83
+ :GG => "Guernsey",
84
+ :GH => "Ghana",
85
+ :GI => "Gibraltar",
86
+ :GL => "Greenland",
87
+ :GM => "Gambia",
88
+ :GN => "Guinea",
89
+ :GP => "Guadeloupe",
90
+ :GQ => "Equatorial Guinea",
91
+ :GR => "Greece",
92
+ :GS => "South Georgia and the South Sandwich Islands",
93
+ :GT => "Guatemala",
94
+ :GU => "Guam",
95
+ :GW => "Guinea-Bissau",
96
+ :GY => "Guyana",
97
+ :HK => "Hong Kong",
98
+ :HM => "Heard Island and McDonald Islands",
99
+ :HN => "Honduras",
100
+ :HR => "Croatia",
101
+ :HT => "Haiti",
102
+ :HU => "Hungary",
103
+ :ID => "Indonesia",
104
+ :IE => "Ireland",
105
+ :IL => "Israel",
106
+ :IM => "Isle of Man",
107
+ :IN => "India",
108
+ :IQ => "Iraq",
109
+ :IR => "Iran",
110
+ :IS => "Iceland",
111
+ :IT => "Italy",
112
+ :JE => "Jersey",
113
+ :JM => "Jamaica",
114
+ :JO => "Jordan",
115
+ :JP => "Japan",
116
+ :KE => "Kenya",
117
+ :KG => "Kyrgyzstan",
118
+ :KH => "Cambodia",
119
+ :KI => "Kiribati",
120
+ :KM => "Comoros",
121
+ :KN => "Saint Kitts and Nevis",
122
+ :KP => "North Korea",
123
+ :KR => "South Korea",
124
+ :KW => "Kuwait",
125
+ :KY => "Cayman Islands",
126
+ :KZ => "Kazakhstan",
127
+ :LA => "Laos",
128
+ :LB => "Lebanon",
129
+ :LC => "Saint Lucia",
130
+ :LI => "Liechtenstein",
131
+ :LK => "Sri Lanka",
132
+ :LR => "Liberia",
133
+ :LS => "Lesotho",
134
+ :LT => "Lithuania",
135
+ :LU => "Luxembourg",
136
+ :LV => "Latvia",
137
+ :LY => "Libya",
138
+ :MA => "Morocco",
139
+ :MC => "Monaco",
140
+ :MD => "Moldova",
141
+ :ME => "Montenegro",
142
+ :MF => "Saint Martin",
143
+ :MG => "Madagascar",
144
+ :MH => "Marshall Islands",
145
+ :MK => "Macedonia",
146
+ :ML => "Mali",
147
+ :MM => "Myanmar",
148
+ :MN => "Mongolia",
149
+ :MO => "Macao",
150
+ :MP => "Northern Mariana Islands",
151
+ :MQ => "Martinique",
152
+ :MR => "Mauritania",
153
+ :MS => "Montserrat",
154
+ :MT => "Malta",
155
+ :MU => "Mauritius",
156
+ :MV => "Maldives",
157
+ :MW => "Malawi",
158
+ :MX => "Mexico",
159
+ :MY => "Malaysia",
160
+ :MZ => "Mozambique",
161
+ :NA => "Namibia",
162
+ :NC => "New Caledonia",
163
+ :NE => "Niger",
164
+ :NF => "Norfolk Island",
165
+ :NG => "Nigeria",
166
+ :NI => "Nicaragua",
167
+ :NL => "Netherlands",
168
+ :NO => "Norway",
169
+ :NP => "Nepal",
170
+ :NR => "Nauru",
171
+ :NU => "Niue",
172
+ :NZ => "New Zealand",
173
+ :OM => "Oman",
174
+ :PA => "Panama",
175
+ :PE => "Peru",
176
+ :PF => "French Polynesia",
177
+ :PG => "Papua New Guinea",
178
+ :PH => "Philippines",
179
+ :PK => "Pakistan",
180
+ :PL => "Poland",
181
+ :PM => "Saint Pierre and Miquelon",
182
+ :PN => "Pitcairn",
183
+ :PR => "Puerto Rico",
184
+ :PS => "Palestinian Territory",
185
+ :PT => "Portugal",
186
+ :PW => "Palau",
187
+ :PY => "Paraguay",
188
+ :QA => "Qatar",
189
+ :RE => "Reunion",
190
+ :RO => "Romania",
191
+ :RS => "Serbia",
192
+ :RU => "Russia",
193
+ :RW => "Rwanda",
194
+ :SA => "Saudi Arabia",
195
+ :SB => "Solomon Islands",
196
+ :SC => "Seychelles",
197
+ :SD => "Sudan",
198
+ :SE => "Sweden",
199
+ :SG => "Singapore",
200
+ :SH => "Saint Helena",
201
+ :SI => "Slovenia",
202
+ :SJ => "Svalbard and Jan Mayen",
203
+ :SK => "Slovakia",
204
+ :SL => "Sierra Leone",
205
+ :SM => "San Marino",
206
+ :SN => "Senegal",
207
+ :SO => "Somalia",
208
+ :SR => "Suriname",
209
+ :ST => "Sao Tome and Principe",
210
+ :SV => "El Salvador",
211
+ :SY => "Syria",
212
+ :SZ => "Swaziland",
213
+ :TC => "Turks and Caicos Islands",
214
+ :TD => "Chad",
215
+ :TF => "French Southern Territories",
216
+ :TG => "Togo",
217
+ :TH => "Thailand",
218
+ :TJ => "Tajikistan",
219
+ :TK => "Tokelau",
220
+ :TL => "East Timor",
221
+ :TM => "Turkmenistan",
222
+ :TN => "Tunisia",
223
+ :TO => "Tonga",
224
+ :TR => "Turkey",
225
+ :TT => "Trinidad and Tobago",
226
+ :TV => "Tuvalu",
227
+ :TW => "Taiwan",
228
+ :TZ => "Tanzania",
229
+ :UA => "Ukraine",
230
+ :UG => "Uganda",
231
+ :US => "United States",
232
+ :UY => "Uruguay",
233
+ :UZ => "Uzbekistan",
234
+ :VA => "Vatican",
235
+ :VC => "Saint Vincent and the Grenadines",
236
+ :VE => "Venezuela",
237
+ :VG => "British Virgin Islands",
238
+ :VI => "U.S. Virgin Islands",
239
+ :VN => "Vietnam",
240
+ :VU => "Vanuatu",
241
+ :WS => "Samoa",
242
+ :YE => "Yemen",
243
+ :YT => "Mayotte",
244
+ :ZA => "South Africa",
245
+ :ZM => "Zambia",
246
+ :ZW => "Zimbabwe",
247
+ }
248
+
249
+ }
250
+ }
data/locale/ru.rb ADDED
@@ -0,0 +1,250 @@
1
+ { :ru => {
2
+
3
+ :countries => {
4
+ :AD => "Андорра",
5
+ :AE => "Объединенные Арабские Эмираты",
6
+ :AF => "Афганистан",
7
+ :AG => "Антигуа и Барбуда",
8
+ :AI => "Ангилья",
9
+ :AL => "Албания",
10
+ :AM => "Армения",
11
+ :AN => "Нидерландские Антильские о-ва",
12
+ :AO => "Ангола",
13
+ :AQ => "Антарктида",
14
+ :AR => "Аргентина",
15
+ :AS => "Американское Самоа",
16
+ :AT => "Австрия",
17
+ :AU => "Австралия",
18
+ :AW => "Аруба",
19
+ :AX => "Аландские о-ва",
20
+ :AZ => "Азербайджан",
21
+ :BA => "Босния и Герцеговина",
22
+ :BB => "Барбадос",
23
+ :BD => "Бангладеш",
24
+ :BE => "Бельгия",
25
+ :BF => "Буркина Фасо",
26
+ :BG => "Болгария",
27
+ :BH => "Бахрейн",
28
+ :BI => "Бурунди",
29
+ :BJ => "Бенин",
30
+ :BL => "Остров Святого Бартоломея",
31
+ :BM => "Бермудские о-ва",
32
+ :BN => "Бруней Даруссалам",
33
+ :BO => "Боливия",
34
+ :BR => "Бразилия",
35
+ :BS => "Багамские о-ва",
36
+ :BT => "Бутан",
37
+ :BV => "Остров Буве",
38
+ :BW => "Ботсвана",
39
+ :BY => "Беларусь",
40
+ :BZ => "Белиз",
41
+ :CA => "Канада",
42
+ :CC => "Кокосовые о-ва",
43
+ :CD => "Демократическая Республика Конго",
44
+ :CF => "Центральноафриканская Республика",
45
+ :CG => "Конго",
46
+ :CH => "Швейцария",
47
+ :CI => "Кот д’Ивуар",
48
+ :CK => "Острова Кука",
49
+ :CL => "Чили",
50
+ :CM => "Камерун",
51
+ :CN => "Китай",
52
+ :CO => "Колумбия",
53
+ :CR => "Коста-Рика",
54
+ :CU => "Куба",
55
+ :CV => "Острова Зеленого Мыса",
56
+ :CX => "Остров Рождества",
57
+ :CY => "Кипр",
58
+ :CZ => "Чехия",
59
+ :DE => "Германия",
60
+ :DJ => "Джибути",
61
+ :DK => "Дания",
62
+ :DM => "Доминика",
63
+ :DO => "Доминиканская Республика",
64
+ :DZ => "Алжир",
65
+ :EC => "Эквадор",
66
+ :EE => "Эстония",
67
+ :EG => "Египет",
68
+ :EH => "Западная Сахара",
69
+ :ER => "Эритрея",
70
+ :ES => "Испания",
71
+ :ET => "Эфиопия",
72
+ :FI => "Финляндия",
73
+ :FJ => "Фиджи",
74
+ :FK => "Фолклендские о-ва",
75
+ :FM => "Федеративные Штаты Микронезии",
76
+ :FO => "Фарерские о-ва",
77
+ :FR => "Франция",
78
+ :GA => "Габон",
79
+ :GB => "Великобритания",
80
+ :GD => "Гренада",
81
+ :GE => "Грузия",
82
+ :GF => "Французская Гвиана",
83
+ :GG => "Гернси",
84
+ :GH => "Гана",
85
+ :GI => "Гибралтар",
86
+ :GL => "Гренландия",
87
+ :GM => "Гамбия",
88
+ :GN => "Гвинея",
89
+ :GP => "Гваделупа",
90
+ :GQ => "Экваториальная Гвинея",
91
+ :GR => "Греция",
92
+ :GS => "Южная Джорджия и Южные Сандвичевы Острова",
93
+ :GT => "Гватемала",
94
+ :GU => "Гуам",
95
+ :GW => "Гвинея-Бисау",
96
+ :GY => "Гайана",
97
+ :HK => "Гонконг",
98
+ :HM => "Острова Херд и Макдональд",
99
+ :HN => "Гондурас",
100
+ :HR => "Хорватия",
101
+ :HT => "Гаити",
102
+ :HU => "Венгрия",
103
+ :ID => "Индонезия",
104
+ :IE => "Ирландия",
105
+ :IL => "Израиль",
106
+ :IM => "Остров Мэн",
107
+ :IN => "Индия",
108
+ :IQ => "Ирак",
109
+ :IR => "Иран",
110
+ :IS => "Исландия",
111
+ :IT => "Италия",
112
+ :JE => "Джерси",
113
+ :JM => "Ямайка",
114
+ :JO => "Иордания",
115
+ :JP => "Япония",
116
+ :KE => "Кения",
117
+ :KG => "Киргизия",
118
+ :KH => "Камбоджа",
119
+ :KI => "Кирибати",
120
+ :KM => "Коморские о-ва",
121
+ :KN => "Сент-Киттс и Невис",
122
+ :KP => "Северная Корея",
123
+ :KR => "Республика Корея",
124
+ :KW => "Кувейт",
125
+ :KY => "Каймановы острова",
126
+ :KZ => "Казахстан",
127
+ :LA => "Лаос",
128
+ :LB => "Ливан",
129
+ :LC => "Сент-Люсия",
130
+ :LI => "Лихтенштейн",
131
+ :LK => "Шри-Ланка",
132
+ :LR => "Либерия",
133
+ :LS => "Лесото",
134
+ :LT => "Литва",
135
+ :LU => "Люксембург",
136
+ :LV => "Латвия",
137
+ :LY => "Ливия",
138
+ :MA => "Марокко",
139
+ :MC => "Монако",
140
+ :MD => "Молдова",
141
+ :ME => "Черногория",
142
+ :MF => "Остров Святого Мартина",
143
+ :MG => "Мадагаскар",
144
+ :MH => "Маршалловы о-ва",
145
+ :MK => "Македония",
146
+ :ML => "Мали",
147
+ :MM => "Мьянма",
148
+ :MN => "Монголия",
149
+ :MO => "Макао",
150
+ :MP => "Северные Марианские о-ва",
151
+ :MQ => "Мартиника",
152
+ :MR => "Мавритания",
153
+ :MS => "Монтсеррат",
154
+ :MT => "Мальта",
155
+ :MU => "Маврикий",
156
+ :MV => "Мальдивские о-ва",
157
+ :MW => "Малави",
158
+ :MX => "Мексика",
159
+ :MY => "Малайзия",
160
+ :MZ => "Мозамбик",
161
+ :NA => "Намибия",
162
+ :NC => "Новая Каледония",
163
+ :NE => "Нигер",
164
+ :NF => "Остров Норфолк",
165
+ :NG => "Нигерия",
166
+ :NI => "Никарагуа",
167
+ :NL => "Нидерланды",
168
+ :NO => "Норвегия",
169
+ :NP => "Непал",
170
+ :NR => "Науру",
171
+ :NU => "Ниуе",
172
+ :NZ => "Новая Зеландия",
173
+ :OM => "Оман",
174
+ :PA => "Панама",
175
+ :PE => "Перу",
176
+ :PF => "Французская Полинезия",
177
+ :PG => "Папуа – Новая Гвинея",
178
+ :PH => "Филиппины",
179
+ :PK => "Пакистан",
180
+ :PL => "Польша",
181
+ :PM => "Сен-Пьер и Микелон",
182
+ :PN => "Питкэрн",
183
+ :PR => "Пуэрто-Рико",
184
+ :PS => "Палестинские территории",
185
+ :PT => "Португалия",
186
+ :PW => "Палау",
187
+ :PY => "Парагвай",
188
+ :QA => "Катар",
189
+ :RE => "Реюньон",
190
+ :RO => "Румыния",
191
+ :RS => "Сербия",
192
+ :RU => "Россия",
193
+ :RW => "Руанда",
194
+ :SA => "Саудовская Аравия",
195
+ :SB => "Соломоновы о-ва",
196
+ :SC => "Сейшельские о-ва",
197
+ :SD => "Судан",
198
+ :SE => "Швеция",
199
+ :SG => "Сингапур",
200
+ :SH => "Остров Святой Елены",
201
+ :SI => "Словения",
202
+ :SJ => "Свальбард и Ян-Майен",
203
+ :SK => "Словакия",
204
+ :SL => "Сьерра-Леоне",
205
+ :SM => "Сан-Марино",
206
+ :SN => "Сенегал",
207
+ :SO => "Сомали",
208
+ :SR => "Суринам",
209
+ :ST => "Сан-Томе и Принсипи",
210
+ :SV => "Сальвадор",
211
+ :SY => "Сирия",
212
+ :SZ => "Свазиленд",
213
+ :TC => "Острова Тёркс и Кайкос",
214
+ :TD => "Чад",
215
+ :TF => "Французские Южные Территории",
216
+ :TG => "Того",
217
+ :TH => "Таиланд",
218
+ :TJ => "Таджикистан",
219
+ :TK => "Токелау",
220
+ :TL => "Восточный Тимор",
221
+ :TM => "Туркменистан",
222
+ :TN => "Тунис",
223
+ :TO => "Тонга",
224
+ :TR => "Турция",
225
+ :TT => "Тринидад и Тобаго",
226
+ :TV => "Тувалу",
227
+ :TW => "Тайвань",
228
+ :TZ => "Танзания",
229
+ :UA => "Украина",
230
+ :UG => "Уганда",
231
+ :US => "США",
232
+ :UY => "Уругвай",
233
+ :UZ => "Узбекистан",
234
+ :VA => "Ватикан",
235
+ :VC => "Сент-Винсент и Гренадины",
236
+ :VE => "Венесуэла",
237
+ :VG => "Британские Виргинские о-ва",
238
+ :VI => "Виргинские о-ва (США)",
239
+ :VN => "Вьетнам",
240
+ :VU => "Вануату",
241
+ :WS => "Самоа",
242
+ :YE => "Йемен",
243
+ :YT => "Майотта",
244
+ :ZA => "ЮАР",
245
+ :ZM => "Замбия",
246
+ :ZW => "Зимбабве",
247
+ }
248
+
249
+ }
250
+ }
@@ -0,0 +1,47 @@
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 = "magic-localized_country_select"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["LIM SAS", "Damien MATHIEU", "Julien SANCHEZ", "Herv\u{e9} GAUCHER"]
12
+ s.date = "2012-01-18"
13
+ s.description = "Localized country select list"
14
+ s.email = "mail@magiclabs.de"
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "init.rb",
24
+ "install.rb",
25
+ "lib/localized_country_select.rb",
26
+ "lib/tasks/localized_country_select_tasks.rake",
27
+ "locale/en.rb",
28
+ "locale/ru.rb",
29
+ "magic-localized_country_select.gemspec",
30
+ "test/localized_country_select_test.rb",
31
+ "uninstall.rb"
32
+ ]
33
+ s.homepage = "https://github.com/magiclabs/localized_country_select"
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.10"
36
+ s.summary = "Localized country select list"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ else
43
+ end
44
+ else
45
+ end
46
+ end
47
+
@@ -0,0 +1,117 @@
1
+ # coding: utf-8
2
+ $KCODE = 'u'
3
+
4
+ require 'test/unit'
5
+
6
+ require 'rubygems'
7
+ require 'active_support'
8
+ require 'action_controller'
9
+ # require 'action_controller/test_process'
10
+ require 'action_view'
11
+ require 'action_view/helpers'
12
+ require 'action_view/helpers/tag_helper'
13
+ require 'i18n'
14
+
15
+ begin
16
+ require 'redgreen'
17
+ rescue LoadError
18
+ puts "[!] Install redgreen gem for better test output ($ sudo gem install redgreen)"
19
+ end unless ENV["TM_FILEPATH"]
20
+
21
+ require 'localized_country_select'
22
+
23
+ class LocalizedCountrySelectTest < Test::Unit::TestCase
24
+
25
+ include ActionView::Helpers::TagHelper
26
+ include ActionView::Helpers::FormOptionsHelper
27
+ include ActionView::Helpers::FormTagHelper
28
+
29
+ def test_action_view_should_include_helper_for_object
30
+ assert ActionView::Helpers::FormBuilder.instance_methods.include?('country_select') # WTF not working with 1.9
31
+ assert ActionView::Helpers::FormOptionsHelper.instance_methods.include?('country_select')
32
+ end
33
+
34
+ def test_action_view_should_include_helper_tag
35
+ assert ActionView::Helpers::FormOptionsHelper.instance_methods.include?('country_select_tag') # WTF not working with 1.9
36
+ end
37
+
38
+ def test_should_return_select_tag_with_proper_name_for_object
39
+ # puts country_select(:user, :country)
40
+ assert country_select(:user, :country) =~
41
+ Regexp.new(Regexp.escape('<select id="user_country" name="user[country]">')),
42
+ "Should have proper name for object"
43
+ end
44
+
45
+ def test_should_return_select_tag_with_proper_name
46
+ # puts country_select_tag( "competition_submission[data][citizenship]", nil)
47
+ assert country_select_tag( "competition_submission[data][citizenship]", nil) =~
48
+ Regexp.new(
49
+ Regexp.escape('<select id="competition_submission_data_citizenship" name="competition_submission[data][citizenship]">') ),
50
+ "Should have proper name"
51
+ end
52
+
53
+ def test_should_return_option_tags
54
+ assert country_select(:user, :country) =~ Regexp.new(Regexp.escape('<option value="ES">Spain</option>'))
55
+ end
56
+
57
+ def test_should_return_localized_option_tags
58
+ I18n.locale = 'ru'
59
+ assert country_select(:user, :country) =~ Regexp.new(Regexp.escape('<option value="ES">Испания</option>'))
60
+ end
61
+
62
+ def test_should_return_priority_countries_first
63
+ assert country_options_for_select(nil, [:ES, :CZ]) =~ Regexp.new(
64
+ Regexp.escape("<option value=\"ES\">Spain</option>\n<option value=\"CZ\">Czech Republic</option><option value=\"\" disabled=\"disabled\">-------------</option>\n<option value=\"AF\">Afghanistan</option>\n"))
65
+ end
66
+
67
+ def test_i18n_should_know_about_countries
68
+ assert_equal 'Spain', I18n.t('ES', :scope => 'countries')
69
+ I18n.locale = 'ru'
70
+ assert_equal 'Испания', I18n.t('ES', :scope => 'countries')
71
+ end
72
+
73
+ def test_localized_countries_array_returns_correctly
74
+ assert_nothing_raised { LocalizedCountrySelect::localized_countries_array() }
75
+ # puts LocalizedCountrySelect::localized_countries_array.inspect
76
+ I18n.locale = 'en'
77
+ assert_equal 243, LocalizedCountrySelect::localized_countries_array.size
78
+ assert_equal 'Afghanistan', LocalizedCountrySelect::localized_countries_array.first[0]
79
+ I18n.locale = 'ru'
80
+ assert_equal 243, LocalizedCountrySelect::localized_countries_array.size
81
+ assert_equal 'Австралия', LocalizedCountrySelect::localized_countries_array.first[0]
82
+ end
83
+
84
+ def test_priority_countries_returns_correctly_and_in_correct_order
85
+ assert_nothing_raised { LocalizedCountrySelect::priority_countries_array([:TW, :CN]) }
86
+ I18n.locale = 'en'
87
+ assert_equal [ ['Taiwan', 'TW'], ['China', 'CN'] ], LocalizedCountrySelect::priority_countries_array([:TW, :CN])
88
+ end
89
+
90
+ def test_priority_countries_allows_passing_either_symbol_or_string
91
+ I18n.locale = 'en'
92
+ assert_equal [ ['United States', 'US'], ['Canada', 'CA'] ], LocalizedCountrySelect::priority_countries_array(['US', 'CA'])
93
+ end
94
+
95
+ def test_priority_countries_allows_passing_upcase_or_lowercase
96
+ I18n.locale = 'en'
97
+ assert_equal [ ['United States', 'US'], ['Canada', 'CA'] ], LocalizedCountrySelect::priority_countries_array(['us', 'ca'])
98
+ assert_equal [ ['United States', 'US'], ['Canada', 'CA'] ], LocalizedCountrySelect::priority_countries_array([:us, :ca])
99
+ end
100
+
101
+ def test_should_list_countries_with_accented_names_in_correct_order
102
+ I18n.locale = 'ru'
103
+ assert_match Regexp.new(Regexp.escape(%Q{<option value="BT">Бутан</option>\n<option value="VU">Вануату</option>})), country_select(:user, :country)
104
+ end
105
+
106
+ # private
107
+
108
+ def setup
109
+ ['ru', 'en'].each do |locale|
110
+ # I18n.load_translations( File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb") ) # <-- Old style! :)
111
+ I18n.load_path += Dir[ File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb") ]
112
+ end
113
+ # I18n.locale = I18n.default_locale
114
+ I18n.locale = 'en'
115
+ end
116
+
117
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic-localized_country_select
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - LIM SAS
9
+ - Damien MATHIEU
10
+ - Julien SANCHEZ
11
+ - Hervé GAUCHER
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2012-01-18 00:00:00.000000000 Z
16
+ dependencies: []
17
+ description: Localized country select list
18
+ email: mail@magiclabs.de
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files:
22
+ - README.rdoc
23
+ files:
24
+ - MIT-LICENSE
25
+ - README.rdoc
26
+ - Rakefile
27
+ - VERSION
28
+ - init.rb
29
+ - install.rb
30
+ - lib/localized_country_select.rb
31
+ - lib/tasks/localized_country_select_tasks.rake
32
+ - locale/en.rb
33
+ - locale/ru.rb
34
+ - magic-localized_country_select.gemspec
35
+ - test/localized_country_select_test.rb
36
+ - uninstall.rb
37
+ homepage: https://github.com/magiclabs/localized_country_select
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.10
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Localized country select list
61
+ test_files: []