Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
birger
users
Commits
34e900f2
Commit
34e900f2
authored
Aug 30, 2018
by
o@immerda.ch
Browse files
let's do something clever and actually parse the accept lang header
parent
f10b6bd5
Changes
1
Hide whitespace changes
Inline
Side-by-side
app/controllers/application_controller.rb
View file @
34e900f2
...
...
@@ -8,12 +8,20 @@ class ApplicationController < ActionController::Base
before_action
:set_locale
def
valid_l
(
l
)
return
l
if
l
&&
I18n
.
available_locales
.
include?
(
l
)
return
l
if
l
&&
I18n
.
available_locales
.
include?
(
l
.
to_sym
)
nil
end
def
extract_locale
(
request
.
env
[
'HTTP_ACCEPT_LANGUAGE'
]
||
''
).
scan
(
/^[a-z]{2}/
).
select
{
|
l
|
valid_l
(
l
)}.
first
req_langs
=
request
.
env
[
'HTTP_ACCEPT_LANGUAGE'
].
to_s
.
split
(
","
).
map
do
|
lang
|
l
,
q
=
lang
.
split
(
";q="
)
[
l
,
(
q
||
'1'
).
to_f
]
end
return
nil
if
req_langs
.
empty?
req_langs
=
req_langs
.
sort_by
(
&
:last
).
map
(
&
:first
).
reverse
req_langs
=
req_langs
.
map
{
|
l
|
l
[
0
..
1
]}.
uniq
available
=
req_langs
.
select
{
|
l
|
valid_l
(
l
)}
available
.
first
unless
available
.
empty?
end
def
set_locale
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment