world-flags 0.2.4 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -2
- data/README.md +43 -9
- data/VERSION +1 -1
- data/lib/world-flags.rb +13 -10
- data/lib/world_flags/core_ext.rb +22 -0
- data/lib/world_flags/helper/all.rb +11 -0
- data/lib/world_flags/helper/browser.rb +22 -0
- data/lib/world_flags/helper/geo.rb +19 -0
- data/lib/world_flags/helper/locale.rb +21 -0
- data/lib/world_flags/helper/view.rb +89 -0
- data/lib/world_flags/helpers.rb +8 -0
- data/lib/world_flags/rails/engine.rb +1 -1
- data/spec/world_flags/view_helper_spec.rb +2 -2
- data/vendor/assets/javascripts/world_flags/url_helper.js +48 -0
- data/world-flags.gemspec +10 -7
- metadata +11 -21
- data/lib/world_flags/locale_helper.rb +0 -77
- data/lib/world_flags/view_helper.rb +0 -81
data/Gemfile
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
source :rubygems
|
2
2
|
|
3
|
-
gem 'httparty'
|
4
|
-
|
5
3
|
# Add dependencies to develop your gem here.
|
6
4
|
# Include everything needed to run rake, tests, features, etc.
|
7
5
|
group :development do
|
@@ -13,5 +11,6 @@ group :development do
|
|
13
11
|
end
|
14
12
|
|
15
13
|
group :test do
|
14
|
+
gem 'httparty'
|
16
15
|
gem 'rails', '>= 3.1'
|
17
16
|
end
|
data/README.md
CHANGED
@@ -204,7 +204,7 @@ A small helper module is provided that can be inserted into a Controller or wher
|
|
204
204
|
class MainController < ApplicationController
|
205
205
|
def home
|
206
206
|
@json = Property.all.to_gmaps4rails
|
207
|
-
@country_code = WorldFlags::Geo.ip_country_code
|
207
|
+
@country_code = WorldFlags::Helper::Geo.ip_country_code
|
208
208
|
end
|
209
209
|
end
|
210
210
|
```
|
@@ -213,8 +213,8 @@ Alternatively you can include the modules directly into the controller:
|
|
213
213
|
|
214
214
|
```ruby
|
215
215
|
class MainController < ApplicationController
|
216
|
-
include WorldFlags::Geo
|
217
|
-
include WorldFlags::Browser
|
216
|
+
include WorldFlags::Helper::Geo
|
217
|
+
include WorldFlags::Helper::Browser
|
218
218
|
|
219
219
|
def home
|
220
220
|
@json = Property.all.to_gmaps4rails
|
@@ -224,17 +224,20 @@ class MainController < ApplicationController
|
|
224
224
|
end
|
225
225
|
```
|
226
226
|
|
227
|
-
If you include the `WorldFlags::Locale` module, you can simply do:
|
227
|
+
If you include the `WorldFlags::Helper::Locale` module, you can simply do:
|
228
228
|
|
229
229
|
```ruby
|
230
230
|
before_filter :set_locale
|
231
231
|
```
|
232
232
|
|
233
233
|
And it should set the I18n.locale appropriately, trying `params[locale], browser, ip address` in succession, defaulting to `I18n.default_locale`.
|
234
|
-
For each locale it will check if it is a vaild locale, using
|
235
|
-
`WorldFlags::Locale#valid_locales`
|
236
234
|
|
237
|
-
For
|
235
|
+
For each locale it will check if it is a valid locale. By default it will call `valid_locales` in the controller, which will first try `I18n.available_locales` and then fall-back to `WorldFlags#valid_locales`.
|
236
|
+
You can override this behavior by defining you custom `valid_locales` method in the controller.
|
237
|
+
|
238
|
+
For convenience you can include `WorldFlags::Helper::All` to include all these helper modules.
|
239
|
+
|
240
|
+
Note that if you include the `WorldFlags::Helper::Geo you need the `httparty` gem as well.
|
238
241
|
|
239
242
|
Example:
|
240
243
|
|
@@ -246,13 +249,25 @@ class MainController < ApplicationController
|
|
246
249
|
end
|
247
250
|
```
|
248
251
|
|
249
|
-
You
|
252
|
+
You can configure valid locales for use with WorldFlags in an initializer, fx `initializers/locales.rb` :
|
250
253
|
|
251
254
|
```ruby
|
252
255
|
# fx [:da, :en] or even ['da', 'en']
|
253
|
-
WorldFlags
|
256
|
+
WorldFlags.valid_locales = my_valid_locales_list
|
254
257
|
```
|
255
258
|
|
259
|
+
Note that if not set, this list is preconfigured to: `['en', 'de', 'es', 'ru']`
|
260
|
+
|
261
|
+
Alternatively configure in your `application.rb` file:
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
class Application < Rails::Application
|
265
|
+
# ...
|
266
|
+
config.i18n.available_locales = [:da, :sv, :no]
|
267
|
+
``
|
268
|
+
|
269
|
+
Note: This approach in turn works well with the `i18n-docs` gem ;)
|
270
|
+
|
256
271
|
## Post flag selection
|
257
272
|
|
258
273
|
Here an example of setting up a flag click handler in order to call the server with the country/locale/language selection of the flag.
|
@@ -264,6 +279,8 @@ $("li.flag").click(function() {
|
|
264
279
|
// full page reload
|
265
280
|
// window.location.href = "/locale/select/" + country;
|
266
281
|
|
282
|
+
// window.location.href = window.location.href + '?locale=' + country;
|
283
|
+
|
267
284
|
// async post
|
268
285
|
$.post("/locale/select", { "country": country }, function(data) {
|
269
286
|
console.log(data);
|
@@ -271,6 +288,23 @@ $("li.flag").click(function() {
|
|
271
288
|
});
|
272
289
|
```
|
273
290
|
|
291
|
+
This gem now comes with a simple javascript object `WorldFlagsUrlHelper` baked in. To use it, add the following to your ``application.js manifest.
|
292
|
+
|
293
|
+
```
|
294
|
+
//= require world_flags/url_helper
|
295
|
+
```
|
296
|
+
|
297
|
+
And use it something like this:
|
298
|
+
|
299
|
+
```javascript
|
300
|
+
$("li.flag").click(function() {
|
301
|
+
country = $(this).data('locale');
|
302
|
+
|
303
|
+
// full page reload with locale=xx param added to url :)
|
304
|
+
WorldFlagsUrlHelper.reloadWithLocaleParam('da');
|
305
|
+
});
|
306
|
+
```
|
307
|
+
|
274
308
|
## Enjoy
|
275
309
|
|
276
310
|
Copyright (2012) Kristian Mandrup
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.7
|
data/lib/world-flags.rb
CHANGED
@@ -1,22 +1,25 @@
|
|
1
|
-
require "world_flags/
|
2
|
-
require "world_flags/
|
1
|
+
require "world_flags/core_ext"
|
2
|
+
require "world_flags/helpers"
|
3
|
+
|
3
4
|
require 'world_flags/rails/engine' if defined?(::Rails::Engine)
|
4
5
|
|
5
6
|
require "world_flags/languages"
|
6
7
|
require "world_flags/countries"
|
7
8
|
|
8
|
-
class Hash
|
9
|
-
def hash_revert
|
10
|
-
r = Hash.new
|
11
|
-
each {|k,v| r[v] = k}
|
12
|
-
r
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
9
|
module WorldFlags
|
17
10
|
class << self
|
18
11
|
attr_accessor :auto_select
|
19
12
|
|
13
|
+
# for WorldFlags::Helper::Locale
|
14
|
+
def valid_locales
|
15
|
+
@valid_locales ||= ['en', 'de', 'es', 'ru']
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid_locales= *list
|
19
|
+
raise ArgumentError, "Must be a list of locales, was #{list}" if list.empty?
|
20
|
+
@valid_locales ||= list.flatten
|
21
|
+
end
|
22
|
+
|
20
23
|
def auto_select?
|
21
24
|
auto_select
|
22
25
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Hash
|
2
|
+
def hash_revert
|
3
|
+
r = Hash.new
|
4
|
+
each {|k,v| r[v] = k}
|
5
|
+
r
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Array
|
10
|
+
def downcase
|
11
|
+
self.map{|e| e.to_s.downcase}
|
12
|
+
end
|
13
|
+
|
14
|
+
def upcase
|
15
|
+
self.map{|e| e.to_s.upcase}
|
16
|
+
end
|
17
|
+
|
18
|
+
def select_first_in *list
|
19
|
+
list = list.flatten.compact
|
20
|
+
self.each {|e| return e if list.include?(e) }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module WorldFlags
|
2
|
+
module Helper
|
3
|
+
module Browser
|
4
|
+
def self.browser_locale request
|
5
|
+
return @browser_locale if @browser_locale
|
6
|
+
if lang = request.env["HTTP_ACCEPT_LANGUAGE"]
|
7
|
+
lang = lang.split(",").map { |l|
|
8
|
+
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
9
|
+
l.split(';q=')
|
10
|
+
}.first
|
11
|
+
@browser_locale = lang.first.split("-").first
|
12
|
+
else
|
13
|
+
@browser_locale = I18n.default_locale
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def browser_locale
|
18
|
+
WorldFlags::Helper::Browser.browser_locale(request)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module WorldFlags
|
2
|
+
module Helper
|
3
|
+
module Geo
|
4
|
+
def self.ip_json
|
5
|
+
require "httparty"
|
6
|
+
|
7
|
+
HTTParty.get('http://freegeoip.net/json/')
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.ip_country_code
|
11
|
+
@ip_country_code ||= ip_json.parsed_response['country_code']
|
12
|
+
end
|
13
|
+
|
14
|
+
def ip_country_code
|
15
|
+
WorldFlags::Helper::Geo.ip_country_code
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module WorldFlags
|
2
|
+
module Helper
|
3
|
+
module Locale
|
4
|
+
def set_locale
|
5
|
+
I18n.locale = locales.select_first_in(valid_locales.downcase)
|
6
|
+
end
|
7
|
+
|
8
|
+
def valid_locales
|
9
|
+
if I18n.respond_to?(:available_locales) && I18n.available_locales.present?
|
10
|
+
I18n.available_locales
|
11
|
+
else
|
12
|
+
WorldFlags.valid_locales
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def locales
|
17
|
+
[params[:locale], browser_locale, ip_country_code, I18n.default_locale].downcase
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module WorldFlags
|
2
|
+
module Helper
|
3
|
+
module View
|
4
|
+
def self.flag_sizes
|
5
|
+
[16, 32, 64]
|
6
|
+
end
|
7
|
+
|
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
|
+
def flags_list size = 16, &block
|
15
|
+
raise "Missing block" unless block_given?
|
16
|
+
unless WorldFlags::Helper::View.flag_sizes.include?(size.to_i)
|
17
|
+
raise "Supported sizes are only #{WorldFlags::Helper::View.flag_sizes}"
|
18
|
+
end
|
19
|
+
content = capture(&block)
|
20
|
+
content_tag :ul, content, :class => "f#{size}"
|
21
|
+
end
|
22
|
+
alias_method :flag_list, :flags_list
|
23
|
+
|
24
|
+
# http://en.wikipedia.org/wiki/ISO_639-1_language_matrix
|
25
|
+
|
26
|
+
# should look up translation for each code
|
27
|
+
def flags flags_arr, options = {}
|
28
|
+
flags_arr.inject("") do |res, elem|
|
29
|
+
case elem
|
30
|
+
when Array
|
31
|
+
code = elem.first
|
32
|
+
name = elem.last
|
33
|
+
when String, Symbol
|
34
|
+
code = elem
|
35
|
+
name = WorldFlags.label(code, options)
|
36
|
+
else
|
37
|
+
raise ArgumentError, "Bad argument: #{flags_arr}, must be Hash or Array"
|
38
|
+
end
|
39
|
+
res << flag(code, name, options)
|
40
|
+
end.html_safe
|
41
|
+
end
|
42
|
+
|
43
|
+
def flags_title flags_arr, options = {}
|
44
|
+
flags flags_arr, options.merge(:title => true)
|
45
|
+
end
|
46
|
+
|
47
|
+
def flag code, name, options = {}
|
48
|
+
label = case options[:content]
|
49
|
+
when true
|
50
|
+
name
|
51
|
+
when String
|
52
|
+
options[:content]
|
53
|
+
else
|
54
|
+
' '
|
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?
|
76
|
+
|
77
|
+
selected ||= [sel, auto_sel].any?{|e| e == code }
|
78
|
+
end
|
79
|
+
|
80
|
+
def flag_title code, name, options = {}
|
81
|
+
flag code, name, options.merge(:title => true)
|
82
|
+
end
|
83
|
+
|
84
|
+
def use_flags size = 16
|
85
|
+
stylesheet_link_tag "flags/flags#{size}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
// Usage:
|
2
|
+
// WorldFlagsUrlHelper.reloadWithLocaleParam('da');
|
3
|
+
|
4
|
+
var WorldFlagsUrlHelper = {
|
5
|
+
reloadWithLocaleParam: function(locale) {
|
6
|
+
|
7
|
+
// will reload page with 'locale' param added for locale
|
8
|
+
// fx www.example.com?locale=da
|
9
|
+
// or www.example.com?status=ok&locale=da
|
10
|
+
window.location.href = this.addLocaleParameter(window.location.href, locale);
|
11
|
+
},
|
12
|
+
|
13
|
+
addLocaleParameter: function(url, locale) {
|
14
|
+
//return this.addParameterToURL(url, 'locale', locale);
|
15
|
+
return this.addUrlParameter(url, 'locale', locale, true);
|
16
|
+
},
|
17
|
+
|
18
|
+
// From http://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript
|
19
|
+
addUrlParameter: function (sourceUrl, parameterName, parameterValue, replaceDuplicates) {
|
20
|
+
if ((sourceUrl == null) || (sourceUrl.length == 0)) sourceUrl = document.location.href;
|
21
|
+
var urlParts = sourceUrl.split("?");
|
22
|
+
var newQueryString = "";
|
23
|
+
if (urlParts.length > 1)
|
24
|
+
{
|
25
|
+
var parameters = urlParts[1].split("&");
|
26
|
+
for (var i=0; (i < parameters.length); i++)
|
27
|
+
{
|
28
|
+
var parameterParts = parameters[i].split("=");
|
29
|
+
if (!(replaceDuplicates && parameterParts[0] == parameterName))
|
30
|
+
{
|
31
|
+
if (newQueryString == "")
|
32
|
+
newQueryString = "?";
|
33
|
+
else
|
34
|
+
newQueryString += "&";
|
35
|
+
newQueryString += parameterParts[0] + "=" + parameterParts[1];
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
if (newQueryString == "")
|
40
|
+
newQueryString = "?";
|
41
|
+
else
|
42
|
+
newQueryString += "&";
|
43
|
+
|
44
|
+
newQueryString += parameterName + "=" + parameterValue;
|
45
|
+
|
46
|
+
return urlParts[0] + newQueryString;
|
47
|
+
}
|
48
|
+
}
|
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.
|
8
|
+
s.version = "0.2.7"
|
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-
|
12
|
+
s.date = "2012-04-26"
|
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 = [
|
@@ -28,11 +28,16 @@ Gem::Specification.new do |s|
|
|
28
28
|
"VERSION",
|
29
29
|
"app/config/country_codes/iso-3166-2.en.json",
|
30
30
|
"lib/world-flags.rb",
|
31
|
+
"lib/world_flags/core_ext.rb",
|
31
32
|
"lib/world_flags/countries.rb",
|
33
|
+
"lib/world_flags/helper/all.rb",
|
34
|
+
"lib/world_flags/helper/browser.rb",
|
35
|
+
"lib/world_flags/helper/geo.rb",
|
36
|
+
"lib/world_flags/helper/locale.rb",
|
37
|
+
"lib/world_flags/helper/view.rb",
|
38
|
+
"lib/world_flags/helpers.rb",
|
32
39
|
"lib/world_flags/languages.rb",
|
33
|
-
"lib/world_flags/locale_helper.rb",
|
34
40
|
"lib/world_flags/rails/engine.rb",
|
35
|
-
"lib/world_flags/view_helper.rb",
|
36
41
|
"sandbox/country_codes_table.html",
|
37
42
|
"sandbox/extract_codes.rb",
|
38
43
|
"spec/spec_helper.rb",
|
@@ -46,6 +51,7 @@ Gem::Specification.new do |s|
|
|
46
51
|
"vendor/assets/images/flags/flags32_semi.png",
|
47
52
|
"vendor/assets/images/flags/flags64.png",
|
48
53
|
"vendor/assets/images/flags/flags64_semi.png",
|
54
|
+
"vendor/assets/javascripts/world_flags/url_helper.js",
|
49
55
|
"vendor/assets/stylesheets/flags/basic.css",
|
50
56
|
"vendor/assets/stylesheets/flags/basic.scss.css",
|
51
57
|
"vendor/assets/stylesheets/flags/flags16-semi.css.erb",
|
@@ -70,14 +76,12 @@ Gem::Specification.new do |s|
|
|
70
76
|
s.specification_version = 3
|
71
77
|
|
72
78
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
73
|
-
s.add_runtime_dependency(%q<httparty>, [">= 0"])
|
74
79
|
s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
|
75
80
|
s.add_development_dependency(%q<rdoc>, [">= 3.12"])
|
76
81
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
77
82
|
s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
|
78
83
|
s.add_development_dependency(%q<simplecov>, [">= 0.5"])
|
79
84
|
else
|
80
|
-
s.add_dependency(%q<httparty>, [">= 0"])
|
81
85
|
s.add_dependency(%q<rspec>, [">= 2.8.0"])
|
82
86
|
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
83
87
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
@@ -85,7 +89,6 @@ Gem::Specification.new do |s|
|
|
85
89
|
s.add_dependency(%q<simplecov>, [">= 0.5"])
|
86
90
|
end
|
87
91
|
else
|
88
|
-
s.add_dependency(%q<httparty>, [">= 0"])
|
89
92
|
s.add_dependency(%q<rspec>, [">= 2.8.0"])
|
90
93
|
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
91
94
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
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.
|
4
|
+
version: 0.2.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: httparty
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
name: rspec
|
32
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,11 +110,16 @@ files:
|
|
126
110
|
- VERSION
|
127
111
|
- app/config/country_codes/iso-3166-2.en.json
|
128
112
|
- lib/world-flags.rb
|
113
|
+
- lib/world_flags/core_ext.rb
|
129
114
|
- lib/world_flags/countries.rb
|
115
|
+
- lib/world_flags/helper/all.rb
|
116
|
+
- lib/world_flags/helper/browser.rb
|
117
|
+
- lib/world_flags/helper/geo.rb
|
118
|
+
- lib/world_flags/helper/locale.rb
|
119
|
+
- lib/world_flags/helper/view.rb
|
120
|
+
- lib/world_flags/helpers.rb
|
130
121
|
- lib/world_flags/languages.rb
|
131
|
-
- lib/world_flags/locale_helper.rb
|
132
122
|
- lib/world_flags/rails/engine.rb
|
133
|
-
- lib/world_flags/view_helper.rb
|
134
123
|
- sandbox/country_codes_table.html
|
135
124
|
- sandbox/extract_codes.rb
|
136
125
|
- spec/spec_helper.rb
|
@@ -144,6 +133,7 @@ files:
|
|
144
133
|
- vendor/assets/images/flags/flags32_semi.png
|
145
134
|
- vendor/assets/images/flags/flags64.png
|
146
135
|
- vendor/assets/images/flags/flags64_semi.png
|
136
|
+
- vendor/assets/javascripts/world_flags/url_helper.js
|
147
137
|
- vendor/assets/stylesheets/flags/basic.css
|
148
138
|
- vendor/assets/stylesheets/flags/basic.scss.css
|
149
139
|
- vendor/assets/stylesheets/flags/flags16-semi.css.erb
|
@@ -172,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
172
162
|
version: '0'
|
173
163
|
segments:
|
174
164
|
- 0
|
175
|
-
hash:
|
165
|
+
hash: -4369520111429852681
|
176
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
167
|
none: false
|
178
168
|
requirements:
|
@@ -1,77 +0,0 @@
|
|
1
|
-
require "httparty"
|
2
|
-
|
3
|
-
class Array
|
4
|
-
def downcase
|
5
|
-
self.map{|e| e.to_s.downcase}
|
6
|
-
end
|
7
|
-
|
8
|
-
def upcase
|
9
|
-
self.map{|e| e.to_s.upcase}
|
10
|
-
end
|
11
|
-
|
12
|
-
def select_first_in *list
|
13
|
-
list = list.flatten.compact
|
14
|
-
self.each {|e| return e if list.include?(e) }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
module WorldFlags
|
19
|
-
module Geo
|
20
|
-
def self.ip_json
|
21
|
-
HTTParty.get('http://freegeoip.net/json/')
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.ip_country_code
|
25
|
-
@ip_country_code ||= ip_json.parsed_response['country_code']
|
26
|
-
end
|
27
|
-
|
28
|
-
def ip_country_code
|
29
|
-
WorldFlags::Geo.ip_country_code
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module Locale
|
34
|
-
def set_locale
|
35
|
-
I18n.locale = locales.select_first_in(valid_locales.downcase)
|
36
|
-
end
|
37
|
-
|
38
|
-
class << self
|
39
|
-
attr_writer :valid_locales
|
40
|
-
|
41
|
-
def valid_locales
|
42
|
-
@valid_locales ||= ['en', 'de', 'es', 'ru']
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def locales
|
47
|
-
[params[:locale], browser_locale, ip_country_code, I18n.default_locale].downcase
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
module Browser
|
52
|
-
def self.browser_locale request
|
53
|
-
return @browser_locale if @browser_locale
|
54
|
-
if lang = request.env["HTTP_ACCEPT_LANGUAGE"]
|
55
|
-
lang = lang.split(",").map { |l|
|
56
|
-
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
57
|
-
l.split(';q=')
|
58
|
-
}.first
|
59
|
-
@browser_locale = lang.first.split("-").first
|
60
|
-
else
|
61
|
-
@browser_locale = I18n.default_locale
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def browser_locale
|
66
|
-
WorldFlags::Browser.browser_locale(request)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
module All
|
71
|
-
def self.included(base)
|
72
|
-
base.send :include, Geo
|
73
|
-
base.send :include, Browser
|
74
|
-
base.send :include, Locale
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
module WorldFlags
|
2
|
-
module ViewHelper
|
3
|
-
def self.flag_sizes
|
4
|
-
[16, 32, 64]
|
5
|
-
end
|
6
|
-
|
7
|
-
# define more mappings by setting WorldFlags.locale_flag_map to a Hash map
|
8
|
-
# http://en.wikipedia.org/wiki/ISO_639-1_language_matrix
|
9
|
-
def flag_code code
|
10
|
-
WorldFlags.flag_code code
|
11
|
-
end
|
12
|
-
|
13
|
-
def flags_list size = 16, &block
|
14
|
-
raise "Missing block" unless block_given?
|
15
|
-
raise "Supported sizes are only #{WorldFlags::ViewHelper.flag_sizes}" unless WorldFlags::ViewHelper.flag_sizes.include?(size.to_i)
|
16
|
-
content = capture(&block)
|
17
|
-
content_tag :ul, content, :class => "f#{size}"
|
18
|
-
end
|
19
|
-
alias_method :flag_list, :flags_list
|
20
|
-
|
21
|
-
# http://en.wikipedia.org/wiki/ISO_639-1_language_matrix
|
22
|
-
|
23
|
-
# should look up translation for each code
|
24
|
-
def flags flags_arr, options = {}
|
25
|
-
flags_arr.inject("") do |res, elem|
|
26
|
-
case elem
|
27
|
-
when Array
|
28
|
-
code = elem.first
|
29
|
-
name = elem.last
|
30
|
-
when String, Symbol
|
31
|
-
code = elem
|
32
|
-
name = WorldFlags.label(code, options)
|
33
|
-
else
|
34
|
-
raise ArgumentError, "Bad argument: #{flags_arr}, must be Hash or Array"
|
35
|
-
end
|
36
|
-
res << flag(code, name, options)
|
37
|
-
end.html_safe
|
38
|
-
end
|
39
|
-
|
40
|
-
def flags_title flags_arr, options = {}
|
41
|
-
flags flags_arr, options.merge(:title => true)
|
42
|
-
end
|
43
|
-
|
44
|
-
def flag code, name, options = {}
|
45
|
-
label = case options[:content]
|
46
|
-
when true
|
47
|
-
name
|
48
|
-
when String
|
49
|
-
options[:content]
|
50
|
-
else
|
51
|
-
' '
|
52
|
-
end
|
53
|
-
|
54
|
-
title = case options[:title]
|
55
|
-
when true
|
56
|
-
name
|
57
|
-
when String
|
58
|
-
options[:title]
|
59
|
-
else
|
60
|
-
nil
|
61
|
-
end
|
62
|
-
locale = WorldFlags.locale(code)
|
63
|
-
extra_options = title ? {:title => title } : {}
|
64
|
-
selected = flag_selected?(code, options) ? 'selected' : ''
|
65
|
-
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)
|
66
|
-
end
|
67
|
-
|
68
|
-
def flag_selected? code, options = {}
|
69
|
-
selected = options[:selected] || options[code.to_sym]
|
70
|
-
selected ||= (flag_code(I18n.locale.to_sym) == code.to_sym) if WorldFlags.auto_select?
|
71
|
-
end
|
72
|
-
|
73
|
-
def flag_title code, name, options = {}
|
74
|
-
flag code, name, options.merge(:title => true)
|
75
|
-
end
|
76
|
-
|
77
|
-
def use_flags size = 16
|
78
|
-
stylesheet_link_tag "flags/flags#{size}"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|