From d96c54f3cc43271c0e3ed3b7d1c41d79595bb27b Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 11 Jan 2013 11:07:11 -0600 Subject: [PATCH] infer locale from params, then cookies, then Accept-Language header --- Gemfile | 2 ++ Gemfile.lock | 7 +++++++ app/controllers/application_controller.rb | 13 +++++++++---- config/locales/{en-meep.yml => en-MEEP.yml} | 2 +- ...ginate.en-meep.yml => will_paginate.en-MEEP.yml} | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) rename config/locales/{en-meep.yml => en-MEEP.yml} (99%) rename config/locales/{will_paginate.en-meep.yml => will_paginate.en-MEEP.yml} (98%) diff --git a/Gemfile b/Gemfile index 09013ef3..00485c4f 100644 --- a/Gemfile +++ b/Gemfile @@ -48,6 +48,8 @@ gem "carrierwave", "~> 0.5.8" gem "parallel", "~> 0.5.17" +gem "http_accept_language", :git => "git://github.com/iain/http_accept_language.git" + group :development do gem "bullet", "~> 4.1.5" end diff --git a/Gemfile.lock b/Gemfile.lock index 42861b15..4d8c952b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,12 @@ GIT specs: eventmachine (1.0.0.beta.4) +GIT + remote: git://github.com/iain/http_accept_language.git + revision: 7738e45ba154015597fca749b7d0257861c14a02 + specs: + http_accept_language (2.0.0.pre) + GIT remote: git://github.com/igrigorik/em-http-request.git revision: ce50f322ce08d43a4a747cf333ea576765d764c4 @@ -254,6 +260,7 @@ DEPENDENCIES fog (~> 1.1.2) haml (~> 3.0.18) hoptoad_notifier + http_accept_language! jammit (~> 0.5.3) memcache-client (~> 1.8.5) mini_magick (~> 3.4) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 50ebfdf7..aa8edccd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -19,11 +19,12 @@ class ApplicationController < ActionController::Base true end - # This locale-inferrence method is designed for debugging. Once we're ready - # for production, we'll probably start inferring from domain. def infer_locale - inference = params[:locale] - inference if inference && I18n.available_locales.include?(inference.to_sym) + return params[:locale] if valid_locale?(params[:locale]) + return cookies[:locale] if valid_locale?(cookies[:locale]) + Rails.logger.debug "Preferred languages: #{http_accept_language.user_preferred_languages}" + http_accept_language.language_region_compatible_from(I18n.available_locales.map(&:to_s)) || + I18n.default_locale end def localized_fragment_exist?(key) @@ -54,5 +55,9 @@ class ApplicationController < ActionController::Base def user_is?(user) user_signed_in? && user == current_user end + + def valid_locale?(locale) + locale && I18n.available_locales.include?(locale.to_sym) + end end diff --git a/config/locales/en-meep.yml b/config/locales/en-MEEP.yml similarity index 99% rename from config/locales/en-meep.yml rename to config/locales/en-MEEP.yml index 3012c48e..9f2b937a 100644 --- a/config/locales/en-meep.yml +++ b/config/locales/en-MEEP.yml @@ -1,4 +1,4 @@ -en-meep: +en-MEEP: app_name: Dreep to Impreep organization_name: OpenMeep your_items: Your Meeps diff --git a/config/locales/will_paginate.en-meep.yml b/config/locales/will_paginate.en-MEEP.yml similarity index 98% rename from config/locales/will_paginate.en-meep.yml rename to config/locales/will_paginate.en-MEEP.yml index fcf2b59d..d8a2158f 100644 --- a/config/locales/will_paginate.en-meep.yml +++ b/config/locales/will_paginate.en-MEEP.yml @@ -1,4 +1,4 @@ -en-meep: +en-MEEP: will_paginate: previous_label: "← Preevious" next_label: "Neext →"