locale_detector 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/lib/locale_detector.rb +8 -0
- data/lib/locale_detector/fallback_locale.rb +5 -0
- data/lib/locale_detector/filter.rb +45 -0
- data/lib/locale_detector/railtie.rb +12 -0
- data/lib/locale_detector/version.rb +3 -0
- data/locale_detector.gemspec +28 -0
- data/spec/locale_detector/fallback_locale_spec.rb +9 -0
- data/spec/locale_detector/filter_spec.rb +46 -0
- data/spec/spec_helper.rb +9 -0
- metadata +175 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright © 2011 Exvo.com Development BV
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Locale Detector
|
2
|
+
|
3
|
+
This Rails gem makes use of the HTTP_ACCEPT_LANGUAGE http header sent by web browsers with every request, to set the `I18n.locale` setting.
|
4
|
+
When this fails (for example if there is no such http header - as is the case for Google bot), it will try to determine the locale based on the toplevel domain suffix (so it will set 'de' for the `example.de` domain).
|
5
|
+
When both fail, it will fall back to the `LocaleDetector.fallback_locale`, which is 'en' by default and can be overriden in `config/initializers/locale_detector.rb`.
|
6
|
+
|
7
|
+
The HTTP_ACCEPT_LANGUAGE header parser is based on:
|
8
|
+
https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language.rb
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Install the gem
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ gem install locale_detector
|
17
|
+
```
|
18
|
+
|
19
|
+
add it to the Gemfile
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'locale_detector'
|
23
|
+
```
|
24
|
+
|
25
|
+
and bundle
|
26
|
+
|
27
|
+
```bash
|
28
|
+
$ bundle
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
You can optionally overwrite the default fallback locale by creating a `config/initializers/locale_detector.rb` file and set the `fallback_locale` to a new string, for example:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
LocaleDetector.fallback_locale = 'pl'
|
36
|
+
```
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
Copyright © 2011 Exvo.com Development BV, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module LocaleDetector
|
2
|
+
module Filter
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
prepend_before_filter :set_locale
|
7
|
+
end
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def set_locale
|
12
|
+
I18n.locale = begin
|
13
|
+
request.env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l|
|
14
|
+
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
15
|
+
l.split(';q=')
|
16
|
+
end.sort do |x,y|
|
17
|
+
raise "Incorrect format" unless x.first =~ /^[a-z\-]+$/i
|
18
|
+
y.last.to_f <=> x.last.to_f
|
19
|
+
end.first.first.gsub(/-[a-z]+$/i, '').downcase
|
20
|
+
rescue # rescue (anything) from the malformed (or missing) accept language headers
|
21
|
+
|
22
|
+
# parse request.host and try to set the locale based on toplevel domain suffix (for netbots mostly)
|
23
|
+
suffix = request.host.split('.').last
|
24
|
+
|
25
|
+
# somewhat incomplete list of more common toplevel domains
|
26
|
+
if [:bg, :be, :cn, :cz, :da, :de, :es, :et, :fi, :fr, :gr, :hi, :hr, :hu, :is, :it, :jp, :ko, :lv, :lt,
|
27
|
+
:mk, :nl, :no, :pl, :pt, :ro, :ru, :se, :sr, :sk, :sl, :tr, :vi, :ua].include?(suffix.to_sym)
|
28
|
+
suffix
|
29
|
+
# some common international/English domains
|
30
|
+
elsif [:eu, :uk, :us].include?(suffix.to_sym)
|
31
|
+
'en'
|
32
|
+
# Spanish speaking countries
|
33
|
+
elsif [:ar, :cl, :co, :cu, :mx].include?(suffix.to_sym)
|
34
|
+
'es'
|
35
|
+
# Portuguese speaking countries
|
36
|
+
elsif suffix == 'br'
|
37
|
+
'pt'
|
38
|
+
# fall back to fallback_locale for .com and all other domains ('en' by default)
|
39
|
+
else
|
40
|
+
LocaleDetector.fallback_locale
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'locale_detector'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module LocaleDetector
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "locale_detector.prepend_before_filter" do
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
|
+
ActionController::Base.send(:include, LocaleDetector::Filter)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require "locale_detector/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "locale_detector"
|
8
|
+
s.version = LocaleDetector::VERSION
|
9
|
+
s.authors = ["Paweł Gościcki"]
|
10
|
+
s.email = ["pawel.goscicki@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/Exvo/locale_detector"
|
12
|
+
s.summary = "Rails gem setting the I18n.locale based on user's browser language"
|
13
|
+
s.description = "Parses HTTP_ACCEPT_LANGUAGE http header and sets the I18n.locale based on that"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.licenses = ['MIT']
|
21
|
+
|
22
|
+
s.add_dependency 'rails', ['>= 3.0.0']
|
23
|
+
s.add_dependency 'i18n', ['>= 0.5.0']
|
24
|
+
s.add_development_dependency 'guard', ['>= 0.5.0']
|
25
|
+
s.add_development_dependency 'guard-rspec', ['>= 0.4.0']
|
26
|
+
s.add_development_dependency 'rspec', ['>= 2.6']
|
27
|
+
s.add_development_dependency 'rspec-rails', ['>= 2.6']
|
28
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleDetector::Filter do
|
4
|
+
|
5
|
+
def set_locale(opts = {})
|
6
|
+
filter = Object.new.extend(LocaleDetector::Filter)
|
7
|
+
|
8
|
+
request = if opts[:language].present?
|
9
|
+
double('request', :env => { 'HTTP_ACCEPT_LANGUAGE' => opts[:language] })
|
10
|
+
else
|
11
|
+
double('request', :env => nil, :host => opts[:host])
|
12
|
+
end
|
13
|
+
|
14
|
+
filter.stub(:request).and_return(request)
|
15
|
+
filter.send(:set_locale)
|
16
|
+
end
|
17
|
+
|
18
|
+
context "http header locale setting" do
|
19
|
+
|
20
|
+
specify { set_locale(:language => 'pl').should eql('pl') }
|
21
|
+
|
22
|
+
specify { set_locale(:language => 'pl-PL').should eql('pl') }
|
23
|
+
|
24
|
+
specify { set_locale(:language => 'pl,en-us;q=0.7,en;q=0.3').should eql('pl') }
|
25
|
+
|
26
|
+
specify { set_locale(:language => 'lt,en-us;q=0.8,en;q=0.6,ru;q=0.4,pl;q=0.2').should eql('lt') }
|
27
|
+
|
28
|
+
specify { set_locale(:language => 'pl-PL;q=0.1,en-us;q=0.7,en;q=0.3').should eql('en') }
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "host based locale setting" do
|
33
|
+
|
34
|
+
specify { set_locale(:host => 'example.pl').should eql('pl') }
|
35
|
+
|
36
|
+
specify { set_locale(:host => 'example.co.uk').should eql('en') }
|
37
|
+
|
38
|
+
specify { set_locale(:host => 'example.mx').should eql('es') }
|
39
|
+
|
40
|
+
specify { set_locale(:host => 'example.br').should eql('pt') }
|
41
|
+
|
42
|
+
specify { set_locale(:host => 'example.com').should eql(LocaleDetector.fallback_locale) }
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: locale_detector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Pawe\xC5\x82 Go\xC5\x9Bcicki"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-08-11 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: i18n
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 11
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 5
|
49
|
+
- 0
|
50
|
+
version: 0.5.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: guard
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 11
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
- 5
|
65
|
+
- 0
|
66
|
+
version: 0.5.0
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 15
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
- 4
|
81
|
+
- 0
|
82
|
+
version: 0.4.0
|
83
|
+
type: :development
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: rspec
|
87
|
+
prerelease: false
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 15
|
94
|
+
segments:
|
95
|
+
- 2
|
96
|
+
- 6
|
97
|
+
version: "2.6"
|
98
|
+
type: :development
|
99
|
+
version_requirements: *id005
|
100
|
+
- !ruby/object:Gem::Dependency
|
101
|
+
name: rspec-rails
|
102
|
+
prerelease: false
|
103
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 15
|
109
|
+
segments:
|
110
|
+
- 2
|
111
|
+
- 6
|
112
|
+
version: "2.6"
|
113
|
+
type: :development
|
114
|
+
version_requirements: *id006
|
115
|
+
description: Parses HTTP_ACCEPT_LANGUAGE http header and sets the I18n.locale based on that
|
116
|
+
email:
|
117
|
+
- pawel.goscicki@gmail.com
|
118
|
+
executables: []
|
119
|
+
|
120
|
+
extensions: []
|
121
|
+
|
122
|
+
extra_rdoc_files: []
|
123
|
+
|
124
|
+
files:
|
125
|
+
- .gitignore
|
126
|
+
- .rspec
|
127
|
+
- Gemfile
|
128
|
+
- MIT-LICENSE
|
129
|
+
- README.md
|
130
|
+
- Rakefile
|
131
|
+
- lib/locale_detector.rb
|
132
|
+
- lib/locale_detector/fallback_locale.rb
|
133
|
+
- lib/locale_detector/filter.rb
|
134
|
+
- lib/locale_detector/railtie.rb
|
135
|
+
- lib/locale_detector/version.rb
|
136
|
+
- locale_detector.gemspec
|
137
|
+
- spec/locale_detector/fallback_locale_spec.rb
|
138
|
+
- spec/locale_detector/filter_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
has_rdoc: true
|
141
|
+
homepage: https://github.com/Exvo/locale_detector
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
hash: 3
|
155
|
+
segments:
|
156
|
+
- 0
|
157
|
+
version: "0"
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
hash: 3
|
164
|
+
segments:
|
165
|
+
- 0
|
166
|
+
version: "0"
|
167
|
+
requirements: []
|
168
|
+
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 1.6.2
|
171
|
+
signing_key:
|
172
|
+
specification_version: 3
|
173
|
+
summary: Rails gem setting the I18n.locale based on user's browser language
|
174
|
+
test_files: []
|
175
|
+
|