diff --git a/Gemfile b/Gemfile index fef7593..bc1ca44 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ gem 'dotenv-rails' gem 'jbuilder' gem 'pg' gem 'puma' +gem 'rack-contrib' gem 'rack-cors', require: 'rack/cors' gem 'rails' diff --git a/Gemfile.lock b/Gemfile.lock index 64bfc61..5e128d1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -207,6 +209,7 @@ DEPENDENCIES pg pry-rails puma + rack-contrib rack-cors rails rubocop diff --git a/config/application.rb b/config/application.rb index 1c7cac4..2dffb82 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 end end diff --git a/test/controllers/application_controller_test.rb b/test/controllers/application_controller_test.rb new file mode 100644 index 0000000..34bc0e9 --- /dev/null +++ b/test/controllers/application_controller_test.rb @@ -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 + + get test_path, params: {}, env: { 'HTTP_ACCEPT_LANGUAGE' => 'gsw' } + assert_equal 'gsw', response.body + end + + def teardown + Rails.application.routes_reloader.reload! + end +end