Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse locale from Accept-Language HTTP header #92

Merged
merged 2 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'dotenv-rails'
gem 'jbuilder'
gem 'pg'
gem 'puma'
gem 'rack-contrib'
gem 'rack-cors', require: 'rack/cors'
gem 'rails'

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ GEM
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
rack-contrib (2.3.0)
rack (~> 2.0)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-test (1.1.0)
Expand Down Expand Up @@ -207,6 +209,7 @@ DEPENDENCIES
pg
pry-rails
puma
rack-contrib
rack-cors
rails
rubocop
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ class Application < Rails::Application
config.api_only = true

config.time_zone = 'UTC'

config.i18n.available_locales = [:de, :en, :fr, :it, :gsw, :rm]
config.i18n.default_locale = :en
config.middleware.use Rack::Locale
dbrgn marked this conversation as resolved.
Show resolved Hide resolved
end
end
35 changes: 35 additions & 0 deletions test/controllers/application_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'test_helper'

class LanguageTestController < ApplicationController
def authorize!; end

def index
render plain: I18n.locale
end
end

class ApplicationControllerTest < ActionDispatch::IntegrationTest
def setup
Rails.application.routes.draw do
get 'test' => 'language_test#index'
end
end

test 'reading the accept language header' do
get test_path, params: {}, env: { 'HTTP_ACCEPT_LANGUAGE' => 'da, loltheheck' }
assert_equal 'en', response.body

get test_path, params: {}, env: { 'HTTP_ACCEPT_LANGUAGE' => 'rm, de-CH' }
assert_equal 'rm', response.body

get test_path, params: {}, env: { 'HTTP_ACCEPT_LANGUAGE' => 'de-CH, en-US, fr' }
assert_equal 'fr', response.body
Comment on lines +25 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attention: no partial matches, only exact ones.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be OK for us, since we can control the request headers.


get test_path, params: {}, env: { 'HTTP_ACCEPT_LANGUAGE' => 'gsw' }
assert_equal 'gsw', response.body
end

def teardown
Rails.application.routes_reloader.reload!
end
end