localized_country_select 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -18,24 +18,24 @@ Don't forget to restart the application when you add new locale.
|
|
18
18
|
ActionView helper code is adapted from Rails' default +country_select+ plugin (previously in core).
|
19
19
|
See http://github.com/rails/country_select/tree/master/lib/country_select.rb
|
20
20
|
|
21
|
-
== Rails 3
|
21
|
+
== Rails 3 / Rails 4
|
22
22
|
|
23
23
|
In your Gemfile add:
|
24
24
|
|
25
|
-
gem 'localized_country_select', '>= 0.9.
|
25
|
+
gem 'localized_country_select', '>= 0.9.3'
|
26
26
|
|
27
27
|
== Rails 2.3
|
28
28
|
|
29
29
|
No longer supported, but you can still use old version of gem.
|
30
30
|
In environment.rb add
|
31
31
|
|
32
|
-
config.gem 'localized_country_select,
|
32
|
+
config.gem 'localized_country_select', :version => '0.0.1'
|
33
33
|
|
34
34
|
== Example
|
35
35
|
|
36
36
|
Show full country names:
|
37
37
|
|
38
|
-
<%= localized_country_select(:user, :country, [], {:include_blank => 'Please choose...')
|
38
|
+
<%= localized_country_select(:user, :country, [], {:include_blank => 'Please choose...'}) %>
|
39
39
|
|
40
40
|
will become:
|
41
41
|
|
@@ -50,7 +50,7 @@ will become:
|
|
50
50
|
|
51
51
|
Show only country codes:
|
52
52
|
|
53
|
-
<%= localized_country_select(:user, :country, [], {:include_blank => 'Please choose...', :description => :abbreviated)
|
53
|
+
<%= localized_country_select(:user, :country, [], {:include_blank => 'Please choose...', :description => :abbreviated}) %>
|
54
54
|
|
55
55
|
will become:
|
56
56
|
|
@@ -66,6 +66,11 @@ will become:
|
|
66
66
|
for the <tt>en</tt> locale.
|
67
67
|
|
68
68
|
|
69
|
+
You can exclude countries by code using the exclude option (a single code or an array of country codes):
|
70
|
+
|
71
|
+
localized_country_select(:user, :country, [], {:exclude => [:ZZ, :US]})
|
72
|
+
|
73
|
+
|
69
74
|
== Formtastic and SimpleForm
|
70
75
|
|
71
76
|
Gem supports (via alias_method) both formtastic and simple_form :country input
|
@@ -24,13 +24,13 @@ module LocalizedCountrySelect
|
|
24
24
|
# Returns array with codes and localized country names (according to <tt>I18n.locale</tt>)
|
25
25
|
# for <tt><option></tt> tags
|
26
26
|
def localized_countries_array(options={})
|
27
|
+
exclude = Array(options[:exclude]).map {|code| code.to_s.upcase }
|
28
|
+
|
27
29
|
if(options[:description]==:abbreviated)
|
28
|
-
I18n.translate(:countries).map { |key, value| [key.to_s.upcase] }
|
29
|
-
sort_by { |country| country.first.parameterize }
|
30
|
+
I18n.translate(:countries).map { |key, value| [key.to_s.upcase] if !exclude.include?(key.to_s.upcase) }
|
30
31
|
else
|
31
|
-
I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] }
|
32
|
-
|
33
|
-
end
|
32
|
+
I18n.translate(:countries).map { |key, value| [value, key.to_s.upcase] if !exclude.include?(key.to_s.upcase) }
|
33
|
+
end.compact.sort_by { |country| country.first.parameterize }
|
34
34
|
end
|
35
35
|
# Return array with codes and localized country names for array of country codes passed as argument
|
36
36
|
# == Example
|
@@ -57,8 +57,15 @@ module ActionView
|
|
57
57
|
# Country codes listed as an array of symbols in +priority_countries+ argument will be listed first
|
58
58
|
# TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
|
59
59
|
def localized_country_select(object, method, priority_countries = nil, options = {}, html_options = {})
|
60
|
-
|
61
|
-
|
60
|
+
tag = if defined?(ActionView::Helpers::InstanceTag) &&
|
61
|
+
ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0
|
62
|
+
|
63
|
+
InstanceTag.new(object, method, self, options.delete(:object))
|
64
|
+
else
|
65
|
+
CountrySelect.new(object, method, self, options)
|
66
|
+
end
|
67
|
+
|
68
|
+
tag.to_localized_country_select_tag(priority_countries, options, html_options)
|
62
69
|
end
|
63
70
|
alias_method :country_select, :localized_country_select
|
64
71
|
|
@@ -88,7 +95,7 @@ module ActionView
|
|
88
95
|
|
89
96
|
end
|
90
97
|
|
91
|
-
|
98
|
+
module ToCountrySelectTag
|
92
99
|
def to_localized_country_select_tag(priority_countries, options, html_options)
|
93
100
|
html_options = html_options.stringify_keys
|
94
101
|
add_default_name_and_id(html_options)
|
@@ -102,6 +109,17 @@ module ActionView
|
|
102
109
|
end
|
103
110
|
end
|
104
111
|
|
112
|
+
if defined?(ActionView::Helpers::InstanceTag) &&
|
113
|
+
ActionView::Helpers::InstanceTag.instance_method(:initialize).arity != 0
|
114
|
+
class InstanceTag
|
115
|
+
include ToCountrySelectTag
|
116
|
+
end
|
117
|
+
else
|
118
|
+
class CountrySelect < Tags::Base
|
119
|
+
include ToCountrySelectTag
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
105
123
|
class FormBuilder
|
106
124
|
def localized_country_select(method, priority_countries = nil, options = {}, html_options = {})
|
107
125
|
@template.localized_country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
|
@@ -9,12 +9,18 @@ require 'open-uri'
|
|
9
9
|
#
|
10
10
|
# Don't forget to restart the application when you add new locale to load it into Rails!
|
11
11
|
#
|
12
|
-
# ==
|
13
|
-
#
|
12
|
+
# == Parameters
|
13
|
+
# LOCALE (required): Sets the locale to use. Output file name will include this.
|
14
|
+
# FORMAT (optional): Output format, either 'rb' or 'yml'. Defaults to 'rb' if not specified.
|
15
|
+
# WEB_LOCALE (optional): Forces a locale code to use when querying the Unicode.org CLDR archive.
|
16
|
+
#
|
17
|
+
# == Examples
|
18
|
+
# rake import:country_select LOCALE=de
|
19
|
+
# rake import:country_select LOCALE=pt-BR WEB_LOCALE=pt FORMAT=yml
|
14
20
|
#
|
15
21
|
# The code is deliberately procedural and simple, so it's easily
|
16
22
|
# understandable by beginners as an introduction to Rake tasks power.
|
17
|
-
# See
|
23
|
+
# See https://github.com/svenfuchs/ruby-cldr for much more robust solution
|
18
24
|
|
19
25
|
namespace :import do
|
20
26
|
|
@@ -36,12 +42,18 @@ namespace :import do
|
|
36
42
|
exit 0
|
37
43
|
end
|
38
44
|
|
45
|
+
# convert locale code to Unicode.org CLDR acceptable code
|
46
|
+
web_locale = if ENV['WEB_LOCALE'] then ENV['WEB_LOCALE']
|
47
|
+
elsif %w(zht zhtw).include?(locale.downcase.gsub(/[-_]/,'')) then 'zh_Hant'
|
48
|
+
elsif %w(zhs zhcn).include?(locale.downcase.gsub(/[-_]/,'')) then 'zh_Hans'
|
49
|
+
else locale.underscore.split('_')[0] end
|
50
|
+
|
39
51
|
# ----- Get the CLDR HTML --------------------------------------------------
|
40
52
|
begin
|
41
|
-
puts "... getting the HTML file for locale '#{
|
42
|
-
doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{
|
53
|
+
puts "... getting the HTML file for locale '#{web_locale}'"
|
54
|
+
doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{web_locale}.html") )
|
43
55
|
rescue => e
|
44
|
-
puts "[!] Invalid locale name '#{
|
56
|
+
puts "[!] Invalid locale name '#{web_locale}'! Not found in CLDR (#{e})"
|
45
57
|
exit 0
|
46
58
|
end
|
47
59
|
|
@@ -63,6 +75,25 @@ namespace :import do
|
|
63
75
|
|
64
76
|
|
65
77
|
# ----- Prepare the output format ------------------------------------------
|
78
|
+
|
79
|
+
format = if ENV['FORMAT'].nil?||%(rb ruby).include?(ENV['FORMAT'].downcase) then :rb
|
80
|
+
elsif %(yml yaml).include?(ENV['FORMAT'].downcase) then :yml end
|
81
|
+
|
82
|
+
unless format
|
83
|
+
puts "\n[!] FORMAT must be either 'rb' or 'yml'\n\n"
|
84
|
+
exit 0
|
85
|
+
end
|
86
|
+
|
87
|
+
if format==:yml
|
88
|
+
output =<<HEAD
|
89
|
+
#{locale}:
|
90
|
+
countries:
|
91
|
+
HEAD
|
92
|
+
countries.each do |country|
|
93
|
+
output << " #{country[:code]}: \"#{country[:name]}\"\n"
|
94
|
+
end
|
95
|
+
|
96
|
+
else # rb format
|
66
97
|
output = "#encoding: UTF-8\n"
|
67
98
|
output <<<<HEAD
|
68
99
|
{ :#{locale} => {
|
@@ -78,11 +109,11 @@ HEAD
|
|
78
109
|
}
|
79
110
|
}
|
80
111
|
TAIL
|
112
|
+
end
|
81
113
|
|
82
|
-
|
83
114
|
# ----- Write the parsed values into file ---------------------------------
|
84
115
|
puts "\n... writing the output"
|
85
|
-
filename = Rails.root.join('config', 'locales', "
|
116
|
+
filename = Rails.root.join('config', 'locales', "countries.#{locale}.#{format}")
|
86
117
|
if filename.exist?
|
87
118
|
filename = Pathname.new("#{filename.to_s}.NEW")
|
88
119
|
end
|
@@ -68,6 +68,18 @@ class LocalizedCountrySelectTest < Test::Unit::TestCase
|
|
68
68
|
assert_equal 'Španělsko', I18n.t('ES', :scope => 'countries')
|
69
69
|
end
|
70
70
|
|
71
|
+
def test_excludes_countries
|
72
|
+
assert_nothing_raised { LocalizedCountrySelect::localized_countries_array(:exclude => :ZZ) }
|
73
|
+
|
74
|
+
assert_block do
|
75
|
+
not LocalizedCountrySelect::localized_countries_array(:exclude => :ZZ).any? {|country| country.last == "ZZ"}
|
76
|
+
end
|
77
|
+
|
78
|
+
assert_block do
|
79
|
+
not LocalizedCountrySelect::localized_countries_array(:exclude => [:ZZ, :US]).any? {|country| country.last == "ZZ" or country.last == "US"}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
71
83
|
def test_localized_countries_array_returns_correctly
|
72
84
|
assert_nothing_raised { LocalizedCountrySelect::localized_countries_array() }
|
73
85
|
# puts LocalizedCountrySelect::localized_countries_array.inspect
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: localized_country_select
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -15,7 +15,7 @@ authors:
|
|
15
15
|
autorequire:
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: actionpack
|
@@ -106,10 +106,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 1.8.
|
109
|
+
rubygems_version: 1.8.23
|
110
110
|
signing_key:
|
111
111
|
specification_version: 3
|
112
112
|
summary: Localized "country_select" helper with Rake task for downloading locales
|
113
113
|
from Unicode.org's CLDR
|
114
114
|
test_files:
|
115
115
|
- test/localized_country_select_test.rb
|
116
|
+
has_rdoc:
|