質問

Our application attempts to support ES variants.

To this end we capture the user_preferred_language from an incoming request in our ApplicationController.

class ApplicationController < ActionController::Base
  before_filter :set_i18n_locale_for_unauthenticated
  ...

  def set_i18n_locale_for_unauthenticated
    users_preferred_languages = request.user_preferred_languages
    ... # Do something with the array of loacle codes
  end
end

Usually this works OK and the array of locale codes matches the preferences set up in the client browser. E.g. inspecting the user_preferred_languages array may look like

[
  [0] "pt-BR",
  [1] "pt",
  [2] "en-GB",
  [3] "en",
  [4] "en-US",
  [5] "es",
  [6] "es-419"
]

However if the es-419 (Lantin America Spanish) locale is anywhere except the last position then user_preferred_languages will return any empty array??

I am guessing that this is an issue with Rails (or Rack), or possibly two issues:

  1. The parser is not correctly handling the es-419 case as it doesn't conform to the typical xx-YY format.

  2. Somehow, when it is the last preferred language in the list, it manages to slip through.

I haven't tried to dig out the source for this as I was hoping someone had bumped into it before and could suggest how best to handle this. Or perhaps there is a reason why this isn't supported?

FURTHER BACKGROUD I am using Chromium as my browser. Looking at the request headers it seems to be passing the language settings without any issue:

Request Headers
  Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:pt-BR,pt;q=0.8,en-GB;q=0.6,en;q=0.4,en-US;q=0.2,es-419;q=0.2,es;q=0.2
  Cache-Control:no-cache
  Connection:keep-alive
役に立ちましたか?

解決

This is a bug in the http_accept_language gem. It's fixed in the current prerelease:

gem "http_accept_language", "~> 2.0.0.pre"

Your code must be adjusted:

class ApplicationController < ActionController::Base
  ...
  def set_i18n_locale_for_unauthenticated
    users_preferred_languages = http_accept_language.user_preferred_languages
  end
  ...
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top