From 55b83226c94c7bfe91d39939fb3d01015e224793 Mon Sep 17 00:00:00 2001 From: Joshua Arts Date: Fri, 13 Jul 2018 11:04:32 -0400 Subject: [PATCH] detect users preferred language --- Gemfile | 3 +++ Gemfile.lock | 2 ++ app/controllers/application_controller.rb | 14 ++++++++++---- config/application.rb | 7 +++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 47ce2f1c..a79eeead 100644 --- a/Gemfile +++ b/Gemfile @@ -56,6 +56,9 @@ gem 'bigbluebutton-api-ruby' gem 'bootstrap', '~> 4.1.1' gem 'tabler-rubygem' +# For detecting the users preferred language. +gem 'http_accept_language' + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 9b2ec5b9..0bdf6cc1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -83,6 +83,7 @@ GEM globalid (0.4.1) activesupport (>= 4.2.0) hashie (3.5.7) + http_accept_language (2.1.1) i18n (1.0.1) concurrent-ruby (~> 1.0) jaro_winkler (1.5.1) @@ -274,6 +275,7 @@ DEPENDENCIES dotenv-rails factory_bot_rails faker + http_accept_language jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2f720083..f159345b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,16 +6,22 @@ class ApplicationController < ActionController::Base include SessionsHelper before_action :migration_error? + before_action :set_locale + + protect_from_forgery with: :exception + + MEETING_NAME_LIMIT = 90 + USER_NAME_LIMIT = 30 # Show an information page when migration fails and there is a version error. def migration_error? render :migration_error unless ENV["DB_MIGRATE_FAILED"].blank? end - protect_from_forgery with: :exception - - MEETING_NAME_LIMIT = 90 - USER_NAME_LIMIT = 30 + # Sets the appropriate locale. + def set_locale + I18n.locale = http_accept_language.language_region_compatible_from(I18n.available_locales) + end def meeting_name_limit MEETING_NAME_LIMIT diff --git a/config/application.rb b/config/application.rb index 3216ccd4..b82dcc0f 100644 --- a/config/application.rb +++ b/config/application.rb @@ -17,9 +17,16 @@ module Greenlight # Use custom error routes. config.exceptions_app = routes + # Configure I18n localization. + config.i18n.available_locales = %w(en) + config.i18n.default_locale = "en" + + # Check if a loadbalancer is configured. config.loadbalanced_configuration = ENV["LOADBALANCER_ENDPOINT"].present? && ENV["LOADBALANCER_SECRET"].present? + # The default callback url that bn launcher will redirect to config.gl_callback_url = ENV["GL_CALLBACK_URL"] + # Setup BigBlueButton configuration. if config.loadbalanced_configuration # Fetch credentials from a loadbalancer based on provider.