mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Update web locale determination, fixes #2243
The code worked with "fr,en" but did not work with "fr-FR,fr,en"
This commit is contained in:
parent
0ec7e1d8dc
commit
3288aee6c3
3 changed files with 60 additions and 2 deletions
|
@ -8,6 +8,7 @@ HEAD
|
||||||
- Web UI polling now uses Ajax to avoid page reload [#2266, davydovanton]
|
- Web UI polling now uses Ajax to avoid page reload [#2266, davydovanton]
|
||||||
- Several Web UI styling improvements [davydovanton]
|
- Several Web UI styling improvements [davydovanton]
|
||||||
- Add Tamil, Hindi translations for Web UI [ferdinandrosario, tejasbubane]
|
- 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]
|
- Handle circular error causes [#2285, eugenk]
|
||||||
|
|
||||||
3.3.3
|
3.3.3
|
||||||
|
|
|
@ -45,9 +45,20 @@ module Sidekiq
|
||||||
eval('', block.binding)
|
eval('', block.binding)
|
||||||
end
|
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
|
def locale
|
||||||
lang = (request.env["HTTP_ACCEPT_LANGUAGE"] || 'en').split(',')[0].downcase
|
@locale ||= begin
|
||||||
strings[lang] ? lang : 'en'
|
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
|
end
|
||||||
|
|
||||||
def get_locale
|
def get_locale
|
||||||
|
|
46
test/test_web_helpers.rb
Normal file
46
test/test_web_helpers.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue