genki-merb_babel 0.1.0.1 → 0.1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +2 -1
- data/lib/merb_babel/country_guesser.rb +80 -0
- data/lib/merb_babel/m_locale.rb +27 -5
- metadata +12 -2
data/Rakefile
CHANGED
|
@@ -5,7 +5,7 @@ require 'merb-core'
|
|
|
5
5
|
require 'merb-core/tasks/merb'
|
|
6
6
|
|
|
7
7
|
GEM_NAME = "merb_babel"
|
|
8
|
-
GEM_VERSION = "0.1.0.
|
|
8
|
+
GEM_VERSION = "0.1.0.2"
|
|
9
9
|
AUTHOR = "Matt Aimonetti"
|
|
10
10
|
EMAIL = "mattaimonetti@gmail.com"
|
|
11
11
|
HOMEPAGE = "http://github.com/mattetti/merb_babel/"
|
|
@@ -24,6 +24,7 @@ spec = Gem::Specification.new do |s|
|
|
|
24
24
|
s.email = EMAIL
|
|
25
25
|
s.homepage = HOMEPAGE
|
|
26
26
|
s.add_dependency('merb', '>= 1.0.7.1')
|
|
27
|
+
s.add_dependency('locale', '>= 0.9.0')
|
|
27
28
|
s.require_path = 'lib'
|
|
28
29
|
s.files = %w(LICENSE README.markdown Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
|
29
30
|
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
module CountryGuesser
|
|
2
|
+
LANGUAGE_TO_COUNTRY = {
|
|
3
|
+
"nn"=>["NO"],
|
|
4
|
+
"uk"=>["UA"],
|
|
5
|
+
"it"=>["IT", "GI", "CH", "LY", "SM"],
|
|
6
|
+
"zh_TW"=>["TT", "VN", "HK"],
|
|
7
|
+
"no"=>["NO"],
|
|
8
|
+
"st"=>["ZA"],
|
|
9
|
+
"tk"=>["TM"],
|
|
10
|
+
"bn"=>["IN"],
|
|
11
|
+
"mn"=>["MN"],
|
|
12
|
+
"ja"=>["JP"],
|
|
13
|
+
"fr"=>["FR", "CD", "GG", "RW", "CG", "LU", "TT", "CH", "CI", "JE", "SC",
|
|
14
|
+
"VN", "BE", "MU", "DJ", "BI", "CA"],
|
|
15
|
+
"hi"=>["TT", "AE", "IN"],
|
|
16
|
+
"de"=>["DE", "LU", "CH", "LI", "AT", "BE", "RO"],
|
|
17
|
+
"ne"=>["NP"], "jw"=>["ID"],
|
|
18
|
+
"ta"=>["IN", "SG"],
|
|
19
|
+
"tl"=>["PH"],
|
|
20
|
+
"pt_PT"=>["PT", "GI"],
|
|
21
|
+
"hu"=>["HU", "SK", "RO"],
|
|
22
|
+
"sk"=>["SK"],
|
|
23
|
+
"fi"=>["FI"],
|
|
24
|
+
"sv"=>["SE", "FI"],
|
|
25
|
+
"iw"=>["IL"],
|
|
26
|
+
"az"=>["AZ"],
|
|
27
|
+
"zh"=>["TW"],
|
|
28
|
+
"ru"=>["RU", "KZ", "KG", "AZ", "LV", "UA", "TM", "AM", "UZ"],
|
|
29
|
+
"ky"=>["KG"],
|
|
30
|
+
"es"=>["ES", "HN", "CU", "PR", "TT", "PA", "VE", "DO", "BO", "GI", "PE",
|
|
31
|
+
"PY", "NI", "CL", "SV", "CO", "UY", "BZ", "CR", "EC", "GT", "AR", "MX"],
|
|
32
|
+
"ko"=>["KR"],
|
|
33
|
+
"pt_BR"=>["BR"],
|
|
34
|
+
"sw"=>["RW", "KE"],
|
|
35
|
+
"ga"=>["IE"],
|
|
36
|
+
"id"=>["ID"],
|
|
37
|
+
"eu"=>["ES"],
|
|
38
|
+
"gl"=>["ES"],
|
|
39
|
+
"xh"=>["ZA"],
|
|
40
|
+
"uz"=>["TM", "UZ"],
|
|
41
|
+
"mr"=>["IN"],
|
|
42
|
+
"fa"=>["AE"],
|
|
43
|
+
"zu"=>["LS", "ZA"],
|
|
44
|
+
"af"=>["NA", "ZA"],
|
|
45
|
+
"pl"=>["PL"],
|
|
46
|
+
"hy"=>["AM"],
|
|
47
|
+
"pa"=>["PK"],
|
|
48
|
+
"te"=>["IN"],
|
|
49
|
+
"ar"=>["AE", "LY", "SA", "DJ"],
|
|
50
|
+
"ms"=>["MY", "SG"],
|
|
51
|
+
"el"=>["GR"],
|
|
52
|
+
"ro"=>["RO"],
|
|
53
|
+
"mt"=>["MT"],
|
|
54
|
+
"ur"=>["AE", "PK"],
|
|
55
|
+
"da"=>["DK", "GL"],
|
|
56
|
+
"ca"=>["ES"],
|
|
57
|
+
"tr"=>["TR"],
|
|
58
|
+
"zh_CN"=>["SG"],
|
|
59
|
+
"nl"=>["NL", "BE", "ID"],
|
|
60
|
+
"vi"=>["VN"],
|
|
61
|
+
"lt"=>["LT", "LV"],
|
|
62
|
+
"th"=>["TH"],
|
|
63
|
+
"fo"=>["DK"],
|
|
64
|
+
"en"=>["US", "AS", "IE", "NP", "MY", "UK", "FM", "VC", "PR", "TT", "AU",
|
|
65
|
+
"PA", "NA", "GG", "LS", "RW", "VG", "AE", "GI", "VI", "AG", "TH", "CH",
|
|
66
|
+
"NF", "JE", "AI", "GL", "PH", "CK", "GM", "LY", "IN", "NZ", "NI", "SC",
|
|
67
|
+
"PK", "VN", "MS", "TO", "MT", "BE", "PN", "MU", "UG", "SG", "ZA", "BZ",
|
|
68
|
+
"SH", "MW", "FJ", "CR", "HK", "JM", "KE", "CA", "ID"],
|
|
69
|
+
"lv"=>["LV"]
|
|
70
|
+
}.freeze
|
|
71
|
+
|
|
72
|
+
module_function
|
|
73
|
+
def countries_from_language(language)
|
|
74
|
+
LANGUAGE_TO_COUNTRY[language]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def country_from_language(language)
|
|
78
|
+
(countries_from_language(language) || []).first
|
|
79
|
+
end
|
|
80
|
+
end
|
data/lib/merb_babel/m_locale.rb
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
|
+
require 'merb_babel/country_guesser'
|
|
2
|
+
require 'locale'
|
|
3
|
+
|
|
1
4
|
# The MLocale module helps you set up a locale, language, country
|
|
2
5
|
# You don't have to use a locale, in some cases you might just want to use the language
|
|
3
6
|
module MLocale
|
|
4
|
-
|
|
7
|
+
|
|
8
|
+
def locale_from_request
|
|
9
|
+
if hal = request.env["HTTP_ACCEPT_LANGUAGE"]
|
|
10
|
+
hal.gsub!(/\s/, "")
|
|
11
|
+
result = hal.split(/,/).map do |v|
|
|
12
|
+
v.split(";q=")
|
|
13
|
+
end.map do |j|
|
|
14
|
+
[j[0], j[1] ? j[1].to_f : 1.0]
|
|
15
|
+
end.sort do |a,b|
|
|
16
|
+
-(a[1] <=> b[1])
|
|
17
|
+
end.map do |v|
|
|
18
|
+
Locale::Tag.parse(v[0])
|
|
19
|
+
end.first
|
|
20
|
+
return nil if result.nil?
|
|
21
|
+
language = result.language
|
|
22
|
+
country = result.country ||
|
|
23
|
+
CountryGuesser.country_from_language(language)
|
|
24
|
+
request.env[:locale] = "#{language}-#{country}"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
5
28
|
# A locale is made of a language + country code, such as en-UK or en-US
|
|
6
29
|
def locale
|
|
7
|
-
request.env[:locale] || params[:locale] || (session ? session[:locale] : nil) || default_locale
|
|
30
|
+
request.env[:locale] || params[:locale] || (session ? session[:locale] : nil) || locale_from_request || default_locale
|
|
8
31
|
end
|
|
9
32
|
|
|
10
33
|
# Many people don't care about locales, they might just want to use languages instead
|
|
@@ -14,7 +37,7 @@ module MLocale
|
|
|
14
37
|
|
|
15
38
|
# The country is used when localizing currency or time
|
|
16
39
|
def country
|
|
17
|
-
request.env[:country] || params[:country] || country_from_locale || (session ? session[:country] : nil) || default_country
|
|
40
|
+
request.env[:country] || params[:country] || country_from_locale || (session ? session[:country] : nil) || CountryGuesser.country_from_language(language) || default_country
|
|
18
41
|
end
|
|
19
42
|
|
|
20
43
|
# Extract the language from the locale
|
|
@@ -58,7 +81,6 @@ module MLocale
|
|
|
58
81
|
|
|
59
82
|
# takes a locale as in fr-FR or en-US
|
|
60
83
|
def set_locale
|
|
61
|
-
|
|
62
84
|
if locale =~ locale_regexp
|
|
63
85
|
language, country = locale.match(locale_regexp).captures
|
|
64
86
|
# Set the locale, language and country
|
|
@@ -73,4 +95,4 @@ module MLocale
|
|
|
73
95
|
request.env[:language] = language.downcase
|
|
74
96
|
end
|
|
75
97
|
|
|
76
|
-
end
|
|
98
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: genki-merb_babel
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.0.
|
|
4
|
+
version: 0.1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matt Aimonetti
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-01-
|
|
12
|
+
date: 2009-01-04 00:00:00 -08:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
@@ -21,6 +21,15 @@ dependencies:
|
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
22
|
version: 1.0.7.1
|
|
23
23
|
version:
|
|
24
|
+
- !ruby/object:Gem::Dependency
|
|
25
|
+
name: locale
|
|
26
|
+
version_requirement:
|
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
28
|
+
requirements:
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 0.9.0
|
|
32
|
+
version:
|
|
24
33
|
description: Merb plugin that provides simple localization/internationalisation
|
|
25
34
|
email: mattaimonetti@gmail.com
|
|
26
35
|
executables: []
|
|
@@ -38,6 +47,7 @@ files:
|
|
|
38
47
|
- TODO
|
|
39
48
|
- lib/merb_babel
|
|
40
49
|
- lib/merb_babel/core_ext.rb
|
|
50
|
+
- lib/merb_babel/country_guesser.rb
|
|
41
51
|
- lib/merb_babel/m_i18n.rb
|
|
42
52
|
- lib/merb_babel/m_l10n.rb
|
|
43
53
|
- lib/merb_babel/m_locale.rb
|