simonmenke-localized_country_select 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +24 -0
- data/lib/localized_country_select.rb +98 -0
- data/locale/en.rb +275 -0
- data/rails/init.rb +3 -0
- data/tasks/localized_country_select_tasks.rake +86 -0
- data/test/localized_country_select_test.rb +90 -0
- metadata +60 -0
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,43 @@
|
|
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
|
+
== Example
|
22
|
+
|
23
|
+
<%= localized_country_select(:user, :country, [], :include_blank => 'Please choose...') %>
|
24
|
+
|
25
|
+
will become:
|
26
|
+
|
27
|
+
<select name="user[country]" id="user_country">
|
28
|
+
<option value="">Please choose...</option>
|
29
|
+
<option disabled="disabled" value="">-------------</option>
|
30
|
+
<option value="AF">Afghanistan</option>
|
31
|
+
...
|
32
|
+
<option value="ZW">Zimbabwe</option>
|
33
|
+
</select>
|
34
|
+
|
35
|
+
for the <tt>en</tt> locale.
|
36
|
+
|
37
|
+
== Other resources
|
38
|
+
|
39
|
+
* http://github.com/rails/country_select (Default Rails plugin)
|
40
|
+
* http://github.com/russ/country_code_select (Stores country code, not name)
|
41
|
+
|
42
|
+
|
43
|
+
Copyright (c) 2008 Karel Minarik (www.karmi.cz), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
load File.join(File.dirname(__FILE__), 'tasks', 'localized_country_select_tasks.rake')
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Test the localized_country_select plugin.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the localized_country_select plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'LocalizedCountrySelect'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# = LocalizedCountrySelect
|
2
|
+
#
|
3
|
+
# View helper for displaying select list with countries:
|
4
|
+
#
|
5
|
+
# localized_country_select(:user, :country)
|
6
|
+
#
|
7
|
+
# Works just like the default Rails' +country_select+ plugin, but stores countries as
|
8
|
+
# country *codes*, not *names*, in the database.
|
9
|
+
#
|
10
|
+
# You can easily translate country codes in your application like this:
|
11
|
+
# <%= I18n.t @user.country, :scope => 'countries' %>
|
12
|
+
#
|
13
|
+
# Uses the Rails internationalization framework (I18n) for translating the names of countries.
|
14
|
+
#
|
15
|
+
# Use Rake task <tt>rake import:country_select 'de'</tt> for importing country names
|
16
|
+
# from Unicode.org's CLDR repository (http://www.unicode.org/cldr/data/charts/summary/root.html)
|
17
|
+
#
|
18
|
+
# Code 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
|
+
module LocalizedCountrySelect
|
22
|
+
class << self
|
23
|
+
# Returns array with codes and localized country names (according to <tt>I18n.locale</tt>)
|
24
|
+
# for <tt><option></tt> tags
|
25
|
+
def localized_countries_array
|
26
|
+
I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] }.sort
|
27
|
+
end
|
28
|
+
# Return array with codes and localized country names for array of country codes passed as argument
|
29
|
+
# == Example
|
30
|
+
# priority_countries_array([:TW, :CN])
|
31
|
+
# # => [ ['Taiwan', 'TW'], ['China', 'CN'] ]
|
32
|
+
def priority_countries_array(country_codes=[])
|
33
|
+
countries = I18n.translate(:countries)
|
34
|
+
country_codes.map { |code| [countries[code], code.to_s.upcase] }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module ActionView
|
40
|
+
module Helpers
|
41
|
+
|
42
|
+
module FormOptionsHelper
|
43
|
+
|
44
|
+
# Return select and option tags for the given object and method, using +localized_country_options_for_select+
|
45
|
+
# to generate the list of option tags. Uses <b>country code</b>, not name as option +value+.
|
46
|
+
# Country codes listed as an array of symbols in +priority_countries+ argument will be listed first
|
47
|
+
# TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
|
48
|
+
def localized_country_select(object, method, priority_countries = nil, options = {}, html_options = {})
|
49
|
+
InstanceTag.new(object, method, self, options.delete(:object)).
|
50
|
+
to_localized_country_select_tag(priority_countries, options, html_options)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Return "named" select and option tags according to given arguments.
|
54
|
+
# Use +selected_value+ for setting initial value
|
55
|
+
# It behaves likes older object-binded brother +localized_country_select+ otherwise
|
56
|
+
# TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
|
57
|
+
def localized_country_select_tag(name, selected_value = nil, priority_countries = nil, html_options = {})
|
58
|
+
content_tag :select,
|
59
|
+
localized_country_options_for_select(selected_value, priority_countries),
|
60
|
+
{ "name" => name, "id" => name }.update(html_options.stringify_keys)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns a string of option tags for countries according to locale. Supply the country code in upper-case ('US', 'DE')
|
64
|
+
# as +selected+ to have it marked as the selected option tag.
|
65
|
+
# Country codes listed as an array of symbols in +priority_countries+ argument will be listed first
|
66
|
+
def localized_country_options_for_select(selected = nil, priority_countries = nil)
|
67
|
+
country_options = ""
|
68
|
+
if priority_countries
|
69
|
+
country_options += options_for_select(LocalizedCountrySelect::priority_countries_array(priority_countries), selected)
|
70
|
+
country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
|
71
|
+
end
|
72
|
+
return country_options + options_for_select(LocalizedCountrySelect::localized_countries_array, selected)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
class InstanceTag
|
78
|
+
def to_localized_country_select_tag(priority_countries, options, html_options)
|
79
|
+
html_options = html_options.stringify_keys
|
80
|
+
add_default_name_and_id(html_options)
|
81
|
+
value = value(object)
|
82
|
+
content_tag("select",
|
83
|
+
add_options(
|
84
|
+
localized_country_options_for_select(value, priority_countries),
|
85
|
+
options, value
|
86
|
+
), html_options
|
87
|
+
)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class FormBuilder
|
92
|
+
def localized_country_select(method, priority_countries = nil, options = {}, html_options = {})
|
93
|
+
@template.localized_country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
data/locale/en.rb
ADDED
@@ -0,0 +1,275 @@
|
|
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
|
+
:BQ => "British Antarctic Territory",
|
35
|
+
:BR => "Brazil",
|
36
|
+
:BS => "Bahamas",
|
37
|
+
:BT => "Bhutan",
|
38
|
+
:BV => "Bouvet Island",
|
39
|
+
:BW => "Botswana",
|
40
|
+
:BY => "Belarus",
|
41
|
+
:BZ => "Belize",
|
42
|
+
:CA => "Canada",
|
43
|
+
:CC => "Cocos Islands",
|
44
|
+
:CD => "Congo - Kinshasa",
|
45
|
+
:CF => "Central African Republic",
|
46
|
+
:CG => "Congo - Brazzaville",
|
47
|
+
:CH => "Switzerland",
|
48
|
+
:CI => "Ivory Coast",
|
49
|
+
:CK => "Cook Islands",
|
50
|
+
:CL => "Chile",
|
51
|
+
:CM => "Cameroon",
|
52
|
+
:CN => "China",
|
53
|
+
:CO => "Colombia",
|
54
|
+
:CR => "Costa Rica",
|
55
|
+
:CS => "Serbia and Montenegro",
|
56
|
+
:CT => "Canton and Enderbury Islands",
|
57
|
+
:CU => "Cuba",
|
58
|
+
:CV => "Cape Verde",
|
59
|
+
:CX => "Christmas Island",
|
60
|
+
:CY => "Cyprus",
|
61
|
+
:CZ => "Czech Republic",
|
62
|
+
:DD => "East Germany",
|
63
|
+
:DE => "Germany",
|
64
|
+
:DJ => "Djibouti",
|
65
|
+
:DK => "Denmark",
|
66
|
+
:DM => "Dominica",
|
67
|
+
:DO => "Dominican Republic",
|
68
|
+
:DZ => "Algeria",
|
69
|
+
:EC => "Ecuador",
|
70
|
+
:EE => "Estonia",
|
71
|
+
:EG => "Egypt",
|
72
|
+
:EH => "Western Sahara",
|
73
|
+
:ER => "Eritrea",
|
74
|
+
:ES => "Spain",
|
75
|
+
:ET => "Ethiopia",
|
76
|
+
:FI => "Finland",
|
77
|
+
:FJ => "Fiji",
|
78
|
+
:FK => "Falkland Islands",
|
79
|
+
:FM => "Micronesia",
|
80
|
+
:FO => "Faroe Islands",
|
81
|
+
:FQ => "French Southern and Antarctic Territories",
|
82
|
+
:FR => "France",
|
83
|
+
:FX => "Metropolitan France",
|
84
|
+
:GA => "Gabon",
|
85
|
+
:GB => "United Kingdom",
|
86
|
+
:GD => "Grenada",
|
87
|
+
:GE => "Georgia",
|
88
|
+
:GF => "French Guiana",
|
89
|
+
:GG => "Guernsey",
|
90
|
+
:GH => "Ghana",
|
91
|
+
:GI => "Gibraltar",
|
92
|
+
:GL => "Greenland",
|
93
|
+
:GM => "Gambia",
|
94
|
+
:GN => "Guinea",
|
95
|
+
:GP => "Guadeloupe",
|
96
|
+
:GQ => "Equatorial Guinea",
|
97
|
+
:GR => "Greece",
|
98
|
+
:GS => "South Georgia and the South Sandwich Islands",
|
99
|
+
:GT => "Guatemala",
|
100
|
+
:GU => "Guam",
|
101
|
+
:GW => "Guinea-Bissau",
|
102
|
+
:GY => "Guyana",
|
103
|
+
:HK => "Hong Kong SAR China",
|
104
|
+
:HK => "Hong Kong",
|
105
|
+
:HM => "Heard Island and McDonald Islands",
|
106
|
+
:HN => "Honduras",
|
107
|
+
:HR => "Croatia",
|
108
|
+
:HT => "Haiti",
|
109
|
+
:HU => "Hungary",
|
110
|
+
:ID => "Indonesia",
|
111
|
+
:IE => "Ireland",
|
112
|
+
:IL => "Israel",
|
113
|
+
:IM => "Isle of Man",
|
114
|
+
:IN => "India",
|
115
|
+
:IO => "British Indian Ocean Territory",
|
116
|
+
:IQ => "Iraq",
|
117
|
+
:IR => "Iran",
|
118
|
+
:IS => "Iceland",
|
119
|
+
:IT => "Italy",
|
120
|
+
:JE => "Jersey",
|
121
|
+
:JM => "Jamaica",
|
122
|
+
:JO => "Jordan",
|
123
|
+
:JP => "Japan",
|
124
|
+
:JT => "Johnston Island",
|
125
|
+
:KE => "Kenya",
|
126
|
+
:KG => "Kyrgyzstan",
|
127
|
+
:KH => "Cambodia",
|
128
|
+
:KI => "Kiribati",
|
129
|
+
:KM => "Comoros",
|
130
|
+
:KN => "Saint Kitts and Nevis",
|
131
|
+
:KP => "North Korea",
|
132
|
+
:KR => "South Korea",
|
133
|
+
:KW => "Kuwait",
|
134
|
+
:KY => "Cayman Islands",
|
135
|
+
:KZ => "Kazakhstan",
|
136
|
+
:LA => "Laos",
|
137
|
+
:LB => "Lebanon",
|
138
|
+
:LC => "Saint Lucia",
|
139
|
+
:LI => "Liechtenstein",
|
140
|
+
:LK => "Sri Lanka",
|
141
|
+
:LR => "Liberia",
|
142
|
+
:LS => "Lesotho",
|
143
|
+
:LT => "Lithuania",
|
144
|
+
:LU => "Luxembourg",
|
145
|
+
:LV => "Latvia",
|
146
|
+
:LY => "Libya",
|
147
|
+
:MA => "Morocco",
|
148
|
+
:MC => "Monaco",
|
149
|
+
:MD => "Moldova",
|
150
|
+
:ME => "Montenegro",
|
151
|
+
:MF => "Saint Martin",
|
152
|
+
:MG => "Madagascar",
|
153
|
+
:MH => "Marshall Islands",
|
154
|
+
:MI => "Midway Islands",
|
155
|
+
:MK => "Macedonia",
|
156
|
+
:ML => "Mali",
|
157
|
+
:MM => "Myanmar",
|
158
|
+
:MN => "Mongolia",
|
159
|
+
:MO => "Macau SAR China",
|
160
|
+
:MO => "Macau",
|
161
|
+
:MP => "Northern Mariana Islands",
|
162
|
+
:MQ => "Martinique",
|
163
|
+
:MR => "Mauritania",
|
164
|
+
:MS => "Montserrat",
|
165
|
+
:MT => "Malta",
|
166
|
+
:MU => "Mauritius",
|
167
|
+
:MV => "Maldives",
|
168
|
+
:MW => "Malawi",
|
169
|
+
:MX => "Mexico",
|
170
|
+
:MY => "Malaysia",
|
171
|
+
:MZ => "Mozambique",
|
172
|
+
:NA => "Namibia",
|
173
|
+
:NC => "New Caledonia",
|
174
|
+
:NE => "Niger",
|
175
|
+
:NF => "Norfolk Island",
|
176
|
+
:NG => "Nigeria",
|
177
|
+
:NI => "Nicaragua",
|
178
|
+
:NL => "Netherlands",
|
179
|
+
:NO => "Norway",
|
180
|
+
:NP => "Nepal",
|
181
|
+
:NQ => "Dronning Maud Land",
|
182
|
+
:NR => "Nauru",
|
183
|
+
:NT => "Neutral Zone",
|
184
|
+
:NU => "Niue",
|
185
|
+
:NZ => "New Zealand",
|
186
|
+
:OM => "Oman",
|
187
|
+
:PA => "Panama",
|
188
|
+
:PC => "Pacific Islands Trust Territory",
|
189
|
+
:PE => "Peru",
|
190
|
+
:PF => "French Polynesia",
|
191
|
+
:PG => "Papua New Guinea",
|
192
|
+
:PH => "Philippines",
|
193
|
+
:PK => "Pakistan",
|
194
|
+
:PL => "Poland",
|
195
|
+
:PM => "Saint Pierre and Miquelon",
|
196
|
+
:PN => "Pitcairn",
|
197
|
+
:PR => "Puerto Rico",
|
198
|
+
:PS => "Palestinian Territory",
|
199
|
+
:PT => "Portugal",
|
200
|
+
:PU => "U.S. Miscellaneous Pacific Islands",
|
201
|
+
:PW => "Palau",
|
202
|
+
:PY => "Paraguay",
|
203
|
+
:PZ => "Panama Canal Zone",
|
204
|
+
:QA => "Qatar",
|
205
|
+
:QO => "Outlying Oceania",
|
206
|
+
:QU => "European Union",
|
207
|
+
:RE => "Reunion",
|
208
|
+
:RO => "Romania",
|
209
|
+
:RS => "Serbia",
|
210
|
+
:RU => "Russia",
|
211
|
+
:RW => "Rwanda",
|
212
|
+
:SA => "Saudi Arabia",
|
213
|
+
:SB => "Solomon Islands",
|
214
|
+
:SC => "Seychelles",
|
215
|
+
:SD => "Sudan",
|
216
|
+
:SE => "Sweden",
|
217
|
+
:SG => "Singapore",
|
218
|
+
:SH => "Saint Helena",
|
219
|
+
:SI => "Slovenia",
|
220
|
+
:SJ => "Svalbard and Jan Mayen",
|
221
|
+
:SK => "Slovakia",
|
222
|
+
:SL => "Sierra Leone",
|
223
|
+
:SM => "San Marino",
|
224
|
+
:SN => "Senegal",
|
225
|
+
:SO => "Somalia",
|
226
|
+
:SR => "Suriname",
|
227
|
+
:ST => "Sao Tome and Principe",
|
228
|
+
:SU => "Union of Soviet Socialist Republics",
|
229
|
+
:SV => "El Salvador",
|
230
|
+
:SY => "Syria",
|
231
|
+
:SZ => "Swaziland",
|
232
|
+
:TC => "Turks and Caicos Islands",
|
233
|
+
:TD => "Chad",
|
234
|
+
:TF => "French Southern Territories",
|
235
|
+
:TG => "Togo",
|
236
|
+
:TH => "Thailand",
|
237
|
+
:TJ => "Tajikistan",
|
238
|
+
:TK => "Tokelau",
|
239
|
+
:TL => "East Timor",
|
240
|
+
:TM => "Turkmenistan",
|
241
|
+
:TN => "Tunisia",
|
242
|
+
:TO => "Tonga",
|
243
|
+
:TR => "Turkey",
|
244
|
+
:TT => "Trinidad and Tobago",
|
245
|
+
:TV => "Tuvalu",
|
246
|
+
:TW => "Taiwan",
|
247
|
+
:TZ => "Tanzania",
|
248
|
+
:UA => "Ukraine",
|
249
|
+
:UG => "Uganda",
|
250
|
+
:UM => "United States Minor Outlying Islands",
|
251
|
+
:US => "United States",
|
252
|
+
:UY => "Uruguay",
|
253
|
+
:UZ => "Uzbekistan",
|
254
|
+
:VA => "Vatican",
|
255
|
+
:VC => "Saint Vincent and the Grenadines",
|
256
|
+
:VD => "North Vietnam",
|
257
|
+
:VE => "Venezuela",
|
258
|
+
:VG => "British Virgin Islands",
|
259
|
+
:VI => "U.S. Virgin Islands",
|
260
|
+
:VN => "Vietnam",
|
261
|
+
:VU => "Vanuatu",
|
262
|
+
:WF => "Wallis and Futuna",
|
263
|
+
:WK => "Wake Island",
|
264
|
+
:WS => "Samoa",
|
265
|
+
:YD => "People's Democratic Republic of Yemen",
|
266
|
+
:YE => "Yemen",
|
267
|
+
:YT => "Mayotte",
|
268
|
+
:ZA => "South Africa",
|
269
|
+
:ZM => "Zambia",
|
270
|
+
:ZW => "Zimbabwe",
|
271
|
+
:ZZ => "Unknown or Invalid Region",
|
272
|
+
}
|
273
|
+
|
274
|
+
}
|
275
|
+
}
|
data/rails/init.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hpricot'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
# Rake task for importing country 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:country_select '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 country codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
|
23
|
+
task :country_select do
|
24
|
+
|
25
|
+
# TODO : Implement locale import chooser from CLDR root via Highline
|
26
|
+
|
27
|
+
# Setup variables
|
28
|
+
locale = ARGV[1]
|
29
|
+
unless locale
|
30
|
+
puts "\n[!] Usage: rake import:country_select 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
|
+
|
44
|
+
# ----- Parse the HTML with Hpricot ----------------------------------------
|
45
|
+
puts "... parsing the HTML file"
|
46
|
+
countries = []
|
47
|
+
doc.search("//tr").each do |row|
|
48
|
+
if row.search("td[@class='n']") &&
|
49
|
+
row.search("td[@class='n']").inner_html =~ /^namesterritory$/ &&
|
50
|
+
row.search("td[@class='g']").inner_html =~ /^[A-Z]{2}/
|
51
|
+
code = row.search("td[@class='g']").inner_text
|
52
|
+
code = code[-code.size, 2]
|
53
|
+
name = row.search("td[@class='v']").inner_text
|
54
|
+
countries << { :code => code.to_sym, :name => name.to_s }
|
55
|
+
print " ... #{name}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
# ----- Prepare the output format ------------------------------------------
|
61
|
+
output =<<HEAD
|
62
|
+
{ :#{locale} => {
|
63
|
+
|
64
|
+
:countries => {
|
65
|
+
HEAD
|
66
|
+
countries.each do |country|
|
67
|
+
output << "\t\t\t:#{country[:code]} => \"#{country[:name]}\",\n"
|
68
|
+
end
|
69
|
+
output <<<<TAIL
|
70
|
+
}
|
71
|
+
|
72
|
+
}
|
73
|
+
}
|
74
|
+
TAIL
|
75
|
+
|
76
|
+
|
77
|
+
# ----- Write the parsed values into file ---------------------------------
|
78
|
+
puts "\n... writing the output"
|
79
|
+
filename = File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb")
|
80
|
+
filename += '.NEW' if File.exists?(filename) # Append 'NEW' if file exists
|
81
|
+
File.open(filename, 'w+') { |f| f << output }
|
82
|
+
puts "\n---\nWritten values for the '#{locale}' into file: #{filename}\n"
|
83
|
+
# ------------------------------------------------------------------------------
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_support'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_controller/test_process'
|
7
|
+
require 'action_view'
|
8
|
+
require 'action_view/helpers/tag_helper'
|
9
|
+
require 'i18n'
|
10
|
+
|
11
|
+
require 'localized_country_select'
|
12
|
+
|
13
|
+
class LocalizedCountrySelectTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
include ActionView::Helpers::FormOptionsHelper
|
16
|
+
include ActionView::Helpers::TagHelper
|
17
|
+
|
18
|
+
def test_action_view_should_include_helper_for_object
|
19
|
+
assert ActionView::Helpers::FormBuilder.instance_methods.include?('localized_country_select')
|
20
|
+
assert ActionView::Helpers::FormOptionsHelper.instance_methods.include?('localized_country_select')
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_action_view_should_include_helper_tag
|
24
|
+
assert ActionView::Helpers::FormOptionsHelper.instance_methods.include?('localized_country_select_tag')
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_return_select_tag_with_proper_name_for_object
|
28
|
+
# puts localized_country_select(:user, :country)
|
29
|
+
assert localized_country_select(:user, :country) =~
|
30
|
+
Regexp.new(Regexp.escape('<select id="user_country" name="user[country]">')),
|
31
|
+
"Should have proper name for object"
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_return_select_tag_with_proper_name
|
35
|
+
# puts localized_country_select_tag( "competition_submission[data][citizenship]", nil)
|
36
|
+
assert localized_country_select_tag( "competition_submission[data][citizenship]", nil) =~
|
37
|
+
Regexp.new(
|
38
|
+
Regexp.escape('<select id="competition_submission[data][citizenship]" name="competition_submission[data][citizenship]">') ),
|
39
|
+
"Should have proper name"
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_return_option_tags
|
43
|
+
assert localized_country_select(:user, :country) =~ Regexp.new(Regexp.escape('<option value="ES">Spain</option>'))
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_return_localized_option_tags
|
47
|
+
I18n.locale = 'cz'
|
48
|
+
assert localized_country_select(:user, :country) =~ Regexp.new(Regexp.escape('<option value="ES">Španělsko</option>'))
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_return_priority_countries_first
|
52
|
+
assert localized_country_options_for_select(nil, [:ES, :CZ]) =~ Regexp.new(
|
53
|
+
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"))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_i18n_should_know_about_countries
|
57
|
+
assert_equal 'Spain', I18n.t('ES', :scope => 'countries')
|
58
|
+
I18n.locale = 'cz'
|
59
|
+
assert_equal 'Španělsko', I18n.t('ES', :scope => 'countries')
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_localized_countries_array_returns_correctly
|
63
|
+
assert_nothing_raised { LocalizedCountrySelect::localized_countries_array() }
|
64
|
+
# puts LocalizedCountrySelect::localized_countries_array.inspect
|
65
|
+
I18n.locale = 'en'
|
66
|
+
assert_equal 266, LocalizedCountrySelect::localized_countries_array.size
|
67
|
+
assert_equal 'Afghanistan', LocalizedCountrySelect::localized_countries_array.first[0]
|
68
|
+
I18n.locale = 'cz'
|
69
|
+
assert_equal 250, LocalizedCountrySelect::localized_countries_array.size
|
70
|
+
assert_equal 'Afghánistán', LocalizedCountrySelect::localized_countries_array.first[0]
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_priority_countries_returns_correctly_and_in_correct_order
|
74
|
+
assert_nothing_raised { LocalizedCountrySelect::priority_countries_array([:TW, :CN]) }
|
75
|
+
I18n.locale = 'en'
|
76
|
+
assert_equal [ ['Taiwan', 'TW'], ['China', 'CN'] ], LocalizedCountrySelect::priority_countries_array([:TW, :CN])
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def setup
|
82
|
+
['cz', 'en'].each do |locale|
|
83
|
+
# I18n.load_translations( File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb") ) # <-- Old style! :)
|
84
|
+
I18n.load_path += Dir[ File.join(File.dirname(__FILE__), '..', 'locale', "#{locale}.rb") ]
|
85
|
+
end
|
86
|
+
# I18n.locale = I18n.default_locale
|
87
|
+
I18n.locale = 'en'
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simonmenke-localized_country_select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Karel Minarik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rails plugin for localized "country_select" helper with Rake task for downloading locales from Unicode.org's CLDR
|
17
|
+
email: karel.minarik@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/localized_country_select.rb
|
26
|
+
- rails/init.rb
|
27
|
+
- tasks/localized_country_select_tasks.rake
|
28
|
+
- test/localized_country_select_test.rb
|
29
|
+
- locale/en.rb
|
30
|
+
- MIT-LICENSE
|
31
|
+
- Rakefile
|
32
|
+
- README.rdoc
|
33
|
+
has_rdoc: false
|
34
|
+
homepage: http://www.restafari.org/localized-country-select.html
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Rails plugin for localized "country_select" helper with Rake task for downloading locales from Unicode.org's CLDR
|
59
|
+
test_files:
|
60
|
+
- test/localized_country_select_test.rb
|