i18n_strategy 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/i18n_strategy.gemspec +26 -0
- data/lib/i18n_strategy.rb +4 -0
- data/lib/i18n_strategy/initializer.rb +27 -0
- data/lib/i18n_strategy/railtie.rb +7 -0
- data/lib/i18n_strategy/strategy.rb +21 -0
- data/lib/i18n_strategy/version.rb +3 -0
- data/spec/app/test_app.rb +25 -0
- data/spec/controllers/locale_spec.rb +101 -0
- data/spec/spec_helper.rb +6 -0
- metadata +146 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kentaro Kuribayashi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# i18n_strategy [](http://travis-ci.org/kentaro/i18n_strategy)
|
2
|
+
|
3
|
+
i18n_strategy provides a very much simple way to detect and set locale
|
4
|
+
in your Rails application.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Add a line below into your `Gemfile`:
|
9
|
+
|
10
|
+
```
|
11
|
+
gem 'i18n_strategy'
|
12
|
+
```
|
13
|
+
|
14
|
+
Then, set your custom strategy into `I18nStrategy.strategy`, which is
|
15
|
+
to detect a locale for a user visiting your application.
|
16
|
+
|
17
|
+
`initializers/i18n_strategy`:
|
18
|
+
|
19
|
+
```
|
20
|
+
module MyStrategy
|
21
|
+
def detect_locale
|
22
|
+
params[:locale] # just simple and fragile way
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
I18nStrategy.strategy = MyStrategy
|
27
|
+
I18nStrategy.available_languages = %w[ja en]
|
28
|
+
```
|
29
|
+
|
30
|
+
Users' locale detected by the strategy is automatically set to
|
31
|
+
`I18n.locale`.
|
32
|
+
|
33
|
+
That's all. Very much simple.
|
34
|
+
|
35
|
+
## Customization
|
36
|
+
|
37
|
+
### I18nStrategy.strategy
|
38
|
+
|
39
|
+
Set your own custom strategy module via this method. If not set,
|
40
|
+
[default strategy](./lib/i18n_strategy/strategy.rb) will be used.
|
41
|
+
|
42
|
+
The module must implement `detect_locale` method or some other one set
|
43
|
+
by `I18nStrategy.method_to_detect_locale` method described below.
|
44
|
+
|
45
|
+
### I18nStrategy.method_to_detect_locale
|
46
|
+
|
47
|
+
Set another method instead of default one, `detect_locale`.
|
48
|
+
|
49
|
+
The method to be used to detect users' locale will be called as a
|
50
|
+
instance method of a controller which is currently dispatched. That is
|
51
|
+
to say, it can duplicate your existing method.
|
52
|
+
|
53
|
+
In case above, you can set another method name to be called via this
|
54
|
+
method.
|
55
|
+
|
56
|
+
### I18nStrategy.available_languages
|
57
|
+
|
58
|
+
Set available language in your application.
|
59
|
+
|
60
|
+
The default strategy utilizes this method to detect whether the
|
61
|
+
language from users can be available in your application or not.
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'i18n_strategy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "i18n_strategy"
|
8
|
+
gem.version = I18nStrategy::VERSION
|
9
|
+
gem.authors = ["Kentaro Kuribayashi"]
|
10
|
+
gem.email = ["kentarok@gmail.com"]
|
11
|
+
gem.description = %q{Provides a very much simple way to detect and set locale in your Rails application}
|
12
|
+
gem.summary = %q{Provides a very much simple way to detect and set locale in your Rails application}
|
13
|
+
gem.homepage = "https://github.com/kentaro/i18n_strategy"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'rails', '>= 3.0.0'
|
21
|
+
gem.add_dependency 'http_accept_language', '>= 2.0.0pre'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'rspec'
|
25
|
+
gem.add_development_dependency 'rspec-rails'
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module I18nStrategy
|
2
|
+
mattr_accessor :strategy, :method_to_detect_locale, :available_languages
|
3
|
+
|
4
|
+
class Initializer
|
5
|
+
def self.init(app)
|
6
|
+
# Set I18nStrategy::Strategy::Default as default strategy
|
7
|
+
I18nStrategy.strategy ||= I18nStrategy::Strategy::Default
|
8
|
+
|
9
|
+
ActiveSupport.on_load(:action_controller) do
|
10
|
+
ActionController::Base.send(:include, Filter)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Filter
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
included do
|
19
|
+
append_before_filter :set_locale
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_locale
|
23
|
+
ActionController::Base.send(:include, I18nStrategy.strategy)
|
24
|
+
I18n.locale = send(I18nStrategy.method_to_detect_locale || :detect_locale)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'http_accept_language/parser'
|
2
|
+
|
3
|
+
module I18nStrategy
|
4
|
+
module Strategy
|
5
|
+
module Default
|
6
|
+
def detect_locale
|
7
|
+
lang = nil
|
8
|
+
available = I18nStrategy.available_languages || []
|
9
|
+
|
10
|
+
if params[:locale] && available.include?(params[:locale])
|
11
|
+
lang = params[:locale]
|
12
|
+
else
|
13
|
+
parser = HttpAcceptLanguage::Parser.new(request.env['HTTP_ACCEPT_LANGUAGE'])
|
14
|
+
lang = parser.preferred_language_from(available)
|
15
|
+
end
|
16
|
+
|
17
|
+
lang || I18n.default_locale
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'action_controller/railtie'
|
2
|
+
|
3
|
+
module I18nStrategyTestApp
|
4
|
+
class Application < Rails::Application
|
5
|
+
config.default_locale = 'en'
|
6
|
+
config.active_support.deprecation = :log
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
I18nStrategyTestApp::Application.initialize!
|
11
|
+
|
12
|
+
# routes
|
13
|
+
I18nStrategyTestApp::Application.routes.draw do
|
14
|
+
root :to => 'root#index'
|
15
|
+
end
|
16
|
+
|
17
|
+
# controllers
|
18
|
+
class ApplicationController < ActionController::Base
|
19
|
+
end
|
20
|
+
|
21
|
+
class RootController < ApplicationController
|
22
|
+
def index
|
23
|
+
render :text => I18n.locale
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RootController do
|
4
|
+
before {
|
5
|
+
I18nStrategy.strategy = I18nStrategy::Strategy::Default
|
6
|
+
I18nStrategy.available_languages = %w[en ja fr]
|
7
|
+
}
|
8
|
+
|
9
|
+
describe 'GET /' do
|
10
|
+
context 'with no preferred language' do
|
11
|
+
before { get :index }
|
12
|
+
|
13
|
+
it {
|
14
|
+
expect(response.body).to be == 'en'
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with preferred language' do
|
19
|
+
context 'en' do
|
20
|
+
before {
|
21
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en'
|
22
|
+
get :index
|
23
|
+
}
|
24
|
+
|
25
|
+
it {
|
26
|
+
expect(response.body).to be == 'en'
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'ja' do
|
31
|
+
before {
|
32
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
33
|
+
get :index
|
34
|
+
}
|
35
|
+
|
36
|
+
it {
|
37
|
+
expect(response.body).to be == 'ja'
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'with preferred language from query param' do
|
43
|
+
context 'with valid language' do
|
44
|
+
before {
|
45
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
46
|
+
get :index, :locale => 'fr'
|
47
|
+
}
|
48
|
+
|
49
|
+
it {
|
50
|
+
expect(response.body).to be == 'fr'
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'with invalid language' do
|
55
|
+
before {
|
56
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
57
|
+
get :index, :locale => 'no_such_language'
|
58
|
+
}
|
59
|
+
|
60
|
+
it {
|
61
|
+
expect(response.body).to be == 'ja'
|
62
|
+
}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'with custom strategy' do
|
67
|
+
module MyStrategy
|
68
|
+
def detect_locale
|
69
|
+
'foo'
|
70
|
+
end
|
71
|
+
|
72
|
+
def another_detect_locale
|
73
|
+
'bar'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'with no preferred method' do
|
78
|
+
before {
|
79
|
+
I18nStrategy.strategy = MyStrategy
|
80
|
+
get :index
|
81
|
+
}
|
82
|
+
|
83
|
+
it {
|
84
|
+
expect(response.body).to be == 'foo'
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'with preferred method' do
|
89
|
+
before {
|
90
|
+
I18nStrategy.strategy = MyStrategy
|
91
|
+
I18nStrategy.method_to_detect_locale = :another_detect_locale
|
92
|
+
get :index
|
93
|
+
}
|
94
|
+
|
95
|
+
it {
|
96
|
+
expect(response.body).to be == 'bar'
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n_strategy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kentaro Kuribayashi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: http_accept_language
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.0.0pre
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.0.0pre
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec-rails
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Provides a very much simple way to detect and set locale in your Rails
|
95
|
+
application
|
96
|
+
email:
|
97
|
+
- kentarok@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .rspec
|
104
|
+
- .travis.yml
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- i18n_strategy.gemspec
|
110
|
+
- lib/i18n_strategy.rb
|
111
|
+
- lib/i18n_strategy/initializer.rb
|
112
|
+
- lib/i18n_strategy/railtie.rb
|
113
|
+
- lib/i18n_strategy/strategy.rb
|
114
|
+
- lib/i18n_strategy/version.rb
|
115
|
+
- spec/app/test_app.rb
|
116
|
+
- spec/controllers/locale_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
homepage: https://github.com/kentaro/i18n_strategy
|
119
|
+
licenses: []
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ! '>='
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 1.8.23
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Provides a very much simple way to detect and set locale in your Rails application
|
142
|
+
test_files:
|
143
|
+
- spec/app/test_app.rb
|
144
|
+
- spec/controllers/locale_spec.rb
|
145
|
+
- spec/spec_helper.rb
|
146
|
+
has_rdoc:
|