Use Rack::Locale to detect locale from HTTP headers

This commit is contained in:
Jérémy Lecour 2018-08-13 15:47:39 +02:00 committed by Jérémy Lecour
parent bcd5a02609
commit 58d8bdd98b
5 changed files with 11 additions and 2 deletions

View File

@ -36,6 +36,8 @@ gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
gem 'rack-contrib'
gem 'open4'
gem 'naught'
gem 'hashie'

View File

@ -214,6 +214,8 @@ GEM
pundit (1.1.0)
activesupport (>= 3.0.0)
rack (2.0.5)
rack-contrib (2.0.1)
rack (~> 2.0)
rack-proxy (0.6.4)
rack
rack-test (1.1.0)
@ -369,6 +371,7 @@ DEPENDENCIES
pry-rails
puma (~> 3.11)
pundit (~> 1.1)
rack-contrib
rails (~> 5.2.0)
rails-i18n (~> 5.1)
rbnacl (>= 3.2, < 5.0)

View File

@ -21,7 +21,9 @@ class ApplicationController < ActionController::Base
end
def set_locale
I18n.locale = current_user.try(:locale) || I18n.default_locale
I18n.locale = current_user.try(:locale) \
|| request.env["rack.locale"].presence \
|| I18n.default_locale
end
def not_found

View File

@ -1,7 +1,7 @@
<% # Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr> %>
<% # License: GNU AGPL-3+ (see full text in LICENSE file) %>
<!DOCTYPE html>
<html>
<html lang="<%= I18n.locale %>">
<head>
<title>Chexpire</title>
<%= csrf_meta_tags %>

View File

@ -2,4 +2,6 @@
require_relative "config/environment"
use Rack::Locale
run Rails.application