simplificator_infrastructure 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +73 -0
- data/Rakefile +35 -0
- data/app/assets/images/simplificator_infrastructure/errors/error_404.png +0 -0
- data/app/assets/images/simplificator_infrastructure/errors/error_generic.png +0 -0
- data/app/assets/images/simplificator_infrastructure/errors/logo.png +0 -0
- data/app/assets/javascripts/simplificator_infrastructure/application.js +13 -0
- data/app/assets/stylesheets/simplificator_infrastructure/application.css +17 -0
- data/app/assets/stylesheets/simplificator_infrastructure/errors.css +39 -0
- data/app/controllers/simplificator_infrastructure/application_controller.rb +4 -0
- data/app/controllers/simplificator_infrastructure/errors_controller.rb +47 -0
- data/app/helpers/simplificator_infrastructure/application_helper.rb +4 -0
- data/app/views/errors/404.html.erb +9 -0
- data/app/views/errors/generic_error.html.erb +9 -0
- data/app/views/layouts/simplificator_infrastructure/errors.html.erb +12 -0
- data/config/locales/de.yml +13 -0
- data/config/locales/en.yml +13 -0
- data/config/routes.rb +2 -0
- data/lib/simplificator_infrastructure.rb +4 -0
- data/lib/simplificator_infrastructure/engine.rb +8 -0
- data/lib/simplificator_infrastructure/error_page_handler.rb +21 -0
- data/lib/simplificator_infrastructure/error_summary.rb +61 -0
- data/lib/simplificator_infrastructure/version.rb +3 -0
- data/lib/tasks/simplificator_infrastructure_tasks.rake +4 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/error_previews_controller.rb +15 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/foos_helper.rb +2 -0
- data/test/dummy/app/views/error_previews/index.html.erb +6 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +32 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +38 -0
- data/test/dummy/config/environments/production.rb +76 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/simplificator_infrastructure.rb +1 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +9 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +9 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/log/development.log +2036 -0
- data/test/dummy/log/test.log +15 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/controllers/foos_controller_test.rb +7 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/0500adeee201d5a348b4c3ec497adae2 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/09ed1ef41f7cd9d17777b1411359e566 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/1344657b8f5d1906efcbafe0e22c17cc +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/5acaff767402e80bb9961b92c8bc3dda +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/6d07f71bc133b650e297569a7e19174e +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/7e3d3b1b8b95a568bcb68e2f29436ca3 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/7f0315cb615b6ab601fd42d59b8afde9 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/8861585cc3fb654bbf27d60c86aba449 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/9c500634b58fe570e801dd477f2f9769 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/9e70666db09ef523b119125cc9eb73c5 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/a58c29f74df199333ef9f066c7b436bd +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/d7cb110ec211a8c4ef021e93a2f976f7 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/e303fbe4b122fbe771c62af737c0e1b5 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/e96855d0fc8137ba701522baf08faf33 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/f39692dced32f5d36d10caed783739ee +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/integration/navigation_test.rb +9 -0
- data/test/simplificator_infrastructure_test.rb +7 -0
- data/test/test_helper.rb +17 -0
- metadata +210 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e5b01844361849db3b53d7fa1a7cd6e17029bfe8
|
4
|
+
data.tar.gz: b2a4ee3dd65383d3f54351a2ad4cefafcaaf705d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03584be764cda1872bb3dd01e15055f8ded7975e7ea5dfb6c21c4a533affcc4658419a34c58d86af4b17cdd847ff6cd09b60262ca972e128239b58e0087d4be7
|
7
|
+
data.tar.gz: b535999662ed60bd16a9d9915042205d49cec3172e7068deafa9a9587ade7d660705aab9c1117dbc3585a7e0e65e2c390cd4dac49e8c9c18c9e3c10f101cade3
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Pascal Betz
|
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.rdoc
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
= SimplificatorInfrastructure
|
2
|
+
|
3
|
+
This project rocks and uses MIT-LICENSE.
|
4
|
+
|
5
|
+
|
6
|
+
SimplificatorInfrastructure provides re-usable code for Rails Webapps.
|
7
|
+
|
8
|
+
== Error Pages
|
9
|
+
|
10
|
+
Shows neat error pages when exceptions are raised.
|
11
|
+
It is implemented as exception app which is a special rack app which gets called when an error occurs inside the rack middleware chain.
|
12
|
+
|
13
|
+
|
14
|
+
=== Installation
|
15
|
+
|
16
|
+
````
|
17
|
+
gem 'simplificator_infrastructure'
|
18
|
+
````
|
19
|
+
|
20
|
+
|
21
|
+
=== Usage
|
22
|
+
|
23
|
+
In order to enable the error page handling do something like this:
|
24
|
+
|
25
|
+
````
|
26
|
+
SimplificatorInfrastructure::ErrorPageHandler.register
|
27
|
+
|
28
|
+
# As this will hide useful information for developers
|
29
|
+
# you might want to do this depending on the environment
|
30
|
+
if Rails.env.production?
|
31
|
+
SimplificatorInfrastructure::ErrorPageHandler.register
|
32
|
+
end
|
33
|
+
|
34
|
+
````
|
35
|
+
|
36
|
+
Note: it's best if you do this in an after_initialize block or in an initializer file (initializers/*.rb) because
|
37
|
+
in order for the error pages to work consider_all_requests_local needs to be false.
|
38
|
+
Some environments set it to true which might override this again because of the order of the calls.
|
39
|
+
|
40
|
+
=== Customize Error pages
|
41
|
+
|
42
|
+
You can add an error page per HTTP status code. I.e. a custom 500, 404, ... error page. The gem comes with
|
43
|
+
|
44
|
+
* 404 (is shown when a routing error occurs)
|
45
|
+
* generic (is shown in all other cases)
|
46
|
+
|
47
|
+
If a specific page is present then it is rendered, else the generic one is shown.
|
48
|
+
|
49
|
+
To add or customize an error page you can add a view at
|
50
|
+
|
51
|
+
app/views/errors/xxx.html.something
|
52
|
+
|
53
|
+
where XXX is the status code
|
54
|
+
|
55
|
+
Or add
|
56
|
+
|
57
|
+
app/views/errors/generic_error.html.erb to replace the generic error view.
|
58
|
+
|
59
|
+
=== Translations
|
60
|
+
|
61
|
+
Translations are provided for
|
62
|
+
|
63
|
+
* de
|
64
|
+
* en
|
65
|
+
|
66
|
+
If you want to add more translations just add the keys to your locale files.
|
67
|
+
|
68
|
+
Please make sure that I18n has
|
69
|
+
|
70
|
+
* available_locales
|
71
|
+
* default_locale
|
72
|
+
|
73
|
+
properly set.
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SimplificatorInfrastructure'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
load 'rails/tasks/statistics.rake'
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
|
27
|
+
Rake::TestTask.new(:test) do |t|
|
28
|
+
t.libs << 'lib'
|
29
|
+
t.libs << 'test'
|
30
|
+
t.pattern = 'test/**/*_test.rb'
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
task default: :test
|
Binary file
|
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
16
|
+
|
17
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
body.simplificator_infrastrcture.errors {
|
2
|
+
background-color: #fff;
|
3
|
+
text-align: center;
|
4
|
+
font-family: 'Helvetica Neue', helvetica, arial sans-serif;
|
5
|
+
}
|
6
|
+
|
7
|
+
body.simplificator_infrastrcture.errors h1 {
|
8
|
+
color: #646464;
|
9
|
+
font-size: 75px;
|
10
|
+
font-weight: 300;
|
11
|
+
}
|
12
|
+
|
13
|
+
body.simplificator_infrastrcture.errors a {
|
14
|
+
color: #646464;
|
15
|
+
}
|
16
|
+
|
17
|
+
body.simplificator_infrastrcture.errors p.lead {
|
18
|
+
font-size: 30px;
|
19
|
+
font-weight: 300;
|
20
|
+
margin-bottom: 10px;
|
21
|
+
line-height: 45px;
|
22
|
+
color: #333333;
|
23
|
+
}
|
24
|
+
|
25
|
+
body.simplificator_infrastrcture.errors p {
|
26
|
+
font-size: 16px;
|
27
|
+
margin-bottom: 40px;
|
28
|
+
line-height: 26px;
|
29
|
+
color: #888;
|
30
|
+
}
|
31
|
+
|
32
|
+
body.simplificator_infrastrcture.errors img.icon {
|
33
|
+
width: 80px;
|
34
|
+
}
|
35
|
+
|
36
|
+
body.simplificator_infrastrcture.errors img.logo {
|
37
|
+
width: 150px;
|
38
|
+
margin-top: 30px;
|
39
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class SimplificatorInfrastructure::ErrorsController < ActionController::Base
|
2
|
+
around_filter :with_locale
|
3
|
+
|
4
|
+
layout 'simplificator_infrastructure/errors'
|
5
|
+
helper_method :error_summary
|
6
|
+
|
7
|
+
|
8
|
+
def render_error
|
9
|
+
render template, status: error_summary.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def template
|
15
|
+
if specific_error_template_exists?
|
16
|
+
specific_template
|
17
|
+
else
|
18
|
+
generic_template
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def specific_template
|
23
|
+
"errors/#{error_summary.status_code}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def generic_template
|
27
|
+
'errors/generic_error'
|
28
|
+
end
|
29
|
+
|
30
|
+
def specific_error_template_exists?
|
31
|
+
lookup_context.template_exists?(error_summary.status_code, "errors", false)
|
32
|
+
end
|
33
|
+
|
34
|
+
def error_summary
|
35
|
+
@error_summary ||= begin
|
36
|
+
::SimplificatorInfrastructure::ErrorSummary.new(env: env)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# sets the locale and resets afterwards for I18n.t to work in error templates
|
41
|
+
def with_locale
|
42
|
+
I18n.with_locale(error_summary.locale) do
|
43
|
+
yield
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<div class="content">
|
2
|
+
<h1 class="error"><%= I18n.t("simplificator_infrastructure.errors.404.title") %></h1>
|
3
|
+
<p class="lead"><%= I18n.t("simplificator_infrastructure.errors.404.lead") %></p>
|
4
|
+
<p><%= I18n.t("simplificator_infrastructure.errors.404.text").html_safe %></p>
|
5
|
+
<p>
|
6
|
+
<%= image_tag("simplificator_infrastructure/errors/error_404.png", class: 'icon')%>
|
7
|
+
<br/>
|
8
|
+
<%= image_tag('simplificator_infrastructure/errors/logo.png') %>
|
9
|
+
</div>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<div class="content">
|
2
|
+
<h1 class="error"><%= I18n.t("simplificator_infrastructure.errors.generic.title") %></h1>
|
3
|
+
<p class="lead"><%= I18n.t("simplificator_infrastructure.errors.generic.lead") %></p>
|
4
|
+
<p><%= I18n.t("simplificator_infrastructure.errors.generic.text").html_safe %></p>
|
5
|
+
<p>
|
6
|
+
<%= image_tag("simplificator_infrastructure/errors/error_generic.png", class: 'icon')%>
|
7
|
+
<br/>
|
8
|
+
<%= image_tag('simplificator_infrastructure/errors/logo.png') %>
|
9
|
+
</div>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title><%= I18n.t('simplificator_infrastructure.errors.page_title') %></title>
|
5
|
+
<%= stylesheet_link_tag "simplificator_infrastructure/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "simplificator_infrastructure/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body class="errors simplificator_infrastrcture">
|
10
|
+
<%= yield %>
|
11
|
+
</body>
|
12
|
+
</html>
|
@@ -0,0 +1,13 @@
|
|
1
|
+
de:
|
2
|
+
simplificator_infrastructure:
|
3
|
+
errors:
|
4
|
+
page_title: 'Ein Fehler ist aufgetreten'
|
5
|
+
|
6
|
+
generic:
|
7
|
+
title: 'Ohhh!'
|
8
|
+
lead: "Die Anfrage konnte leider nicht beantwortet werden."
|
9
|
+
text: "Wir sind benachrichtig worden und schauen uns das an. Bitte versuchen Sie es später nochmals"
|
10
|
+
'404':
|
11
|
+
title: "Oooops!"
|
12
|
+
lead: "Diese Seite existiert nicht (mehr)"
|
13
|
+
text: "Bitte gehen Sie <a href=\"/\">zurück</a> und fangen nochmals an."
|
@@ -0,0 +1,13 @@
|
|
1
|
+
en:
|
2
|
+
simplificator_infrastructure:
|
3
|
+
errors:
|
4
|
+
page_title: 'An error occured'
|
5
|
+
|
6
|
+
generic:
|
7
|
+
title: 'Ohhh!'
|
8
|
+
lead: "We had a problem serving your request."
|
9
|
+
text: "We have been notified and will take care of this.<br/>Please try again later."
|
10
|
+
'404':
|
11
|
+
title: "Oooops!"
|
12
|
+
lead: "The page does not exist."
|
13
|
+
text: "Please go <a href=\"/\">back</a> and start over."
|
data/config/routes.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class SimplificatorInfrastructure::ErrorPageHandler
|
2
|
+
|
3
|
+
def self.register
|
4
|
+
disable_all_requests_local
|
5
|
+
register_exception_app
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def self.disable_all_requests_local
|
11
|
+
Rails.application.config.consider_all_requests_local = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.register_exception_app
|
15
|
+
Rails.application.config.exceptions_app= lambda do |env|
|
16
|
+
action = SimplificatorInfrastructure::ErrorsController.action(:render_error)
|
17
|
+
action.call(env)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class SimplificatorInfrastructure::ErrorSummary
|
2
|
+
|
3
|
+
attr_reader :env
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
@env = options[:env]
|
7
|
+
end
|
8
|
+
|
9
|
+
# detects the status code based on the exception that was thrown
|
10
|
+
def status_code
|
11
|
+
@status_code ||= ActionDispatch::ExceptionWrapper.new(env, exception).status_code
|
12
|
+
end
|
13
|
+
|
14
|
+
# extracts the exception from env
|
15
|
+
def exception
|
16
|
+
env['action_dispatch.exception']
|
17
|
+
end
|
18
|
+
|
19
|
+
def params
|
20
|
+
env['action_dispatch.request.parameters']
|
21
|
+
end
|
22
|
+
|
23
|
+
# tries to detect locale based on #available_locales
|
24
|
+
# and a locale which is in the path, in the accept header or default
|
25
|
+
def locale
|
26
|
+
detected_locale = path_locale || params_locale || accept_locale || default_locale
|
27
|
+
available_locales.include?(detected_locale) ? detected_locale : default_locale
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def default_locale
|
33
|
+
I18n.default_locale
|
34
|
+
end
|
35
|
+
|
36
|
+
def params_locale
|
37
|
+
locale = params['locale']
|
38
|
+
locale.to_sym if locale
|
39
|
+
end
|
40
|
+
|
41
|
+
def path_locale
|
42
|
+
path = env['REQUEST_PATH']
|
43
|
+
# RE is built on the fly to avoid load order issues with I18n.available_locales
|
44
|
+
locales_matcher = available_locales.map(&:to_s).join('|')
|
45
|
+
match = path.match(/\A\/(#{locales_matcher})\/.*\z/)
|
46
|
+
match[1].to_sym if match && match[1]
|
47
|
+
end
|
48
|
+
|
49
|
+
def available_locales
|
50
|
+
I18n.available_locales
|
51
|
+
end
|
52
|
+
|
53
|
+
# Gets the first two letter locale from HTTP_ACCEPT_LANGUAGE header
|
54
|
+
# (ignoring de_DE style locales and expecting locales to be ordered by quality)
|
55
|
+
def accept_locale
|
56
|
+
accept_header = env['HTTP_ACCEPT_LANGUAGE']
|
57
|
+
accept_header.scan(/^[a-z-]{2}/).try(:first).try(:to_sym)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
end
|