about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorAkihiko Odaki (@fn_aki@pawoo.net) <akihiko.odaki.4i@stu.hosei.ac.jp>2017-06-10 16:44:02 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-06-10 09:44:02 +0200
commit4919b89ab83f8eee43b89ff4786eae46bf2fa30e (patch)
treef779dac8d3f736ecd6696dac249729aaef3918f7 /app
parent2925372ff44347fa7066c380a5d51dd35f80682f (diff)
Improve default language decision and spec (#3351)
* Improve default language decision

This change allows to takes account of accepted language determined by
the user agent even if the custom default locale of the instance is
configured.

* Cover Localized more

* Fix code style
Diffstat (limited to 'app')
-rw-r--r--app/controllers/concerns/localized.rb22
1 files changed, 17 insertions, 5 deletions
diff --git a/app/controllers/concerns/localized.rb b/app/controllers/concerns/localized.rb
index 5501b6793..a9ea60f7d 100644
--- a/app/controllers/concerns/localized.rb
+++ b/app/controllers/concerns/localized.rb
@@ -17,12 +17,24 @@ module Localized
   end
 
   def default_locale
-    ENV.fetch('DEFAULT_LOCALE') do
-      user_supplied_locale || I18n.default_locale
-    end
+    request_locale || env_locale || I18n.default_locale
   end
 
-  def user_supplied_locale
-    http_accept_language.language_region_compatible_from(I18n.available_locales)
+  def env_locale
+    ENV['DEFAULT_LOCALE']
+  end
+
+  def request_locale
+    preferred_locale || compatible_locale
+  end
+
+  def preferred_locale
+    http_accept_language.preferred_language_from([env_locale]) ||
+      http_accept_language.preferred_language_from(I18n.available_locales)
+  end
+
+  def compatible_locale
+    http_accept_language.compatible_language_from([env_locale]) ||
+      http_accept_language.compatible_language_from(I18n.available_locales)
   end
 end