world-flags 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,6 +27,8 @@ $("li.flag['data-cc'=dk).addClass('semi');
27
27
  $("li.flag['data-cc'=dk).removeClass('semi');
28
28
  ```
29
29
 
30
+ CSS configuration in `application.css` manifest.
31
+
30
32
  ```css
31
33
  */
32
34
  *= require_self
@@ -60,13 +62,16 @@ Alternatively for the 32 width flags
60
62
 
61
63
  ## Configuring localization
62
64
 
65
+ You can run the Rails generator `world_flags:init` in order to create a basic initializer file for world flags with an overview of the different options available.
66
+
63
67
  You can setup WorldFlags to use a localization map for the labels of the flag icons
64
68
 
69
+ ```ruby
65
70
  WorldFlags.languages = some_language_hash # fx loaded from a yaml file
66
71
  WorldFlags.countries = some_country_hash # fx loaded from a yaml file
72
+ ```
67
73
 
68
- Notice that it is a locale code pointing to a map of ISO_3166-1_alpha-2 codes
69
- to labels for that locale.
74
+ Notice that it is a locale code pointing to a map of *ISO_3166-1_alpha-2* codes to labels for that locale.
70
75
 
71
76
  ```ruby
72
77
  {
@@ -75,7 +80,7 @@ to labels for that locale.
75
80
  }
76
81
  ```
77
82
 
78
- An english translation file of country codes (in json format) can be found in app/config/country_codes.
83
+ An english translation file of country codes (in json format) can be found in `app/config/country_codes`.
79
84
 
80
85
  ```ruby
81
86
  cc_file_en = File.join(Rails.root, 'app/config/country_codes/iso-3166-2.en.json')
@@ -91,11 +96,15 @@ WorldFlags::Language.en = country_codes_en
91
96
 
92
97
  If you use this approach, you must currently add a WorldFlags::Language class method for that locale (fx a method `#da`for danish) or fx use `attr_accessor :da`. For this approach, you must also set the active locales using `#active_locales`.
93
98
 
94
- ```
99
+ ```ruby
95
100
  WorldFlags.active_locales = [:en, :da]
96
101
  WorldFlags::Language.da = country_codes_da
97
102
  ```
98
103
 
104
+ To customize the locale to flag code map, use:
105
+
106
+ `WorldFlags.locale_flag_map = some hash`
107
+
99
108
  Please feel free to suggest or improve this locale/translation infrastructure!
100
109
 
101
110
  ## Rendering
@@ -122,7 +131,7 @@ You can also use built in helper methods:
122
131
 
123
132
  ```haml
124
133
  = flag_list 16 do
125
- = flags [:ar, :gb], :selected => :gb
134
+ = flags :ar, :gb, :selected => :gb
126
135
  ```
127
136
 
128
137
  Alternatively
@@ -139,11 +148,13 @@ Or using the #flag_code helper
139
148
  = flag(:ar) + flag(:gb, :selected => flag_code(I18n.locale)
140
149
  ```
141
150
 
151
+ You can also include the :with_semi => true option in order to have flags not selected displayed with the 'semi' class (semi-bright image)
152
+
142
153
  For use with tooltips or similar js plguins, it can be useful to set the <li> title attribute:
143
154
 
144
155
  ```haml
145
156
  = flag_list 32 do
146
- = flags [:ar, :br, :gb], :title => true
157
+ = flags :ar, :br, :gb, :title => true
147
158
  ```
148
159
 
149
160
  The flag_title will render the following list item:
@@ -163,16 +174,22 @@ The :title and :content can also be set to a string which is then displayed
163
174
  To also get content rendered for the <li>
164
175
 
165
176
  ```haml
166
- = flags [:ar, :br, :gb], :content => true
177
+ = flags :ar, :br, :gb, :content => true
167
178
  ```
168
179
 
169
180
  Note: There is also a #flag_selected? helper, which is (and/or can be) used to determine if the flag to be drawn should have the "selected" class set)
181
+
182
+ ## Automatic flag selection
183
+
170
184
  The auto-select feature is by default turned off, but can be turned on/off using:
171
185
 
172
186
  ```ruby
173
187
  WorldFlags.auto_select = true # or WorldFlags.auto_select!
174
188
  ```
175
189
 
190
+ With this feature turned on, WorlfFlags will attempt to set the `selected` CSS class on whatever flag drawn that matches the current `I18n.locale`code.
191
+ If this doesn't work out for you, try to add a WorldFlags flag-to-locale code mapping for the locale.
192
+
176
193
  ## Using localization
177
194
 
178
195
  You can specify whether to look up labels for the flags for either language or country and for which locale to look up the labels (see Configuring localization)
@@ -181,14 +198,14 @@ Use danish (da) country labels
181
198
 
182
199
  ```haml
183
200
  = flag_list 32 do
184
- = flags [:ar, :br, :gb], :country => :da
201
+ = flags :ar, :br, :gb, :country => :da
185
202
  ```
186
203
 
187
204
  Use danish (da) language labels
188
205
 
189
206
  ```haml
190
207
  = flag_list 32 do
191
- = flags [:ar, :br, :gb], :language => I18n.locale
208
+ = flags :ar, :br, :gb, :language => I18n.locale
192
209
  ```
193
210
 
194
211
  Note: In the config folder there is now a json file with all the english ISO-3166-2 code translations ready for use. You can make similar locale files for other locales/languages.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.3.0
@@ -0,0 +1,14 @@
1
+ module WorldFlags
2
+ module Generators
3
+ class InitGenerator < ::Rails::Generators::Base
4
+ desc "Creates world flags initializer"
5
+
6
+ source_root File.dirname(__FILE__) + '/templates'
7
+
8
+ def main_flow
9
+ template "world_flags.erb", "config/initializers/world_flags.rb"
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,12 @@
1
+ WorldFlags.auto_select! # = false to disable
2
+
3
+ # cc_en = 'app/config/country_codes/iso-3166-2.en.json'
4
+ # cc_path = File.join(Rails.root, cc_en)
5
+ # country_codes_en = JSON.parse File.read(cc_path)
6
+
7
+ # WorldFlags.languages = some_language_hash # fx loaded from a yaml file
8
+ # WorldFlags.countries = some_country_hash # fx loaded from a yaml file
9
+
10
+ # WorldFlags.active_locales = [:en, :da]
11
+
12
+ # WorldFlags.locale_flag_map = some hash
data/lib/world-flags.rb CHANGED
@@ -10,6 +10,14 @@ module WorldFlags
10
10
  class << self
11
11
  attr_accessor :auto_select
12
12
 
13
+ # TODO: Why both active and valid locales? Does this even make sense!?
14
+ attr_writer :active_locales
15
+
16
+ def active_locales
17
+ @active_locales ||= I18n.available_locales unless I18n.available_locales.blank?
18
+ @active_locales ||= [:en]
19
+ end
20
+
13
21
  # for WorldFlags::Helper::Locale
14
22
  def valid_locales
15
23
  @valid_locales ||= ['en', 'de', 'es', 'ru']
@@ -45,11 +53,7 @@ module WorldFlags
45
53
  flag_locale_map[code.to_sym] || code
46
54
  end
47
55
 
48
- attr_writer :locale_flag_map, :active_locales
49
-
50
- def active_locales
51
- @active_locales ||= [:en]
52
- end
56
+ attr_writer :locale_flag_map
53
57
 
54
58
  # translate locales to flag code: ISO_3166-1_alpha-2
55
59
  def locale_flag_map
@@ -1,3 +1,5 @@
1
+ require 'world_flags/helper/view/util'
2
+
1
3
  module WorldFlags
2
4
  module Helper
3
5
  module View
@@ -5,12 +7,6 @@ module WorldFlags
5
7
  [16, 32, 64]
6
8
  end
7
9
 
8
- # define more mappings by setting WorldFlags.locale_flag_map to a Hash map
9
- # http://en.wikipedia.org/wiki/ISO_639-1_language_matrix
10
- def flag_code code
11
- WorldFlags.flag_code code
12
- end
13
-
14
10
  def flags_list size = 16, &block
15
11
  raise "Missing block" unless block_given?
16
12
  unless WorldFlags::Helper::View.flag_sizes.include?(size.to_i)
@@ -24,57 +20,31 @@ module WorldFlags
24
20
  # http://en.wikipedia.org/wiki/ISO_639-1_language_matrix
25
21
 
26
22
  # should look up translation for each code
27
- def flags flags_arr, options = {}
28
- flags_arr.inject("") do |res, elem|
23
+ def flags *args
24
+ options = args.extract_options!
25
+ args.flatten!
26
+ args.inject("") do |res, elem|
29
27
  case elem
30
- when Array
31
- code = elem.first
32
- name = elem.last
33
28
  when String, Symbol
34
29
  code = elem
35
30
  name = WorldFlags.label(code, options)
36
31
  else
37
- raise ArgumentError, "Bad argument: #{flags_arr}, must be Hash or Array"
32
+ raise ArgumentError, "Bad argument: #{args}, must be Array"
38
33
  end
39
34
  res << flag(code, name, options)
40
35
  end.html_safe
41
36
  end
42
37
 
43
- def flags_title flags_arr, options = {}
44
- flags flags_arr, options.merge(:title => true)
38
+ def flags_title *args
39
+ options = args.extract_options!
40
+ flags args, options.merge(:title => true)
45
41
  end
46
42
 
47
43
  def flag code, name, options = {}
48
- label = case options[:content]
49
- when true
50
- name
51
- when String
52
- options[:content]
53
- else
54
- '&nbsp;'
55
- end
56
-
57
- title = case options[:title]
58
- when true
59
- name
60
- when String
61
- options[:title]
62
- else
63
- nil
64
- end
65
- locale = WorldFlags.locale(code)
66
- extra_options = title ? {:title => title } : {}
67
- selected = flag_selected?(code, options) ? 'selected' : ''
68
- content_tag :li, label.html_safe, {:class => "flag #{code} #{selected}", :'data-country' => name, :'data-cc' => code, :'data-locale' => locale}.merge(options[:html] || {}).merge(extra_options)
69
- end
70
-
71
- def flag_selected? code, options = {}
72
- code = code.to_sym
73
- sel = options[:selected] || options[code]
74
-
75
- auto_sel = flag_code(I18n.locale).to_sym if WorldFlags.auto_select?
44
+ label = WorldFlags::Helper::View::Util.label_for options
45
+ title = WorldFlags::Helper::View::Util.title_for name, options
76
46
 
77
- selected ||= [sel, auto_sel].any?{|e| e == code }
47
+ content_tag :li, label.html_safe, WorldFlags::Helper::View::Util.flag_options(code, title, name, options)
78
48
  end
79
49
 
80
50
  def flag_title code, name, options = {}
@@ -0,0 +1,56 @@
1
+ module WorldFlags
2
+ module Helper
3
+ module View
4
+ module Util
5
+ def self.label_for options = {}
6
+ label = case options[:content]
7
+ when true
8
+ name
9
+ when String
10
+ options[:content]
11
+ else
12
+ '&nbsp;'
13
+ end
14
+ end
15
+
16
+ def self.title_for name, options = {}
17
+ case options[:title]
18
+ when true
19
+ name
20
+ when String
21
+ options[:title]
22
+ else
23
+ nil
24
+ end
25
+ end
26
+
27
+ def self.flag_options code, title, name, options = {}
28
+ locale = WorldFlags.locale(code)
29
+ extra_options = title ? {:title => title } : {}
30
+ selected = flag_selected?(code, options) ? ' selected' : ''
31
+
32
+ # add semi class if not selected
33
+ semi = (selected.blank? ? ' semi' : '') if options[:with_semi]
34
+
35
+ {:class => "flag #{code}#{selected}#{semi}", :'data-country' => name, :'data-cc' => code, :'data-locale' => locale}.merge(options[:html] || {}).merge(extra_options)
36
+ end
37
+
38
+ def self.flag_selected? code, options = {}
39
+ code = code.to_sym
40
+ sel = options[:selected] || options[code]
41
+
42
+ auto_sel = flag_code(I18n.locale).to_sym if WorldFlags.auto_select?
43
+ match_locales = [sel, auto_sel]
44
+
45
+ selected ||= match_locales.any?{|e| e == code }
46
+ end
47
+
48
+ # define more mappings by setting WorldFlags.locale_flag_map to a Hash map
49
+ # http://en.wikipedia.org/wiki/ISO_639-1_language_matrix
50
+ def self.flag_code code
51
+ WorldFlags.flag_code code
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -4,6 +4,11 @@ describe WorldFlags::Helper::View do
4
4
  include ControllerTestHelpers,
5
5
  WorldFlags::Helper::View
6
6
 
7
+ before do
8
+ WorldFlags.auto_select!
9
+ I18n.locale = 'ar'
10
+ end
11
+
7
12
  it "should be empty, with an empty block" do
8
13
  output = flags_list do
9
14
  end
@@ -27,37 +32,30 @@ describe WorldFlags::Helper::View do
27
32
  output.should == "<ul class=\"f32\"></ul>"
28
33
  end
29
34
 
30
- it "should list flags using Hash arg" do
31
- output = flags_list 32 do
32
- flags :ar => 'Argentina', :gb => 'England', :dk => 'Denmark'
33
- end
34
- output.should == "<ul class=\"f32\"><li class=\"flag ar \" data-cc=\"ar\" data-country=\"Argentina\" data-locale=\"ar\">&nbsp;</li><li class=\"flag gb \" data-cc=\"gb\" data-country=\"England\" data-locale=\"en_UK\">&nbsp;</li><li class=\"flag dk \" data-cc=\"dk\" data-country=\"Denmark\" data-locale=\"da\">&nbsp;</li></ul>"
35
- end
36
-
37
35
  it "should list flags using Array" do
38
36
  output = flags_list 32 do
39
37
  flags [:ar, :gb]
40
38
  end
41
- output.should == "<ul class=\"f32\"><li class=\"flag ar \" data-cc=\"ar\" data-country=\"Argentinian Spanish\" data-locale=\"ar\">&nbsp;</li><li class=\"flag gb \" data-cc=\"gb\" data-country=\"British English\" data-locale=\"en_UK\">&nbsp;</li></ul>"
39
+ output.should == "<ul class=\"f32\"><li class=\"flag ar selected\" data-cc=\"ar\" data-country=\"Argentinian Spanish\" data-locale=\"ar\">&nbsp;</li><li class=\"flag gb\" data-cc=\"gb\" data-country=\"British English\" data-locale=\"en_UK\">&nbsp;</li></ul>"
42
40
  end
43
41
 
44
- it "should list flags" do
45
- output = flag_title :ar, 'Argentina'
46
- output.should == "<li class=\"flag ar \" data-cc=\"ar\" data-country=\"Argentina\" data-locale=\"ar\" title=\"Argentina\">&nbsp;</li>"
47
- end
48
-
49
- it "should list flags" do
42
+ it "should list flags using args" do
50
43
  output = flags_list 32 do
51
- flags_title :ar => 'Argentina'
44
+ flags :ar, :gb
52
45
  end
53
- output.should == "<ul class=\"f32\"><li class=\"flag ar \" data-cc=\"ar\" data-country=\"Argentina\" data-locale=\"ar\" title=\"Argentina\">&nbsp;</li></ul>"
46
+ output.should == "<ul class=\"f32\"><li class=\"flag ar selected\" data-cc=\"ar\" data-country=\"Argentinian Spanish\" data-locale=\"ar\">&nbsp;</li><li class=\"flag gb\" data-cc=\"gb\" data-country=\"British English\" data-locale=\"en_UK\">&nbsp;</li></ul>"
54
47
  end
55
48
 
56
- it "should list flags combined" do
49
+ it "should list flags using args and :with_semi" do
57
50
  output = flags_list 32 do
58
- [flags(:ar => 'Argentina', :gb => 'England'), flag(:br, 'Brazil')].join.html_safe
51
+ flags :ar, :gb, :with_semi => true
59
52
  end
60
- output.should == "<ul class=\"f32\"><li class=\"flag ar \" data-cc=\"ar\" data-country=\"Argentina\" data-locale=\"ar\">&nbsp;</li><li class=\"flag gb \" data-cc=\"gb\" data-country=\"England\" data-locale=\"en_UK\">&nbsp;</li><li class=\"flag br \" data-cc=\"br\" data-country=\"Brazil\" data-locale=\"br\">&nbsp;</li></ul>"
53
+ output.should == "<ul class=\"f32\"><li class=\"flag ar selected\" data-cc=\"ar\" data-country=\"Argentinian Spanish\" data-locale=\"ar\">&nbsp;</li><li class=\"flag gb semi\" data-cc=\"gb\" data-country=\"British English\" data-locale=\"en_UK\">&nbsp;</li></ul>"
54
+ end
55
+
56
+ it "should list flags" do
57
+ output = flag_title :ar, 'Argentina'
58
+ output.should == "<li class=\"flag ar selected\" data-cc=\"ar\" data-country=\"Argentina\" data-locale=\"ar\" title=\"Argentina\">&nbsp;</li>"
61
59
  end
62
60
  end
63
61
 
data/world-flags.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "world-flags"
8
- s.version = "0.2.7"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-04-26"
12
+ s.date = "2012-04-27"
13
13
  s.description = "Use world flag icons in your Rails app"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,8 @@ Gem::Specification.new do |s|
27
27
  "Rakefile",
28
28
  "VERSION",
29
29
  "app/config/country_codes/iso-3166-2.en.json",
30
+ "lib/generators/world_flags/init_generator.rb",
31
+ "lib/generators/world_flags/templates/world_flags.erb",
30
32
  "lib/world-flags.rb",
31
33
  "lib/world_flags/core_ext.rb",
32
34
  "lib/world_flags/countries.rb",
@@ -35,6 +37,7 @@ Gem::Specification.new do |s|
35
37
  "lib/world_flags/helper/geo.rb",
36
38
  "lib/world_flags/helper/locale.rb",
37
39
  "lib/world_flags/helper/view.rb",
40
+ "lib/world_flags/helper/view/util.rb",
38
41
  "lib/world_flags/helpers.rb",
39
42
  "lib/world_flags/languages.rb",
40
43
  "lib/world_flags/rails/engine.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: world-flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-26 00:00:00.000000000 Z
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -109,6 +109,8 @@ files:
109
109
  - Rakefile
110
110
  - VERSION
111
111
  - app/config/country_codes/iso-3166-2.en.json
112
+ - lib/generators/world_flags/init_generator.rb
113
+ - lib/generators/world_flags/templates/world_flags.erb
112
114
  - lib/world-flags.rb
113
115
  - lib/world_flags/core_ext.rb
114
116
  - lib/world_flags/countries.rb
@@ -117,6 +119,7 @@ files:
117
119
  - lib/world_flags/helper/geo.rb
118
120
  - lib/world_flags/helper/locale.rb
119
121
  - lib/world_flags/helper/view.rb
122
+ - lib/world_flags/helper/view/util.rb
120
123
  - lib/world_flags/helpers.rb
121
124
  - lib/world_flags/languages.rb
122
125
  - lib/world_flags/rails/engine.rb
@@ -162,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
165
  version: '0'
163
166
  segments:
164
167
  - 0
165
- hash: -4369520111429852681
168
+ hash: -1830379886448737503
166
169
  required_rubygems_version: !ruby/object:Gem::Requirement
167
170
  none: false
168
171
  requirements: