grosser-fast_gettext 0.4.10 → 0.4.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 4
4
- :patch: 10
4
+ :patch: 11
@@ -103,8 +103,7 @@ module FastGettext
103
103
 
104
104
  @@default_locale = nil
105
105
  def default_locale=(new_locale)
106
- new_locale = best_locale_in(new_locale)
107
- @@default_locale = new_locale
106
+ @@default_locale = best_locale_in(new_locale)
108
107
  update_current_cache
109
108
  end
110
109
 
@@ -117,21 +116,10 @@ module FastGettext
117
116
  #IE6/7 de
118
117
  #nil if nothing matches
119
118
  def best_locale_in(locales)
120
- locales = locales.to_s.gsub(/\s/,'')
121
-
122
- #split the locale and seperate it into different languages
123
- #[['de-de','de','0.5'], ['en','0.8'], ...]
124
- parts = locales.split(',')
125
- locales = [[]]
126
- parts.each do |part|
127
- locales.last << part.split(/;q=/)#add another language or language and weight
128
- locales += [] if part.length == 2 #if it could be split we are now in a new locale
129
- end
130
-
131
- locales = locales.sort_by{|l|l.last} #sort them by weight which is the last entry
132
- locales.flatten.each do |candidate|
133
- candidate = candidate.sub(/^([a-zA-Z]{2})[-_]([a-zA-Z]{2})$/){$1.downcase+'_'+$2.upcase}#de-de -> de_DE
134
- return candidate if not available_locales or available_locales.include?(candidate)
119
+ formatted_sorted_locales(locales).each do |candidate|
120
+ return candidate if not available_locales
121
+ return candidate if available_locales.include?(candidate)
122
+ return candidate[0..1] if available_locales.include?(candidate[0..1])#available locales include a langauge
135
123
  end
136
124
  return nil#nothing found im sorry :P
137
125
  end
@@ -144,6 +132,34 @@ module FastGettext
144
132
 
145
133
  private
146
134
 
135
+ # de-de,DE-CH;q=0.9 -> ['de_DE','de_CH']
136
+ def formatted_sorted_locales(locales)
137
+ found = weighted_locales(locales).reject{|x|x.empty?}.sort_by{|l|l.last}.reverse #sort them by weight which is the last entry
138
+ found.flatten.map{|l| format_locale(l)}
139
+ end
140
+
141
+ #split the locale and seperate it into different languages
142
+ #de-de,de;q=0.9,en;q=0.8 => [['de-de','de','0.5'], ['en','0.8']]
143
+ def weighted_locales(locales)
144
+ locales = locales.to_s.gsub(/\s/,'')
145
+ found = [[]]
146
+ locales.split(',').each do |part|
147
+ if part =~ /;q=/ #contains language and weight ?
148
+ found.last << part.split(/;q=/)
149
+ found.last.flatten!
150
+ found << []
151
+ else
152
+ found.last << part
153
+ end
154
+ end
155
+ found
156
+ end
157
+
158
+ #de-de -> de_DE
159
+ def format_locale(locale)
160
+ locale.sub(/^([a-zA-Z]{2})[-_]([a-zA-Z]{2})$/){$1.downcase+'_'+$2.upcase}
161
+ end
162
+
147
163
  def update_current_cache
148
164
  caches[text_domain] ||= {}
149
165
  caches[text_domain][locale] ||= {}
@@ -149,6 +149,18 @@ describe 'Storage' do
149
149
  FastGettext.locale.should == 'en'
150
150
  end
151
151
 
152
+ it "sets the locale with the highest wheight" do
153
+ FastGettext.available_locales = ['en','de']
154
+ FastGettext.locale = "xx-us;q=0.5,de-de,de;q=0.8,en;q=0.9"
155
+ FastGettext.locale.should == 'en'
156
+ end
157
+
158
+ it "sets the locale from languages" do
159
+ FastGettext.available_locales = ['de']
160
+ FastGettext.locale = "xx-us;q=0.5,de-de;q=0.8,en-uk;q=0.9"
161
+ FastGettext.locale.should == 'de'
162
+ end
163
+
152
164
  it "sets locale from comma seperated" do
153
165
  FastGettext.available_locales = ['de_DE','en','xx']
154
166
  FastGettext.locale = "de,de-de,en"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grosser-fast_gettext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.4.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-30 00:00:00 -07:00
12
+ date: 2009-06-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -68,7 +68,7 @@ files:
68
68
  - vendor/mofile.rb
69
69
  - vendor/poparser.rb
70
70
  - vendor/string.rb
71
- has_rdoc: true
71
+ has_rdoc: false
72
72
  homepage: http://github.com/grosser/fast_gettext
73
73
  post_install_message:
74
74
  rdoc_options:
@@ -92,7 +92,7 @@ requirements: []
92
92
  rubyforge_project:
93
93
  rubygems_version: 1.2.0
94
94
  signing_key:
95
- specification_version: 2
95
+ specification_version: 3
96
96
  summary: A simple, fast and threadsafe implementation of GetText
97
97
  test_files:
98
98
  - spec/fast_gettext_spec.rb