world-flags 0.3.2.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source :rubygems
2
2
 
3
+ gem 'hashie'
4
+
3
5
  # Add dependencies to develop your gem here.
4
6
  # Include everything needed to run rake, tests, features, etc.
5
7
  group :development do
data/Gemfile.lock CHANGED
@@ -33,6 +33,7 @@ GEM
33
33
  diff-lcs (1.1.3)
34
34
  erubis (2.7.0)
35
35
  git (1.2.5)
36
+ hashie (1.2.0)
36
37
  hike (1.2.1)
37
38
  httparty (0.8.1)
38
39
  multi_json
@@ -106,6 +107,7 @@ PLATFORMS
106
107
 
107
108
  DEPENDENCIES
108
109
  bundler (>= 1.0.0)
110
+ hashie
109
111
  httparty
110
112
  jeweler (>= 1.8.3)
111
113
  rails (>= 3.1)
data/README.md CHANGED
@@ -80,30 +80,58 @@ Notice that it is a locale code pointing to a map of *ISO_3166-1_alpha-2* codes
80
80
  }
81
81
  ```
82
82
 
83
- An english translation file of country codes (in json format) can be found in `app/config/country_codes`.
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`.
84
92
 
85
93
  ```ruby
86
- cc_file_en = File.join(Rails.root, 'app/config/country_codes/iso-3166-2.en.json')
87
- country_codes_en = JSON.parse File.read(cc_file_en)
88
- WorldFlags.languages = country_codes_en
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 = countries(:en)
89
100
  ```
90
101
 
91
- Here an even better, more flexible approach that allows for multiple translations.
102
+ ### Languages mappings
92
103
 
104
+ A country code to languages mappping file (also in json), can be found in `app/config/languages`.
105
+
106
+ ```ruby
107
+ def languages locale = :en
108
+ path = File.join(Rails.root, "app/config/languages/locale_languages.#{locale}.json")
109
+ JSON.parse File.read(path)
110
+ end
111
+
112
+ WorldFlags.languages = languages(:en)
93
113
  ```
94
- WorldFlags::Language.en = country_codes_en
114
+
115
+ Another approach more suited to adding single files is the following. Note that both `countrieds` and `languages` are instances of classes that inherit from the `Hashie::Mash` class (hash access via method missing).
116
+
117
+ ```
118
+ WorldFlags.countries.en = countries_en
119
+ WorldFlags.languages.en = languages_en
95
120
  ```
96
121
 
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`.
122
+ You can set the active locales using:
98
123
 
99
124
  ```ruby
100
125
  WorldFlags.active_locales = [:en, :da]
101
- WorldFlags::Language.da = country_codes_da
102
126
  ```
103
127
 
104
- To customize the locale to flag code map, use:
128
+ ## Locale to country code
129
+
130
+ You can customize the locale to flag code map, using:
131
+
132
+ `WorldFlags.locale_flag_map = some_hash`
105
133
 
106
- `WorldFlags.locale_flag_map = some hash`
134
+ 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).
107
135
 
108
136
  Please feel free to suggest or improve this locale/translation infrastructure!
109
137
 
@@ -271,7 +299,7 @@ Example:
271
299
 
272
300
  ```ruby
273
301
  class MainController < ApplicationController
274
- include WorldFlags::All
302
+ include WorldFlags::Helper::All
275
303
 
276
304
  before_filter :set_locale
277
305
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2.2
1
+ 0.3.4
@@ -0,0 +1,253 @@
1
+ {
2
+ "en": {
3
+ "ad": "Catalan",
4
+ "ae": "Arabic",
5
+ "af": "Afghan",
6
+ "ag": "Antigua and Barbuda",
7
+ "ai": "French",
8
+ "al": "Albanian",
9
+ "am": "Armenian",
10
+ "ao": "Portuguese",
11
+ "aq": "None",
12
+ "ar": "Spanish",
13
+ "as": "Samoan",
14
+ "at": "Austrian",
15
+ "au": "English",
16
+ "aw": "English",
17
+ "ax": "English",
18
+ "az": "Dutch",
19
+ "ba": "Azerbaijani",
20
+ "bb": ["Bosnian", "Serbian", "Croatian"],
21
+ "bd": "English",
22
+ "be": "Bengali",
23
+ "bf": ["Dutch", "French", "German"],
24
+ "bg": "French",
25
+ "bh": "Bulgarian",
26
+ "bi": "Arabic",
27
+ "bj": ["Kirundi", "French"],
28
+ "bl": "French",
29
+ "bm": "English",
30
+ "bn": "Malay",
31
+ "bo": "Spanish",
32
+ "bq": "Dutch",
33
+ "br": "Portuguese",
34
+ "bs": "English",
35
+ "bt": "Dzongkha",
36
+ "bv": "Norwegian",
37
+ "bw": ["English", "Setswana"],
38
+ "by": ["Belarusian", "Russian"],
39
+ "bz": "French",
40
+ "ca": "English",
41
+ "cc": "English",
42
+ "cd": "French",
43
+ "cf": "French",
44
+ "cg": "French",
45
+ "ch": ["German", "French", "Italian"],
46
+ "ci": "French",
47
+ "ck": "English",
48
+ "cl": "Spanish",
49
+ "cm": ["French", "English"],
50
+ "cn": "Chinese",
51
+ "co": "Spanish",
52
+ "cr": "Spanish",
53
+ "cu": "Spanish",
54
+ "cv": "Portuguese",
55
+ "cw": ["Papiamentu", "Dutch"],
56
+ "cx": "English",
57
+ "cy": ["Greek", "Turkish"]
58
+ "cz": "Czech",
59
+ "de": "German",
60
+ "dj": ["French", "Arabic"],
61
+ "dk": "Danish",
62
+ "dm": "Spanish",
63
+ "do": "Spanish",
64
+ "dz": "French",
65
+ "ec": "Spanish",
66
+ "ee": "Estonian",
67
+ "eg": "Arabic",
68
+ "eh": "Arabic",
69
+ "er": ["Arabic", "English"],
70
+ "es": "Spanish",
71
+ "et": "Amharic",
72
+ "fi": ["Finnish", "Swedish"],
73
+ "fj": "English",
74
+ "fk": "English",
75
+ "fm": "English",
76
+ "fo": ["Faroese", "Danish"],
77
+ "fr": "French",
78
+ "ga": "English",
79
+ "gb": "French",
80
+ "gd": "English",
81
+ "ge": "English",
82
+ "gf": "French",
83
+ "gg": "English",
84
+ "gh": "English",
85
+ "gi": "English",
86
+ "gl": "Danish",
87
+ "gm": "English",
88
+ "gn": "French",
89
+ "gp": "French",
90
+ "gq": "French",
91
+ "gr": "Greek",
92
+ "gs": "Georgian",
93
+ "gt": "Spanish",
94
+ "gu": "French",
95
+ "gw": "French",
96
+ "gy": "English",
97
+ "hk": ["English", "Chinese"],
98
+ "hm": "English",
99
+ "hn": "Spanish",
100
+ "hr": "Croatian",
101
+ "ht": "French",
102
+ "hu": "Hungarian",
103
+ "id": ["Indonesian","Vietnamese"],
104
+ "ie": "Irish",
105
+ "il": ["Hebrew", "Arabic"],
106
+ "im": "English",
107
+ "in": ["Hindi", "English"],
108
+ "io": "English",
109
+ "iq": ["Arabic", "Kurdish"],
110
+ "ir": "Persian",
111
+ "is": "Icelandic",
112
+ "it": "Italian",
113
+ "je": "English",
114
+ "jm": "English",
115
+ "jo": "Jordanian",
116
+ "jp": "Japanese",
117
+ "ke": "English",
118
+ "kg": ["Kyrgyz", "Russian"],
119
+ "kh": "French",
120
+ "ki": ["English", "Gilbertese"],
121
+ "km": ["Comorian", "Arabic", "French"],
122
+ "kn": "English",
123
+ "kp": "Korean",
124
+ "kr": "Korean",
125
+ "kw": "Arabic",
126
+ "ky": "English",
127
+ "kz": ["Kazakh", "Russian"],
128
+ "la": "Lao",
129
+ "lb": "Arabic",
130
+ "lc": "English",
131
+ "li": "German",
132
+ "lk": ["Sinhala", "Tamil"],
133
+ "lr": "English",
134
+ "ls": ["Sesotho", "English"],
135
+ "lt": "Lithuanian",
136
+ "lu": ["Luxembourgish", "French", "German"],
137
+ "lv": "Latvian",
138
+ "ly": "Arabic",
139
+ "ma": ["Arabic", "Berber"],
140
+ "mc": "French",
141
+ "md": "Romanian",
142
+ "me": ["Serbian", "Bosnian", "Albanian", "Croatian"],
143
+ "mf": "French",
144
+ "mg": "French",
145
+ "mh": "English",
146
+ "mk": "Macedonian",
147
+ "ml": "French",
148
+ "mm": "Burmese",
149
+ "mn": "Mongolian",
150
+ "mo": "French",
151
+ "mp": "English",
152
+ "mq": "French",
153
+ "mr": "English",
154
+ "ms": "French",
155
+ "mt": "French",
156
+ "mu": "English",
157
+ "mv": "Dhivehi",
158
+ "mw": ["Chichewa", "English"],
159
+ "mx": "Spanish",
160
+ "my": "Malaysian",
161
+ "mz": "French",
162
+ "na": "English",
163
+ "nc": "French",
164
+ "ne": "French",
165
+ "nf": "English",
166
+ "ng": "English",
167
+ "ni": "Spanish",
168
+ "nl": "Dutch",
169
+ "no": "Norwegian",
170
+ "np": "Nepalese",
171
+ "nr": ["English", "Nauruan"],
172
+ "nu": ["Niuean", "English"],
173
+ "nz": "English",
174
+ "om": "Arabic",
175
+ "pa": "Spanish",
176
+ "pe": "Spanish",
177
+ "pf": "French",
178
+ "pg": "English",
179
+ "ph": "Filipino",
180
+ "pk": "Arabic",
181
+ "pl": "Polish",
182
+ "pm": "French",
183
+ "pn": "English",
184
+ "pr": "Spanish",
185
+ "ps": ["Arabic", "Hebrew", "English"],
186
+ "pt": "Portuguese",
187
+ "pw": ["Palaun", "English"],
188
+ "py": "Spanish",
189
+ "qa": "Arabic",
190
+ "re": "French",
191
+ "ro": "Romanian",
192
+ "rs": "Serbian",
193
+ "ru": "Russian",
194
+ "rw": "French",
195
+ "sa": "Arabic",
196
+ "sb": "English",
197
+ "sc": ["French", "English"],
198
+ "sd": "Arabic",
199
+ "se": "Swedish",
200
+ "sg": ["English", "Malay", "Chinese"],
201
+ "sh": "Spanish",
202
+ "si": "Slovenian",
203
+ "sj": "Norwegian",
204
+ "sk": "Slovakian",
205
+ "sl": "French",
206
+ "sm": "Italian",
207
+ "sn": "Senegalese",
208
+ "so": ["Somali", "Arabic"],
209
+ "sr": "French",
210
+ "ss": "French",
211
+ "st": "French",
212
+ "sv": "Spanish",
213
+ "sx": "Dutch",
214
+ "sy": "Arabic",
215
+ "sz": ["Swati", "English"],
216
+ "tc": "English",
217
+ "td": "French",
218
+ "tf": "French",
219
+ "tg": "French",
220
+ "th": "Thai",
221
+ "tj": ["Tajik", "Russian"],
222
+ "tk": ["Tokelauan", "English"],
223
+ "tl": ["Tetum", "Portuguese"],
224
+ "tm": ["Turkmen", "Russian"],
225
+ "tn": "French",
226
+ "to": "French",
227
+ "tr": "Turkish",
228
+ "tt": "French",
229
+ "tv": ["Tuvaluan", "English"],
230
+ "tw": ["Chinese", "Taiwanese"],
231
+ "tz": ["Swahili", "English"],
232
+ "ua": ["Ukrainian", "Russian"],
233
+ "ug": ["Swahili", "English"],
234
+ "um": "English",
235
+ "us": "English",
236
+ "uy": "Spanish",
237
+ "uz": ["Uzbek", "Russian"],
238
+ "va": ["Latin", "Italian"],
239
+ "vc": "French",
240
+ "ve": "Spanish",
241
+ "vg": "English",
242
+ "vi": "English",
243
+ "vn": "Vietnamese",
244
+ "vu": ["Bislama", "English", "French"],
245
+ "wf": "French",
246
+ "ws": ["Samoan", "English"],
247
+ "ye": "Arabic",
248
+ "yt": "French",
249
+ "za": ["Afrikaans", "English", "Ndebele", "Sotho", "Swazi", "Tswana", "Tsonga", "Venda", "Xhosa", "Zulu"],
250
+ "zm": "English",
251
+ "zw": ["English", "Shona", "Sindebele"]
252
+ }
253
+ }
@@ -0,0 +1,35 @@
1
+ {
2
+ "af": "za",
3
+ "en": "us",
4
+ "da": "dk",
5
+ "sv": "se",
6
+ "sq": "al",
7
+ "nb": "no",
8
+ "ja": "jp",
9
+ "kk": "kz",
10
+ "ko": "kr",
11
+ "ky": "kg",
12
+ "hy": "am",
13
+ "ms": "bn",
14
+ "be": "by",
15
+ "bg": "bh",
16
+ "ca": "ad",
17
+ "zh": "cn",
18
+ "sr": "rs",
19
+ "sl": "si",
20
+ "et": "ee",
21
+ "sw": "tz",
22
+ "ta": "lk",
23
+ "ka": "gs",
24
+ "el": "gr",
25
+ "uk": "ua",
26
+ "he": "il",
27
+ "hi": "in",
28
+ "vi": "vn"
29
+ }
30
+
31
+
32
+
33
+
34
+
35
+
data/lib/world-flags.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'hashie'
1
2
  require "world_flags/core_ext"
2
3
  require "world_flags/helpers"
3
4
 
@@ -6,9 +7,13 @@ require 'world_flags/rails/engine' if defined?(::Rails::Engine)
6
7
  require "world_flags/languages"
7
8
  require "world_flags/countries"
8
9
 
10
+ require "world_flags/lang_util"
11
+ require "world_flags/country_util"
12
+
13
+
9
14
  module WorldFlags
10
15
  class << self
11
- attr_accessor :auto_select, :raise_error, :default_code
16
+ attr_accessor :auto_select, :raise_error, :default_code, :default_locale
12
17
 
13
18
  # TODO: Why both active and valid locales? Does this even make sense!?
14
19
  attr_writer :active_locales
@@ -32,6 +37,14 @@ module WorldFlags
32
37
  @raise_error
33
38
  end
34
39
 
40
+ def raise_error!
41
+ @raise_error = true
42
+ end
43
+
44
+ def raise_error_off!
45
+ @raise_error = false
46
+ end
47
+
35
48
  def auto_select?
36
49
  auto_select
37
50
  end
@@ -40,117 +53,63 @@ module WorldFlags
40
53
  @auto_select = true
41
54
  end
42
55
 
43
- def label code = :en, options = {:language => :en}
44
- label = options[:country] ? WorldFlags.country(options[:country], code) : WorldFlags.language(options[:language], code)
45
- # if all else fails
46
- default_locale = I18n.locale || :en
47
- label ||= WorldFlags.language(default_locale, code)
56
+ def label code = :us, options = {:language => :en}
57
+ locale = extract_locale!(options) || default_locale_used || :en
58
+ options[:country] ? country_label(code, locale) : language_label(code, locale)
59
+ end
60
+
61
+ def extract_locale! options
62
+ options[:country] ? options.delete(:country) : options.delete(:language)
48
63
  end
49
64
 
50
65
  # Locale translation helper macros
51
66
 
52
- def flag_code code = :en
53
- locale_flag_map[code.to_sym] || code
67
+ def flag_code code = :us
68
+ # ensure that 'en_US' becomes simply 'us'
69
+ code = code.to_s.sub(/^\w+_/, '').downcase
70
+ (locale_flag_map[code.to_sym] || code).to_sym
54
71
  end
55
72
 
56
- def locale code = :en
73
+ def locale code = :us
57
74
  flag_locale_map[code.to_sym] || code
58
75
  end
59
76
 
60
77
  attr_writer :locale_flag_map
61
78
 
62
- # translate locales to flag code: ISO_3166-1_alpha-2
79
+ # override using fx 'locale_to_country_code.json' file
63
80
  def locale_flag_map
64
- @locale_map ||= {
65
- :en => :us,
66
- :da => :dk,
67
- :sv => :se,
68
- :nb => :no,
69
- :'sv_SE' => :se,
70
- :'en_UK' => :gb,
71
- :'en_US' => :us
72
- }
73
- end
74
-
75
- def flag_locale_map
76
- locale_flag_map.hash_revert
77
- end
78
-
79
- def default_code_used
80
- WorldFlags.default_code || :en
81
+ @locale_flag_map ||= keys_to_sym(locale_flag_hash)
81
82
  end
82
83
 
83
- # Language helper macros
84
-
85
- def language locale = :en, code = :en
86
- locale ||= default_code_used
87
- locale_languages_map = languages[locale] || languages[default_code_used]
88
-
89
- raise "No language-locale map defined for locale: #{locale} in #{languages}" if locale_languages_map.blank?
90
-
91
- # raise("No language map defined for language code: #{code} in #{locale_languages_map[code]}")
92
- locale_languages_map[code] ? locale_languages_map[code] : locale_languages_map[default_code_used]
93
- rescue Exception => e
94
- raise e if WorldFlags.raise_error?
95
- "Undefined"
96
- end
97
-
98
- def languages= languages
99
- raise ArgumentError, "Must be a hash, was: #{languages}" unless languages.kind_of?(Hash)
100
- @languages = languages
84
+ def keys_to_sym hash
85
+ hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
101
86
  end
102
87
 
103
- def languages
104
- @languages ||= begin
105
- active_locales.inject({}) do |res, loc|
106
- res[locale] = find_language_map(loc)
107
- res
108
- end
109
- end
110
- end
88
+ def locale_flag_hash
89
+ {
90
+ :en => "us",
91
+ :da => "dk",
92
+ :sv => "se",
93
+ :sq => "al",
94
+ :nb => "no",
95
+ :ja => "jp",
96
+ :uk => "ua"
97
+ }
98
+ end
111
99
 
112
- def find_language_map loc
113
- # return Countries.send(loc) if Countries.respond_to?(loc)
114
- [loc, flag_code(loc), locale(loc)].each do |code|
115
- return Languages.send(locale) if Languages.respond_to?(locale)
116
- end
100
+ def flag_locale_map
101
+ locale_flag_map.hash_revert
117
102
  end
118
103
 
119
- # Country helper macros
120
-
121
- def country locale = :en, code = :en
122
- locale ||= default_code_used
123
-
124
- locale_countries_map = countries[locale] || countries[default_code_used]
125
-
126
- raise "No country-locale map defined for locale: #{locale} in #{countries}" if locale_countries_map.blank?
127
-
128
- # raise("No country map defined for country code: #{code} in #{locale_countries_map[code]}")
129
- locale_countries_map[code] ? locale_countries_map[code] : locale_countries_map[default_code_used]
130
- rescue Exception => e
131
- raise e if WorldFlags.raise_error?
132
- "Undefined"
104
+ def default_code_used
105
+ WorldFlags.default_code || :us
133
106
  end
134
107
 
135
- def countries= countries
136
- raise ArgumentError, "Must be a hash, was: #{countries}" unless countries.kind_of?(Hash)
137
- @countries = countries
108
+ def default_locale_used
109
+ WorldFlags.default_locale || I18n.locale
138
110
  end
139
111
 
140
- def countries
141
- @countries ||= begin
142
- active_locales.inject({}) do |res, loc|
143
- res[loc] = find_country_map(loc)
144
- res
145
- end
146
- end
147
- end
148
-
149
- def find_country_map loc
150
- # return Countries.send(loc) if Countries.respond_to?(loc)
151
- [loc, flag_code(loc), locale(loc)].each do |code|
152
- return Countries.send(code) if Countries.respond_to?(code)
153
- end
154
- end
112
+ include WorldFlags::LangUtil
113
+ include WorldFlags::CountryUtil
155
114
  end
156
115
  end