Skip to content

Commit

Permalink
Update web locale determination, fixes sidekiq#2243
Browse files Browse the repository at this point in the history
The code worked with "fr,en" but did not work with "fr-FR,fr,en"
  • Loading branch information
mperham committed Apr 9, 2015
1 parent 0ec7e1d commit 3288aee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes.md
Expand Up @@ -8,6 +8,7 @@ HEAD
- Web UI polling now uses Ajax to avoid page reload [#2266, davydovanton]
- Several Web UI styling improvements [davydovanton]
- Add Tamil, Hindi translations for Web UI [ferdinandrosario, tejasbubane]
- Fix Web UI to work with country-specific locales [#2243]
- Handle circular error causes [#2285, eugenk]

3.3.3
Expand Down
15 changes: 13 additions & 2 deletions lib/sidekiq/web_helpers.rb
Expand Up @@ -45,9 +45,20 @@ def capture(&block)
eval('', block.binding)
end

# Given a browser request Accept-Language header like
# "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2", this function
# will return "fr" since that's the first code with a matching
# locale in web/locales
def locale
lang = (request.env["HTTP_ACCEPT_LANGUAGE"] || 'en').split(',')[0].downcase
strings[lang] ? lang : 'en'
@locale ||= begin
locale = 'en'.freeze
languages = request.env['HTTP_ACCEPT_LANGUAGE'.freeze] || 'en'.freeze
languages.downcase.split(','.freeze).each do |lang|
lang = lang.split(';'.freeze)[0]
break locale = lang if strings.has_key?(lang)
end
locale
end
end

def get_locale
Expand Down
46 changes: 46 additions & 0 deletions test/test_web_helpers.rb
@@ -0,0 +1,46 @@
require_relative 'helper'
require 'sidekiq'
require 'sidekiq/web_helpers'

class TestWebHelpers < Sidekiq::Test

class Helpers
include Sidekiq::WebHelpers

def initialize(params={})
@thehash = default.merge(params)
end

def request
self
end

def settings
self
end

def locales
['web/locales']
end

def env
@thehash
end

def default
{
}
end
end

def test_locale_determination
obj = Helpers.new
assert_equal 'en', obj.locale

obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2')
assert_equal 'fr', obj.locale

obj = Helpers.new('HTTP_ACCEPT_LANGUAGE' => 'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2')
assert_equal 'zh-cn', obj.locale
end
end

0 comments on commit 3288aee

Please sign in to comment.