rack-i18n_locale_switcher 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,49 +1,86 @@
1
- # Rack::I18nLocaleSwitcher
1
+ # Rack I18n Locale Switcher
2
2
 
3
3
  [![alt text][2]][1]
4
4
 
5
5
  [1]: http://travis-ci.org/#!/christoph-buente/rack-i18n_locale_switcher
6
6
  [2]: https://secure.travis-ci.org/christoph-buente/rack-i18n_locale_switcher.png
7
7
 
8
- This Rack middleware determines the I18n language from various sources.
8
+ This Rack middleware sets the I18n locale from the requested URL.
9
9
 
10
+ The locale can be determined from four different sources: host name, path prefix, query parameter and the Accept-Language header.
10
11
 
12
+ Each of the following URLs will set the locale to 'es' (Spanish):
11
13
 
12
- A language or locale, can be encode within an HTTP requests in various ways. Let's have a look at the following request url:
14
+ http://es.example.org
15
+ http://example.org/es
16
+ http://example.org?locale=es
17
+
18
+ as will the following request:
13
19
 
14
- http://es.example.it/tr/?locale=de
20
+ GET / HTTP/1.1
21
+ Host: example.org
22
+ Accept-Language: es
23
+
15
24
 
16
- To extract the correct language from such a request, you probably don't want to hardcode it into you application. This middleware extracts the desired locale from this request in the following order:
17
-
18
- * request parameter (de, german)
19
- * url path (tr, turkish)
20
- * subdomain (es, spanish)
21
- * top level domain (it, italian)
22
- * HTTP Accept-language header (not visible in the url, but could be something completely different)
23
-
24
- Sound's good, gimme the code!
25
-
26
- ## Rails
25
+ ### Using it with Rails
27
26
 
28
27
  # file application.rb
29
28
 
30
29
  require 'rack/i18n_locale_switcher'
31
- config.middleware.use(Rack::I18nLocaleSwitcher)
30
+ config.middleware.use Rack::I18nLocaleSwitcher
32
31
 
33
- ## Sinatra
32
+ ### Using it with Sinatra
34
33
 
35
34
  require 'rack/i18n_locale_switcher'
36
- use Rack::I18nLocaleSwitcher, :i18n_locale_switcher
35
+ use Rack::I18nLocaleSwitcher
36
+
37
+
38
+ ## Configuration options
39
+
40
+
41
+ ### Source
42
+
43
+ The sources from which the locale is determined and the order in which they are probed can be specified with the `source` option. By default this is `[ :param, :path, :host, :header ]`. In case ambiguous locales are provided such as in `en.example.org/de`, the path (de) will have precedence over the host (en), due to its higher order.
44
+
45
+ If you want to support only one source, which is strongly encouraged, you can configure the middleware using the `source` option:
46
+
47
+ use Rack::I18nLocaleSwitcher, :source => [ :path, :header ]
48
+
49
+ You might always want to include `header` as the last option. In case no locale was specified in the URL (i.e. `http://example.org`) and you would otherwise fall back to the default locale, the user preferred locale as indicated in the Accept-Language header will always be the better choice.
50
+
51
+
52
+ ### Redirect
53
+
54
+ If you allow more than one source (because you have to support a legacy URL scheme, for example) you can choose to redirect requests to the preferred scheme. Redirect is not enabled by default.
55
+
56
+ use Rack::I18nLocaleSwitcher, :source => [ :path, :param, :header ], :redirect => :path
57
+
58
+ With this setup, a request to `https://example.org?locale=es` would be permanently redirected to `https://example.org/es`.
59
+
60
+
61
+ ### Canonical URLs
62
+
63
+ Many sites have a default locale which is not part of the URL. Only if a different locale is requested, it will be indicated in the URL. For example, `www.apple.com` uses English by default while other languages have a prefix to the path such as `www.apple.com/jp/` for Japanese.
64
+
65
+ If no locale can be determined from the URL, the locale switcher falls back to the `I18n.default_locale`. However, depending on your application, this might result in the default locale to be available under two different URLs, such as `http://example.org` and `http://example.org/en`. The problem with this is mainly that search engines will see both URLs and consider it duplicate content resulting in your page rank taking a penalty.
66
+
67
+ To avoid this, you can set the `canonical` option resulting in requests to be redirected to the canonical URL (without the locale):
68
+
69
+ use Rack::I18nLocaleSwitcher, :source => [ :host, :header ], :redirect => :path, :canonical => true
70
+
71
+ In this configuration, requests to `http://en.example.org` will be redirected to `http://example.org` (provided you have set `I18n.default_locale` to `:en`).
37
72
 
38
73
 
39
- Q: Ok, but where does the locale go?
74
+ ## Configuring I18n
40
75
 
41
- A: The middleware uses the ruby internationalization gem [i18n](http://rubygems.org/gems/i18n), which stores the locale in a variable. You can use this variable in your app as
76
+ You have to define which locales are actually supported by your application. In Rails this happens automagically, in other application you have to set the available locales explicitely:
42
77
 
43
- I18n.locale
78
+ I18n.available_locales = [:de, :en, :es, :it, :tr ]
79
+
80
+ You should also set the default locale to which Locale Switcher will fall back in case the locale can't be determined. This setting is also important if you use canonical URLs (see above).
44
81
 
45
- Q: How can I define, which locales are available?
82
+ I18n.default_locale = :de
46
83
 
47
- A: You can set the available locales before using instantiation the middleware.
84
+ ## Feedback and Contributions
48
85
 
49
- I18n.available_locales = [:de, :en, :es, :it, :tr ]
86
+ We appreciate your feedback and contributions. If you find a bug, feel free to to open a GitHub issue. Better yet, add a test that exposes the bug, fix it and send us a pull request.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('rack-i18n_locale_switcher', '0.5.0') do |p|
5
+ Echoe.new('rack-i18n_locale_switcher', '0.5.1') do |p|
6
6
 
7
7
  p.description = "Detects the current locale from query parameter, path prefix, host or accept header."
8
8
  p.url = "http://github.com/christoph-buente/rack-i18n_locale_switcher"
@@ -3,7 +3,7 @@ require 'i18n'
3
3
  module Rack
4
4
  class I18nLocaleSwitcher
5
5
 
6
- LOCALE_PATTERN = '([a-z]{1,8})(-[a-zA-Z]{1,8})?'.freeze
6
+ LOCALE_PATTERN = '([a-zA-Z]{2,3})(-[a-zA-Z]{2,3})?'.freeze
7
7
 
8
8
  SOURCES = [ :param, :path, :host, :header ].freeze
9
9
  REDIRECTS = [ :param, :path, :host ].freeze
@@ -125,14 +125,12 @@ module Rack
125
125
  end
126
126
 
127
127
  def set_locale_in_path(env)
128
- env['PATH_INFO'] = "/#{ I18n.locale }#{ env['PATH_INFO'] }"
129
- env['PATH_INFO'].gsub!(/\/$/, '')
128
+ env['PATH_INFO'] = "/#{ I18n.locale }#{ env['PATH_INFO'] }".gsub(/\/$/, '')
130
129
  end
131
130
 
132
131
  def set_locale_in_host(env)
133
- ['HTTP_HOST', 'SERVER_NAME'].each do |key|
134
- env[ key ] = "#{ I18n.locale }.#{ env[ key ] }"
135
- end
132
+ env['HTTP_HOST'] = "#{ I18n.locale }.#{ env['HTTP_HOST'] }"
133
+ env['SERVER_NAME'] = "#{ I18n.locale }.#{ env['SERVER_NAME'] }"
136
134
  end
137
135
 
138
136
  def available_locale(language, region)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rack-i18n_locale_switcher}
5
- s.version = "0.5.0"
5
+ s.version = "0.5.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Christoph B\303\274nte, Andreas Korth"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-i18n_locale_switcher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 1
10
+ version: 0.5.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Christoph B\xC3\xBCnte, Andreas Korth"