world-flags 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -242
- data/VERSION +1 -1
- data/lib/world-flags.rb +16 -1
- data/lib/world_flags/countries.rb +1 -1
- data/lib/world_flags/helper/locale.rb +26 -3
- data/lib/world_flags/languages.rb +1 -1
- data/lib/world_flags/util/config.rb +15 -0
- data/lib/world_flags/util/country.rb +19 -8
- data/lib/world_flags/util/language.rb +14 -3
- data/spec/world_flags/json/json_countries_spec.rb +1 -1
- data/spec/world_flags/json/json_languages_spec.rb +1 -1
- data/world-flags.gemspec +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -22,123 +22,6 @@ There is also support for semi-transparent flags. This can be used to fade certa
|
|
22
22
|
|
23
23
|
Simply add or remove the "semi" class for the flag to adjust the brightness level (fx for selection/mouse over).
|
24
24
|
|
25
|
-
```javascript
|
26
|
-
$("li.flag['data-cc'=dk).addClass('semi');
|
27
|
-
$("li.flag['data-cc'=dk).removeClass('semi');
|
28
|
-
```
|
29
|
-
|
30
|
-
CSS configuration in `application.css` manifest.
|
31
|
-
|
32
|
-
```css
|
33
|
-
*/
|
34
|
-
*= require_self
|
35
|
-
*= require_tree .
|
36
|
-
*= require flags/flags32
|
37
|
-
*= require flags/flags62
|
38
|
-
*/
|
39
|
-
```
|
40
|
-
|
41
|
-
## Alternative config
|
42
|
-
|
43
|
-
Note that the ruby examples uses HAML syntax
|
44
|
-
|
45
|
-
In the head of your view or layout file:
|
46
|
-
|
47
|
-
```haml
|
48
|
-
= stylesheet_link_tag 'flags16'
|
49
|
-
```
|
50
|
-
|
51
|
-
or using a helper
|
52
|
-
|
53
|
-
```haml
|
54
|
-
= use_flags(16)
|
55
|
-
```
|
56
|
-
|
57
|
-
Alternatively for the 32 width flags
|
58
|
-
|
59
|
-
```haml
|
60
|
-
= use_flags 32
|
61
|
-
```
|
62
|
-
|
63
|
-
## Configuring localization
|
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
|
-
|
67
|
-
You can setup WorldFlags to use a localization map for the labels of the flag icons
|
68
|
-
|
69
|
-
```ruby
|
70
|
-
WorldFlags.language_map = some_language_hash # fx loaded from a yaml file
|
71
|
-
WorldFlags.countries = some_country_hash # fx loaded from a yaml file
|
72
|
-
```
|
73
|
-
|
74
|
-
Notice that it is a locale code pointing to a map of *ISO_3166-1_alpha-2* codes to labels for that locale.
|
75
|
-
|
76
|
-
```ruby
|
77
|
-
{
|
78
|
-
:en => {:gb => 'English', :dk => 'Danish'}
|
79
|
-
:da => {:gb => 'Engelsk', :dk => 'Dansk'}
|
80
|
-
}
|
81
|
-
```
|
82
|
-
|
83
|
-
You can use [countries_and_languages](https://github.com/kristianmandrup/countries_and_languages) in order to generate the locale translation files needed for each locale.
|
84
|
-
|
85
|
-
## Locale mapping files
|
86
|
-
|
87
|
-
The engine/gem includes English translation mapping files for countries and languages. They are not complete, so please edit them if you find errors or find your favorite country or language missing or misplaced somehow.
|
88
|
-
|
89
|
-
### Country mappings
|
90
|
-
|
91
|
-
A country code to country name (in json format) can be found in `app/config/countries`.
|
92
|
-
|
93
|
-
```ruby
|
94
|
-
def countries locale = :en
|
95
|
-
path = File.join(Rails.root, "app/config/countries/locale_countries.#{locale}.json")
|
96
|
-
JSON.parse File.read(path)
|
97
|
-
end
|
98
|
-
|
99
|
-
WorldFlags.countries = Hashie::Mash.new countries
|
100
|
-
```
|
101
|
-
|
102
|
-
PS: See the json specs for examples.
|
103
|
-
|
104
|
-
### Languages mappings
|
105
|
-
|
106
|
-
A country code to languages mappping file (also in json), can be found in `app/config/languages`.
|
107
|
-
|
108
|
-
```ruby
|
109
|
-
def languages locale = :en
|
110
|
-
path = File.join(Rails.root, "app/config/languages/locale_languages.#{locale}.json")
|
111
|
-
JSON.parse File.read(path)
|
112
|
-
end
|
113
|
-
|
114
|
-
WorldFlags.languages = Hashie::Mash.new languages
|
115
|
-
```
|
116
|
-
|
117
|
-
PS: See the json specs for examples.
|
118
|
-
|
119
|
-
Another approach more suited to adding single files is the following. Note that both `countries` and `languages` are instances of classes that inherit from the `Hashie::Mash` class (hash access via method missing).
|
120
|
-
|
121
|
-
```
|
122
|
-
WorldFlags.countries.en = countries_en
|
123
|
-
WorldFlags.languages.en = languages_en
|
124
|
-
```
|
125
|
-
|
126
|
-
You can set the available locales. By default they are the same as `I18n.available_locales`.
|
127
|
-
|
128
|
-
```ruby
|
129
|
-
WorldFlags.available_locales = [:en, :da]
|
130
|
-
```
|
131
|
-
|
132
|
-
## Locale to country code
|
133
|
-
|
134
|
-
You can customize the locale to flag code map, using:
|
135
|
-
|
136
|
-
`WorldFlags.locale_flag_map = some_hash`
|
137
|
-
|
138
|
-
The gem also comes with a json file at `config/locale_map/locale_to_country_code.json` that you can load in as a hash. WorldFlags will treat any locale of the form `xx_YY` as the YY downcased (yy).
|
139
|
-
|
140
|
-
Please feel free to suggest or improve this locale/translation infrastructure!
|
141
|
-
|
142
25
|
## Rendering
|
143
26
|
|
144
27
|
Flags will be rendered in HTML as:
|
@@ -200,10 +83,10 @@ Note: The ` is needed in order for the background (flag icon) to have some
|
|
200
83
|
The :title and :content can also be set to a string which is then displayed
|
201
84
|
|
202
85
|
```haml
|
203
|
-
= flag :ar, :title => 'Argentina
|
86
|
+
= flag :ar, :title => 'Argentina country', :content => 'Argh!'
|
204
87
|
```
|
205
88
|
|
206
|
-
To
|
89
|
+
To get content rendered for the <li>
|
207
90
|
|
208
91
|
```haml
|
209
92
|
= flags :ar, :br, :gb, :content => true
|
@@ -211,17 +94,6 @@ To also get content rendered for the <li>
|
|
211
94
|
|
212
95
|
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)
|
213
96
|
|
214
|
-
## Automatic flag selection
|
215
|
-
|
216
|
-
The auto-select feature is by default turned off, but can be turned on/off using:
|
217
|
-
|
218
|
-
```ruby
|
219
|
-
WorldFlags.auto_select = true # or WorldFlags.auto_select!
|
220
|
-
```
|
221
|
-
|
222
|
-
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.
|
223
|
-
If this doesn't work out for you, try to add a WorldFlags flag-to-locale code mapping for the locale.
|
224
|
-
|
225
97
|
## Using localization
|
226
98
|
|
227
99
|
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)
|
@@ -233,129 +105,26 @@ Use danish (da) country labels
|
|
233
105
|
= flags :ar, :br, :gb, :country => :da
|
234
106
|
```
|
235
107
|
|
236
|
-
Use
|
108
|
+
Use language labels for current locale
|
237
109
|
|
238
110
|
```haml
|
239
111
|
= flag_list 32 do
|
240
112
|
= flags :ar, :br, :gb, :language => I18n.locale
|
241
113
|
```
|
242
114
|
|
243
|
-
|
244
|
-
|
245
|
-
h## Get client country code (browser and geo)
|
246
|
-
|
247
|
-
A small helper module is provided that can be inserted into a Controller or wherever you see fit
|
248
|
-
|
249
|
-
* ip_country_code
|
250
|
-
* browser_locale
|
251
|
-
|
252
|
-
```ruby
|
253
|
-
class MainController < ApplicationController
|
254
|
-
def home
|
255
|
-
@country_code = WorldFlags::Helper::Geo.ip_country_code
|
256
|
-
end
|
257
|
-
end
|
258
|
-
```
|
259
|
-
|
260
|
-
Alternatively you can include the modules directly into the controller:
|
261
|
-
|
262
|
-
```ruby
|
263
|
-
class MainController < ApplicationController
|
264
|
-
include WorldFlags::Helper::Geo
|
265
|
-
include WorldFlags::Helper::Browser
|
266
|
-
|
267
|
-
def home
|
268
|
-
@country_code = ip_country_code
|
269
|
-
@locale = browser_locale
|
270
|
-
end
|
271
|
-
end
|
272
|
-
```
|
273
|
-
|
274
|
-
If you include the `WorldFlags::Helper::Locale` module, you can simply do:
|
275
|
-
|
276
|
-
```ruby
|
277
|
-
before_filter :set_locale
|
278
|
-
```
|
279
|
-
|
280
|
-
And it should set the I18n.locale appropriately, trying `params[locale], browser, ip address` in succession, defaulting to `I18n.default_locale`.
|
115
|
+
In the /config folder of this gem/engine there is 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. You can use either of the following:
|
281
116
|
|
117
|
+
* [countries_and_languages](https://github.com/grosser/countries_and_languages)
|
118
|
+
* [i18n data](https://github.com/grosser/i18n_data)
|
119
|
+
* [i18n gen](https://github.com/kristianmandrup/i18n-gen)
|
282
120
|
|
283
|
-
|
121
|
+
To generate i18n data for your particular locale(s).
|
284
122
|
|
285
|
-
|
286
|
-
def locale_sources
|
287
|
-
[params[:locale], extract_locale_from_tld,
|
288
|
-
browser_locale, ip_country_code, I18n.default_locale]
|
289
|
-
end
|
290
|
-
```
|
291
|
-
|
292
|
-
Note: You can override this method in your controller to set the priority order for getting the user locale. Each code will be mapped to a locale code using the `WorldFlags#locale` mapping helper method.
|
293
|
-
|
294
|
-
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`.
|
295
|
-
You can override this behavior by defining you custom `valid_locales` method in the controller.
|
296
|
-
|
297
|
-
For convenience you can include `WorldFlags::Helper::All` to include all these helper modules.
|
298
|
-
|
299
|
-
Note that if you include the `WorldFlags::Helper::Geo you need the `httparty` gem as well.
|
300
|
-
|
301
|
-
Example:
|
302
|
-
|
303
|
-
```ruby
|
304
|
-
class MainController < ApplicationController
|
305
|
-
include WorldFlags::Helper::All
|
306
|
-
|
307
|
-
before_filter :set_locale
|
308
|
-
end
|
309
|
-
```
|
310
|
-
|
311
|
-
You can define the available locales for your app in your `application.rb` file:
|
312
|
-
|
313
|
-
```ruby
|
314
|
-
class Application < Rails::Application
|
315
|
-
# ...
|
316
|
-
config.i18n.available_locales = [:da, :sv, :no]
|
317
|
-
``
|
318
|
-
|
319
|
-
The perhaps sync i18n `I18n.available_locales = MyCool::Application.i18n.available_locales`
|
320
|
-
|
321
|
-
Note: This approach in turn works well with the `i18n-docs` gem ;)
|
123
|
+
## More configuration/usage tips
|
322
124
|
|
323
|
-
|
125
|
+
[Handling flag selection](https://github.com/kristianmandrup/world-flags/wiki/Handling-flag-selection)
|
324
126
|
|
325
|
-
|
326
|
-
|
327
|
-
```javascript
|
328
|
-
$("li.flag").click(function() {
|
329
|
-
country = $(this).data('locale');
|
330
|
-
|
331
|
-
// full page reload
|
332
|
-
// window.location.href = "/locale/select/" + country;
|
333
|
-
|
334
|
-
// window.location.href = window.location.href + '?locale=' + country;
|
335
|
-
|
336
|
-
// async post
|
337
|
-
$.post("/locale/select", { "country": country }, function(data) {
|
338
|
-
console.log(data);
|
339
|
-
});
|
340
|
-
});
|
341
|
-
```
|
342
|
-
|
343
|
-
This gem now comes with a simple javascript object `WorldFlagsUrlHelper` baked in. To use it, add the following to your ``application.js manifest.
|
344
|
-
|
345
|
-
```
|
346
|
-
//= require world_flags/url_helper
|
347
|
-
```
|
348
|
-
|
349
|
-
And use it something like this:
|
350
|
-
|
351
|
-
```javascript
|
352
|
-
$("li.flag").click(function() {
|
353
|
-
country = $(this).data('locale');
|
354
|
-
|
355
|
-
// full page reload with locale=xx param added to url :)
|
356
|
-
WorldFlagsUrlHelper.reloadWithLocaleParam('da');
|
357
|
-
});
|
358
|
-
```
|
127
|
+
[Configuration](https://github.com/kristianmandrup/world-flags/wiki/Configuration)
|
359
128
|
|
360
129
|
## TODO for version 1.0
|
361
130
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.3
|
data/lib/world-flags.rb
CHANGED
@@ -29,7 +29,22 @@ module WorldFlags
|
|
29
29
|
def locale code = :us
|
30
30
|
flag_locale_map[code.to_sym] || code
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
|
+
# avoid uk being translated to ukraine for domain names!
|
34
|
+
def domain_to_locale code
|
35
|
+
domain_locale_map[code.to_sym] || flag_locale_map[code.to_sym] || code
|
36
|
+
end
|
37
|
+
|
38
|
+
def domain_locale_map
|
39
|
+
{
|
40
|
+
:uk => "en_GB",
|
41
|
+
:tp => "tl",
|
42
|
+
:su => 'ru',
|
43
|
+
:an => 'nl'
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
|
33
48
|
# override using fx 'locale_to_country_code.json' file
|
34
49
|
def locale_flag_map
|
35
50
|
@locale_flag_map ||= keys_to_sym(locale_flag_hash)
|
@@ -6,10 +6,11 @@ module WorldFlags
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def valid_locales
|
9
|
+
return WorldFlags.available_locales if WorldFlags.available_locales.present?
|
9
10
|
if I18n.respond_to?(:available_locales) && I18n.available_locales.present?
|
10
11
|
I18n.available_locales
|
11
12
|
else
|
12
|
-
WorldFlags.
|
13
|
+
raise "You must define a list of available locales for use with WorldFlags either in WorldFlags.available_locales or I18n.available_locales"
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
@@ -19,7 +20,29 @@ module WorldFlags
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def locale_sources
|
22
|
-
[
|
23
|
+
locale_source_priority.inject([]) do |res, name|
|
24
|
+
res << locale_priority(name)
|
25
|
+
res
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def locale_priority name
|
30
|
+
case name.to_sym
|
31
|
+
when :param
|
32
|
+
params[:locale]
|
33
|
+
when :domain
|
34
|
+
extract_locale_from_tld # http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
|
35
|
+
when :browser
|
36
|
+
browser_locale # http://www.metamodpro.com/browser-language-codes
|
37
|
+
when :ip
|
38
|
+
ip_country_code
|
39
|
+
when :default
|
40
|
+
I18n.default_locale
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def locale_source_priority
|
45
|
+
WorldFlags.locale_source_priority
|
23
46
|
end
|
24
47
|
|
25
48
|
# Get locale from top-level domain or return nil if such locale is not available
|
@@ -33,7 +56,7 @@ module WorldFlags
|
|
33
56
|
end
|
34
57
|
|
35
58
|
def parsed_locale
|
36
|
-
WorldFlags.
|
59
|
+
WorldFlags.domain_to_locale(parsed_domain)
|
37
60
|
end
|
38
61
|
|
39
62
|
def parsed_domain
|
@@ -3,15 +3,30 @@ module WorldFlags
|
|
3
3
|
module Config
|
4
4
|
attr_accessor :auto_select, :raise_error
|
5
5
|
attr_accessor :default_code, :default_locale
|
6
|
+
attr_writer :locale_source_priority
|
7
|
+
|
8
|
+
def locale_source_priority
|
9
|
+
@locale_source_priority ||= default_locale_source_priority
|
10
|
+
end
|
11
|
+
|
12
|
+
def default_locale_source_priority
|
13
|
+
supported_locale_source_priorities
|
14
|
+
end
|
15
|
+
|
16
|
+
def supported_locale_source_priorities
|
17
|
+
[:param, :domain, :browser, :ip, :default]
|
18
|
+
end
|
6
19
|
|
7
20
|
def reset!
|
8
21
|
raise_error_off!
|
9
22
|
auto_select_off!
|
10
23
|
|
11
24
|
self.countries = nil
|
25
|
+
self.hashied_countries = nil
|
12
26
|
self.countries_map = nil
|
13
27
|
|
14
28
|
self.languages_map = nil
|
29
|
+
self.hashied_languages = nil
|
15
30
|
self.languages = nil
|
16
31
|
end
|
17
32
|
|
@@ -1,16 +1,12 @@
|
|
1
1
|
module WorldFlags
|
2
2
|
module Util
|
3
3
|
module Country
|
4
|
-
attr_writer :countries_map
|
4
|
+
attr_writer :countries_map, :hashied_countries
|
5
5
|
|
6
6
|
def country_label code, locale
|
7
7
|
WorldFlags.country code, locale
|
8
8
|
end
|
9
9
|
|
10
|
-
def countries
|
11
|
-
@countries ||= Countries.new
|
12
|
-
end
|
13
|
-
|
14
10
|
def country code = :us, locale = :en
|
15
11
|
locale ||= default_locale_used
|
16
12
|
locale = WorldFlags.locale(locale).to_sym
|
@@ -32,6 +28,10 @@ module WorldFlags
|
|
32
28
|
@countries = countries
|
33
29
|
end
|
34
30
|
|
31
|
+
def countries
|
32
|
+
@countries ||= Countries.new
|
33
|
+
end
|
34
|
+
|
35
35
|
def countries_map
|
36
36
|
@countries_map ||= begin
|
37
37
|
available_locales.inject({}) do |res, loc|
|
@@ -41,10 +41,21 @@ module WorldFlags
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
45
|
-
|
44
|
+
def hashied_countries
|
45
|
+
@hashied_countries ||= begin
|
46
|
+
case countries
|
47
|
+
when Hash
|
48
|
+
Hashie::Mash.new countries
|
49
|
+
else
|
50
|
+
countries
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_country_map loc
|
56
|
+
hashied_countries.respond_to?(loc) ? hashied_countries.send(loc) : hashied_countries.send(locale loc)
|
46
57
|
rescue
|
47
|
-
|
58
|
+
hashied_countries.send(default_locale_used)
|
48
59
|
end
|
49
60
|
end
|
50
61
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module WorldFlags
|
2
2
|
module Util
|
3
3
|
module Language
|
4
|
-
attr_writer :languages_map
|
4
|
+
attr_writer :languages_map, :hashied_languages
|
5
5
|
|
6
6
|
def language_label code, locale
|
7
7
|
language code, locale
|
@@ -50,10 +50,21 @@ module WorldFlags
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
+
def hashied_languages
|
54
|
+
@hashied_languages ||= begin
|
55
|
+
case languages
|
56
|
+
when Hash
|
57
|
+
Hashie::Mash.new languages
|
58
|
+
else
|
59
|
+
languages
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
53
64
|
def find_language_map loc
|
54
|
-
|
65
|
+
hashied_languages.respond_to?(loc) ? hashied_languages.send(loc) : hashied_languages.send(locale(loc))
|
55
66
|
rescue
|
56
|
-
|
67
|
+
hashied_languages.send(default_locale_used)
|
57
68
|
end
|
58
69
|
end
|
59
70
|
end
|
data/world-flags.gemspec
CHANGED
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.4.
|
4
|
+
version: 0.4.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -194,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
194
194
|
version: '0'
|
195
195
|
segments:
|
196
196
|
- 0
|
197
|
-
hash:
|
197
|
+
hash: 4215201966939774215
|
198
198
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
199
199
|
none: false
|
200
200
|
requirements:
|