grosser-fast_gettext 0.2.8 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,10 @@ Setup
8
8
  =====
9
9
  sudo gem install grosser-fast_gettext -s http://gems.github.com/
10
10
 
11
+ Or from source:
12
+ git clone git://github.com/grosser/fast_gettext.git
13
+ cd fast_gettext && rake install
14
+
11
15
  Tell Gettext where your mo-files lie:
12
16
  #e.g. for locale/de/LC_MESSAGES/my_app.mo
13
17
  FastGettext.add_text_domain('my_app',:path=>'locale')
@@ -70,7 +74,7 @@ then e.g. controllers, so set them inside your application_controller.
70
74
  def set_locale
71
75
  FastGettext.available_locales = ['de','en',...]
72
76
  FastGettext.text_domain = 'frontend'
73
- sessions[:locale] = I18n.locale = FastGettext.set_locale(params[:locale] || sessions[:locale] || 'en')
77
+ sessions[:locale] = I18n.locale = FastGettext.set_locale(params[:locale] || sessions[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
74
78
  end
75
79
 
76
80
  #application_helper.rb
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 8
4
+ :patch: 10
@@ -37,8 +37,8 @@ module FastGettext
37
37
  end
38
38
 
39
39
  def locale=(new_locale)
40
- new_locale = new_locale.to_s
41
- if not available_locales or available_locales.include?(new_locale)
40
+ new_locale = best_locale_in(new_locale)
41
+ if new_locale
42
42
  write_thread_store(:locale,new_locale)
43
43
  update_current_translations
44
44
  end
@@ -52,6 +52,30 @@ module FastGettext
52
52
  locale
53
53
  end
54
54
 
55
+ #Opera: de-DE,de;q=0.9,en;q=0.8
56
+ #Firefox de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
57
+ #IE6/7 de
58
+ #nil if nothing matches
59
+ def best_locale_in(locales)
60
+ locales = locales.to_s.gsub(/\s/,'')
61
+
62
+ #split the locale and seperate it into different languages
63
+ #[['de-de','de','0.5'], ['en','0.8'], ...]
64
+ parts = locales.split(',')
65
+ locales = [[]]
66
+ parts.each do |part|
67
+ locales.last << part.split(/;q=/)#add another language or language and weight
68
+ locales += [] if part.length == 2 #if it could be split we are now in a new locale
69
+ end
70
+
71
+ locales.sort!(&:last) #sort them by weight which is the last entry
72
+ locales.flatten.each do |candidate|
73
+ candidate = candidate.sub(/^([a-zA-Z]{2})[-_]([a-zA-Z]{2})$/){$1.downcase+'_'+$2.upcase}#de-de -> de_DE
74
+ return candidate if not available_locales or available_locales.include?(candidate)
75
+ end
76
+ return nil#nothing found im sorry :P
77
+ end
78
+
55
79
  #turn off translation if none was defined to disable all resulting errors
56
80
  def silence_errors
57
81
  if not self.current_translations or self.current_translations == NoTextDomainConfigured
@@ -39,6 +39,10 @@ describe Storage do
39
39
  self.available_locales = nil
40
40
  locale.should == 'en'
41
41
  end
42
+ it "does not change the locale if locales was called with nil" do
43
+ self.locale = nil
44
+ locale.should == 'en'
45
+ end
42
46
  it "is the first available_locale if one was set" do
43
47
  self.available_locales = ['de']
44
48
  locale.should == 'de'
@@ -53,6 +57,27 @@ describe Storage do
53
57
  self.available_locales = ['de']
54
58
  self.set_locale('en').should == 'de'
55
59
  end
60
+ {
61
+ 'Opera' => "de-DE,de;q=0.9,en;q=0.8",
62
+ 'Firefox' => "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3",
63
+ }.each do |browser,accept_language|
64
+ it "sets the locale from #{browser} headers" do
65
+ FastGettext.available_locales = ['de_DE','de','xx']
66
+ FastGettext.locale = 'xx'
67
+ FastGettext.locale = accept_language
68
+ FastGettext.locale.should == 'de_DE'
69
+ end
70
+ end
71
+ it "sets a unimportant locale if it is the only available" do
72
+ FastGettext.available_locales = ['en','xx']
73
+ FastGettext.locale = "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"
74
+ FastGettext.locale.should == 'en'
75
+ end
76
+ it "sets locale from comma seperated" do
77
+ FastGettext.available_locales = ['de_DE','en','xx']
78
+ FastGettext.locale = "de,de-de,en"
79
+ FastGettext.locale.should == 'de_DE'
80
+ end
56
81
  end
57
82
 
58
83
  describe :silence_errors do
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.2.8
4
+ version: 0.2.10
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-02-24 00:00:00 -08:00
12
+ date: 2009-02-26 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15