rack-user-locale 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3-p362@rack-user-locale
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "rack"
7
+ gem "i18n"
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "bundler"
13
+ gem 'debugger'
14
+ gem "jeweler"
15
+ gem "minitest"
16
+ gem "rack-test"
17
+ gem "turn"
18
+ gem "yard"
19
+ end
@@ -0,0 +1,44 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ansi (1.4.3)
5
+ columnize (0.3.6)
6
+ debugger (1.3.0)
7
+ columnize (>= 0.3.1)
8
+ debugger-linecache (~> 1.1.1)
9
+ debugger-ruby_core_source (~> 1.1.7)
10
+ debugger-linecache (1.1.2)
11
+ debugger-ruby_core_source (>= 1.1.1)
12
+ debugger-ruby_core_source (1.1.7)
13
+ git (1.2.5)
14
+ i18n (0.6.1)
15
+ jeweler (1.8.4)
16
+ bundler (~> 1.0)
17
+ git (>= 1.2.5)
18
+ rake
19
+ rdoc
20
+ json (1.7.6)
21
+ minitest (4.5.0)
22
+ rack (1.5.1)
23
+ rack-test (0.6.2)
24
+ rack (>= 1.0)
25
+ rake (10.0.3)
26
+ rdoc (3.12)
27
+ json (~> 1.4)
28
+ turn (0.9.6)
29
+ ansi
30
+ yard (0.8.3)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler
37
+ debugger
38
+ i18n
39
+ jeweler
40
+ minitest
41
+ rack
42
+ rack-test
43
+ turn
44
+ yard
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Stuart Chinery
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ rack-user-locale
2
+ ----------------
3
+
4
+ A Rack module for getting and setting a user's locale via a cookie or browser default language.
5
+
6
+ Credit to these gems for pointing me in the direction I wanted to go in...
7
+
8
+ https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb
9
+ https://github.com/jeremyvdw/rack-locale-selector
10
+ https://github.com/jeffersongirao/rack-locale_chooser
11
+
12
+ Installation
13
+ ============
14
+
15
+ ```
16
+ gem install rack-user-locale
17
+ ```
18
+
19
+ In a Gemfile:
20
+
21
+ ```ruby
22
+ gem 'rack-user-locale'
23
+ ```
24
+
25
+ Usage
26
+ =====
27
+
28
+ Add the following line to your Rack application
29
+
30
+ ```
31
+ use Rack::UserLocale
32
+ ```
33
+
34
+ and thats it!!
35
+
36
+ Your application will now attempt to set the I18n.locale value based on the following:
37
+
38
+ * Whether a user has a "user-locale" cookie set
39
+ * The "HTTP_ACCEPT_LANGUAGE" value
40
+ * The I18n.default_locale value (basically a fallback)
41
+
42
+ Should you wish to overwrite a users locale value the simple rewrite the "user-locale" cookie.
43
+
44
+ Contributing to rack-user-locale
45
+ =======================
46
+
47
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
48
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
49
+ * Fork the project.
50
+ * Start a feature/bugfix branch.
51
+ * Commit and push until you are happy with your contribution.
52
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
53
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
54
+
55
+ Copyright
56
+ =========
57
+
58
+ Copyright (c) 2013 [Stuart Chinery](http://www.headlondon.com/who-we-are#stuart-chinery) and [Dave Hrycyszyn](http://www.headlondon.com/who-we-are#david-hrycyszyn) - [headlondon.com](http://www.headlondon.com).
59
+
60
+ See LICENSE.txt for further details.
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rack-user-locale"
18
+ gem.homepage = "http://github.com/sleepingstu/rack-user-locale"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Rack module for getting and setting a user's locale}
21
+ gem.description = %Q{A Rack module for getting and setting a user's locale via a cookie or browser default language.}
22
+ gem.email = "stuart.chinery@headlondon.com"
23
+ gem.authors = ["Stuart Chinery", "Dave Hrycyszyn"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'yard'
38
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,62 @@
1
+ require "i18n"
2
+
3
+ module Rack
4
+ class UserLocale
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ @env = env
12
+ @request = Rack::Request.new(@env)
13
+ set_locale
14
+
15
+ if @request.post? || @request.put? || @request.delete?
16
+ @app.call(env)
17
+ else
18
+ status, headers, body = @app.call(@env)
19
+ response = Rack::Response.new(body, status, headers)
20
+ response.set_cookie("user-locale", {
21
+ :value => I18n.locale,
22
+ :path => "/",
23
+ :domain => @request.host}) if get_cookie_locale != I18n.locale.to_s
24
+ response.finish
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def set_locale
31
+ I18n.locale = @env["rack.locale"] = locale.to_sym
32
+ end
33
+
34
+ def locale
35
+ get_cookie_locale || get_browser_locale || get_default_locale
36
+ end
37
+
38
+ def get_cookie_locale
39
+ @request.cookies["user-locale"]
40
+ end
41
+
42
+ def get_browser_locale
43
+ accept_langs = @env["HTTP_ACCEPT_LANGUAGE"]
44
+ return if accept_langs.nil?
45
+
46
+ languages_and_qvalues = accept_langs.split(",").map { |l|
47
+ l += ";q=1.0" unless l =~ /;q=\d+(?:\.\d+)?$/
48
+ l.split(";q=")
49
+ }
50
+
51
+ lang = languages_and_qvalues.sort_by { |(locale, qvalue)|
52
+ qvalue.to_f
53
+ }.last.first
54
+
55
+ lang == "*" ? nil : lang
56
+ end
57
+
58
+ def get_default_locale
59
+ I18n.default_locale
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require "rack/test"
4
+ require "debugger"
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'minitest/autorun'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'rack-user-locale'
18
+ begin; require 'turn/autorun'; rescue LoadError; end
19
+
20
+ class MiniTest::Unit::TestCase
21
+ include Rack::Test::Methods
22
+
23
+ def app
24
+ app = Rack::Builder.new {
25
+ use Rack::UserLocale
26
+
27
+ run BasicRackApp.new
28
+ }
29
+ end
30
+ end
31
+
32
+ class BasicRackApp
33
+ def call(env)
34
+ [200, {'Content-Type' => 'text/plain'}, 'Hello World']
35
+ end
36
+ end
37
+
38
+ MiniTest::Unit.autorun
@@ -0,0 +1,67 @@
1
+ require 'helper'
2
+
3
+ describe "RackUserLocale" do
4
+ before do
5
+ I18n.default_locale = :en
6
+ end
7
+
8
+ it "should have I18n.locale set to :en initially" do
9
+ assert_equal :en, I18n.locale
10
+ end
11
+
12
+ describe "when a locale cookie is set" do
13
+ before do
14
+ get 'http://example.com/', {}, 'HTTP_COOKIE' => 'user-locale=es'
15
+ end
16
+
17
+ it "should have I18n.locale set to :es" do
18
+ assert_equal :es, I18n.locale
19
+ end
20
+
21
+ it "should not set a cookie in the response" do
22
+ assert_equal nil, last_response["Set-Cookie"]
23
+ end
24
+ end
25
+
26
+ describe "when from HTTP_ACCEPT_LANGUAGE headers" do
27
+ before do
28
+ get 'http://example.com/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'ru'
29
+ end
30
+
31
+ it "should have I18n.locale set to :ru" do
32
+ assert_equal :ru, I18n.locale
33
+ end
34
+
35
+ it "should set a cookie in the response" do
36
+ assert_equal "user-locale=ru; domain=example.com; path=/", last_response["Set-Cookie"]
37
+ end
38
+ end
39
+
40
+ describe "when both a cooke and HTTP_ACCEPT_LANGUAGE headers are set" do
41
+ before do
42
+ get 'http://example.com/', {}, 'HTTP_COOKIE' => 'user-locale=jp', 'HTTP_ACCEPT_LANGUAGE' => 'fr'
43
+ end
44
+
45
+ it "should have I18n.locale set to :jp" do
46
+ assert_equal :jp, I18n.locale
47
+ end
48
+
49
+ it "should not set a cookie in the response" do
50
+ assert_equal nil, last_response["Set-Cookie"]
51
+ end
52
+ end
53
+
54
+ describe "when nothing is changed" do
55
+ before do
56
+ get 'http://example.com/'
57
+ end
58
+
59
+ it "should have I18n.locale set to :en" do
60
+ assert_equal :en, I18n.locale
61
+ end
62
+
63
+ it "should set a cookie in the response" do
64
+ assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-user-locale
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stuart Chinery
9
+ - Dave Hrycyszyn
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-02-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: i18n
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: debugger
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: jeweler
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: minitest
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack-test
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: turn
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ - !ruby/object:Gem::Dependency
144
+ name: yard
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ description: A Rack module for getting and setting a user's locale via a cookie or
160
+ browser default language.
161
+ email: stuart.chinery@headlondon.com
162
+ executables: []
163
+ extensions: []
164
+ extra_rdoc_files:
165
+ - LICENSE.txt
166
+ - README.md
167
+ files:
168
+ - .document
169
+ - .rvmrc
170
+ - Gemfile
171
+ - Gemfile.lock
172
+ - LICENSE.txt
173
+ - README.md
174
+ - Rakefile
175
+ - VERSION
176
+ - lib/rack-user-locale.rb
177
+ - test/helper.rb
178
+ - test/test_rack-user-locale.rb
179
+ homepage: http://github.com/sleepingstu/rack-user-locale
180
+ licenses:
181
+ - MIT
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ none: false
188
+ requirements:
189
+ - - ! '>='
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ segments:
193
+ - 0
194
+ hash: 981042501419718969
195
+ required_rubygems_version: !ruby/object:Gem::Requirement
196
+ none: false
197
+ requirements:
198
+ - - ! '>='
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ requirements: []
202
+ rubyforge_project:
203
+ rubygems_version: 1.8.24
204
+ signing_key:
205
+ specification_version: 3
206
+ summary: Rack module for getting and setting a user's locale
207
+ test_files: []