genki-merb_babel 0.1.0.7 → 0.1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +8 -1
- data/lib/merb_babel/m_i18n.rb +3 -3
- data/lib/merb_babel/m_l10n.rb +11 -5
- data/lib/merb_babel/m_locale.rb +78 -80
- data/lib/merb_babel.rb +3 -3
- data/spec/m_i18n_spec.rb +5 -1
- data/spec/m_l10n_spec.rb +13 -9
- data/spec/spec_helper.rb +1 -1
- metadata +2 -3
- data/lib/merb_babel/core_ext.rb +0 -29
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.1.0"
|
9
9
|
AUTHOR = "Matt Aimonetti"
|
10
10
|
EMAIL = "mattaimonetti@gmail.com"
|
11
11
|
HOMEPAGE = "http://github.com/mattetti/merb_babel/"
|
@@ -49,3 +49,10 @@ task :gemspec do
|
|
49
49
|
file.puts spec.to_ruby
|
50
50
|
end
|
51
51
|
end
|
52
|
+
|
53
|
+
desc "Run specs"
|
54
|
+
task :spec do
|
55
|
+
sh "spec --color spec"
|
56
|
+
end
|
57
|
+
|
58
|
+
task :default => :spec
|
data/lib/merb_babel/m_i18n.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module MI18n
|
2
2
|
|
3
3
|
def self.lookup(options)
|
4
|
-
key = options[:key].to_s
|
4
|
+
key = options[:key].to_s
|
5
5
|
language = options[:language]
|
6
6
|
country = options[:country]
|
7
7
|
raise ArgumentError, "You need to pass a language reference" unless language
|
@@ -19,9 +19,9 @@ module MI18n
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.lookup_with_full_locale(key, language, country)
|
22
|
-
country = country.to_s.intern
|
23
22
|
if ML10n.localizations.has_key?(language)
|
24
|
-
ML10n.localizations[language].has_key?(country) ?
|
23
|
+
ML10n.localizations[language].has_key?(country) ?
|
24
|
+
ML10n.localizations[language][country][key.to_s] : nil
|
25
25
|
else
|
26
26
|
nil
|
27
27
|
end
|
data/lib/merb_babel/m_l10n.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module ML10n
|
2
|
+
LANGUAGE_CODE_KEY = 'mloc_language_code'.freeze
|
3
|
+
COUNTRY_CODE_KEY = 'mloc_country_code'.freeze
|
2
4
|
|
3
5
|
class << self
|
4
6
|
|
@@ -45,7 +47,7 @@ module ML10n
|
|
45
47
|
ML10n.reset_localization_files!
|
46
48
|
ML10n.find_localization_files.each do |l_file|
|
47
49
|
begin
|
48
|
-
l_hash = YAML.load_file(l_file)
|
50
|
+
l_hash = YAML.load_file(l_file)#.symbolize_keys
|
49
51
|
rescue Exception => e
|
50
52
|
# might raise a real error here in the future
|
51
53
|
p e.inspect
|
@@ -85,6 +87,10 @@ module ML10n
|
|
85
87
|
@@localization_files = nil
|
86
88
|
ML10n.find_localization_files
|
87
89
|
end
|
90
|
+
|
91
|
+
def reset_localizations!
|
92
|
+
@@localizations = {}
|
93
|
+
end
|
88
94
|
|
89
95
|
def reload_localization_files!
|
90
96
|
ML10n.reset_localization_files!
|
@@ -99,10 +105,10 @@ module ML10n
|
|
99
105
|
protected
|
100
106
|
|
101
107
|
def load_localization_hash(l_hash)
|
102
|
-
if l_hash.
|
103
|
-
language = l_hash[
|
104
|
-
if l_hash.
|
105
|
-
country = l_hash[
|
108
|
+
if l_hash.has_key?(LANGUAGE_CODE_KEY)
|
109
|
+
language = l_hash[LANGUAGE_CODE_KEY]
|
110
|
+
if l_hash.has_key?(COUNTRY_CODE_KEY)
|
111
|
+
country = l_hash[COUNTRY_CODE_KEY]
|
106
112
|
# load localization under the full locale namespace
|
107
113
|
ML10n.localizations[language] ||= {}
|
108
114
|
(ML10n.localizations[language][country] ||= {}).merge!(l_hash)
|
data/lib/merb_babel/m_locale.rb
CHANGED
@@ -4,95 +4,93 @@ require 'locale'
|
|
4
4
|
# The MLocale module helps you set up a locale, language, country
|
5
5
|
# You don't have to use a locale, in some cases you might just want to use the language
|
6
6
|
module MLocale
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
request.env[:locale] = "#{language}-#{country}"
|
25
|
-
end
|
7
|
+
def locale_from_request
|
8
|
+
if hal = request.env["HTTP_ACCEPT_LANGUAGE"]
|
9
|
+
hal.gsub!(/\s/, "")
|
10
|
+
result = hal.split(/,/).map do |v|
|
11
|
+
v.split(";q=")
|
12
|
+
end.map do |j|
|
13
|
+
[j[0], j[1] ? j[1].to_f : 1.0]
|
14
|
+
end.sort do |a,b|
|
15
|
+
-(a[1] <=> b[1])
|
16
|
+
end.map do |v|
|
17
|
+
Locale::Tag.parse(v[0])
|
18
|
+
end.first
|
19
|
+
return nil if result.nil?
|
20
|
+
language = result.language
|
21
|
+
country = result.country ||
|
22
|
+
LocaleDetector.country_from_language(language)
|
23
|
+
request.env[:locale] = "#{language}-#{country}"
|
26
24
|
end
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
# A locale is made of a language + country code, such as en-UK or en-US
|
28
|
+
def locale
|
29
|
+
request.env[:locale] || params[:locale] || (session ? session[:locale] : nil) || locale_from_request || default_locale
|
30
|
+
end
|
31
|
+
|
32
|
+
# Many people don't care about locales, they might just want to use languages instead
|
33
|
+
def language
|
34
|
+
request.env[:language] || params[:language] || language_from_locale || (session ? session[:language] : nil) || default_language
|
35
|
+
end
|
36
|
+
|
37
|
+
# The country is used when localizing currency or time
|
38
|
+
def country
|
39
|
+
request.env[:country] || params[:country] || country_from_locale || (session ? session[:country] : nil) || LocaleDetector.country_from_language(language) || default_country
|
40
|
+
end
|
41
|
+
|
42
|
+
# Extract the language from the locale
|
43
|
+
def language_from_locale
|
44
|
+
if request.env[:locale] && request.env[:locale] =~ locale_regexp
|
45
|
+
language, country = request.env[:locale].match(locale_regexp).captures
|
46
|
+
return language
|
47
|
+
else
|
48
|
+
return nil
|
36
49
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
50
|
+
end
|
51
|
+
|
52
|
+
# Extract the country from the locale
|
53
|
+
def country_from_locale
|
54
|
+
request.env[:locale] ? request.env[:locale][3..5].upcase : nil
|
55
|
+
end
|
56
|
+
|
57
|
+
# Defaults set in the plugin settings
|
58
|
+
# You can change the default settings by overwriting
|
59
|
+
# the Merb::Plugins.config[:merb_abel] hash in your settings
|
60
|
+
#
|
61
|
+
def default_locale
|
62
|
+
Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_locale] : nil
|
41
63
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
if request.env[:locale] && request.env[:locale] =~ locale_regexp
|
46
|
-
language, country = request.env[:locale].match(locale_regexp).captures
|
47
|
-
return language
|
48
|
-
else
|
49
|
-
return nil
|
50
|
-
end
|
64
|
+
|
65
|
+
def default_language
|
66
|
+
Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_language] : nil
|
51
67
|
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
request.env[:locale] ? request.env[:locale][3..5].upcase : nil
|
68
|
+
|
69
|
+
def default_country
|
70
|
+
Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_country] : nil
|
56
71
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
#
|
62
|
-
def default_locale
|
63
|
-
Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_locale] : nil
|
64
|
-
end
|
72
|
+
#
|
73
|
+
#### end of defaults
|
74
|
+
|
75
|
+
protected
|
65
76
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
def default_country
|
71
|
-
Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_country] : nil
|
72
|
-
end
|
73
|
-
#
|
74
|
-
#### end of defaults
|
77
|
+
def locale_regexp
|
78
|
+
/(.+)\-([a-z]{2})/i
|
79
|
+
end
|
75
80
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
81
|
+
# takes a locale as in fr-FR or en-US
|
82
|
+
def set_locale
|
83
|
+
if locale =~ locale_regexp
|
84
|
+
language, country = locale.match(locale_regexp).captures
|
85
|
+
# Set the locale, language and country
|
86
|
+
language = language.downcase
|
87
|
+
country = country.upcase
|
88
|
+
request.env[:locale] = "#{language}-#{country}"
|
80
89
|
end
|
81
90
|
|
82
|
-
|
83
|
-
def set_locale
|
84
|
-
if locale =~ locale_regexp
|
85
|
-
language, country = locale.match(locale_regexp).captures
|
86
|
-
# Set the locale, language and country
|
87
|
-
language = language.downcase
|
88
|
-
country = country.upcase
|
89
|
-
request.env[:locale] = "#{language}-#{country}"
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
def set_language
|
95
|
-
request.env[:language] = language.downcase
|
96
|
-
end
|
91
|
+
end
|
97
92
|
|
93
|
+
def set_language
|
94
|
+
request.env[:language] = language.downcase
|
95
|
+
end
|
98
96
|
end
|
data/lib/merb_babel.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) / "merb_babel" / "core_ext")
|
1
|
+
#require File.join(File.dirname(__FILE__) / "merb_babel" / "core_ext")
|
2
2
|
|
3
3
|
# make sure we're running inside Merb
|
4
4
|
if defined?(Merb::Plugins)
|
@@ -29,8 +29,8 @@ if defined?(Merb::Plugins)
|
|
29
29
|
options.merge!(:language => language) unless options.has_key?(:language)
|
30
30
|
options.merge!(:country => country) unless options.has_key?(:country)
|
31
31
|
MI18n.lookup(options)
|
32
|
-
rescue
|
33
|
-
|
32
|
+
#rescue
|
33
|
+
# key.to_s
|
34
34
|
end
|
35
35
|
end
|
36
36
|
alias :translate :babelize
|
data/spec/m_i18n_spec.rb
CHANGED
@@ -10,6 +10,10 @@ describe '#babelize' do
|
|
10
10
|
ML10n.load_localization!
|
11
11
|
end
|
12
12
|
|
13
|
+
after(:each) do
|
14
|
+
ML10n.reset_localizations!
|
15
|
+
end
|
16
|
+
|
13
17
|
it "should babelize a word in English " do
|
14
18
|
@c.locale.should == 'en-US'
|
15
19
|
@c.language.should == 'en'
|
@@ -46,4 +50,4 @@ describe '#babelize' do
|
|
46
50
|
@c.t(:greetings, :language => 'fr').should == 'Salut'
|
47
51
|
end
|
48
52
|
|
49
|
-
end
|
53
|
+
end
|
data/spec/m_l10n_spec.rb
CHANGED
@@ -7,6 +7,10 @@ describe "ML10n" do
|
|
7
7
|
@lang_test_path_2 = File.expand_path(File.dirname(__FILE__) + "/other_lang_dir")
|
8
8
|
ML10n.reset_localization_files_and_dirs!
|
9
9
|
end
|
10
|
+
|
11
|
+
after(:each) do
|
12
|
+
ML10n.reset_localizations!
|
13
|
+
end
|
10
14
|
|
11
15
|
it "should have a list of localization directories" do
|
12
16
|
ML10n.localization_dirs.should == Merb::Plugins.config[:merb_babel][:localization_dirs]
|
@@ -27,24 +31,24 @@ describe "ML10n" do
|
|
27
31
|
it "should load localization files and have them available" do
|
28
32
|
ML10n.add_localization_dir(@lang_test_path)
|
29
33
|
ML10n.load_localization!
|
30
|
-
ML10n.localizations['en'][
|
31
|
-
ML10n.localizations['en'][
|
32
|
-
ML10n.localizations['en']['US'][
|
34
|
+
ML10n.localizations['en']['right'].should == 'right'
|
35
|
+
ML10n.localizations['en']['left'].should == 'left'
|
36
|
+
ML10n.localizations['en']['US']['greetings'].should == 'Howdie'
|
33
37
|
end
|
34
38
|
|
35
39
|
it "should load more localization files and have them available" do
|
36
40
|
ML10n.add_localization_dir(@lang_test_path)
|
37
41
|
ML10n.load_localization!
|
38
|
-
ML10n.localizations['en'][
|
42
|
+
ML10n.localizations['en']['right'].should == 'right'
|
39
43
|
ML10n.localizations.has_key?('fr').should be_false
|
40
44
|
|
41
45
|
ML10n.add_localization_dir(@lang_test_path_2)
|
42
46
|
ML10n.load_localization!
|
43
|
-
ML10n.localizations['en'][
|
47
|
+
ML10n.localizations['en']['right'].should == 'right'
|
44
48
|
ML10n.localizations.has_key?('fr').should be_true
|
45
|
-
ML10n.localizations['fr'][
|
46
|
-
ML10n.localizations['fr'][
|
47
|
-
ML10n.localizations['fr'][
|
49
|
+
ML10n.localizations['fr']['right'].should == 'la droite'
|
50
|
+
ML10n.localizations['fr']['left'].should == 'la gauche'
|
51
|
+
ML10n.localizations['fr']['greetings'].should == 'Salut'
|
48
52
|
end
|
49
53
|
|
50
|
-
end
|
54
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.1.0
|
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-13 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -46,7 +46,6 @@ files:
|
|
46
46
|
- Rakefile
|
47
47
|
- TODO
|
48
48
|
- lib/merb_babel
|
49
|
-
- lib/merb_babel/core_ext.rb
|
50
49
|
- lib/merb_babel/locale_detector.rb
|
51
50
|
- lib/merb_babel/m_i18n.rb
|
52
51
|
- lib/merb_babel/m_l10n.rb
|
data/lib/merb_babel/core_ext.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
module MerbBabel
|
2
|
-
|
3
|
-
module HashExtension
|
4
|
-
|
5
|
-
def symbolize_keys
|
6
|
-
inject({}) do |options, (key, value)|
|
7
|
-
options[key.to_sym || key] = value
|
8
|
-
options
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def symbolize_keys!
|
13
|
-
self.replace(self.symbolize_keys)
|
14
|
-
end
|
15
|
-
|
16
|
-
def valid_localization_hash?
|
17
|
-
self.has_key?(:mloc_language_code)
|
18
|
-
end
|
19
|
-
|
20
|
-
def localization_hash_with_locale?
|
21
|
-
self.has_key?(:mloc_country_code)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
Hash.send :include, MerbBabel::HashExtension
|