1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Merge pull request #2296 from davydovanton/load-locale

Load only required locale files
This commit is contained in:
Mike Perham 2015-04-09 15:36:48 -07:00
commit 4e5ce7da4e
2 changed files with 31 additions and 7 deletions

View file

@ -3,19 +3,30 @@ require 'uri'
module Sidekiq module Sidekiq
# This is not a public API # This is not a public API
module WebHelpers module WebHelpers
def strings def strings(lang)
@@strings ||= begin @@strings ||= {}
@@strings[lang] ||= begin
# Allow sidekiq-web extensions to add locale paths # Allow sidekiq-web extensions to add locale paths
# so extensions can be localized # so extensions can be localized
settings.locales.each_with_object({}) do |path,global| settings.locales.each_with_object({}) do |path, global|
Dir["#{path}/*.yml"].each_with_object(global) do |file,hash| find_locale_files(lang).each do |file|
strs = YAML.load(File.open(file)) strs = YAML.load(File.open(file))
hash.deep_merge!(strs) global.deep_merge!(strs[lang])
end end
end end
end end
end end
def locale_files
@@locale_files = settings.locales.flat_map do |path|
Dir["#{path}/*.yml"]
end
end
def find_locale_files(lang)
locale_files.select { |file| file =~ /\/#{lang}\.yml$/ }
end
# This is a hook for a Sidekiq Pro feature. Please don't touch. # This is a hook for a Sidekiq Pro feature. Please don't touch.
def filtering(*) def filtering(*)
end end
@ -55,14 +66,14 @@ module Sidekiq
languages = request.env['HTTP_ACCEPT_LANGUAGE'.freeze] || 'en'.freeze languages = request.env['HTTP_ACCEPT_LANGUAGE'.freeze] || 'en'.freeze
languages.downcase.split(','.freeze).each do |lang| languages.downcase.split(','.freeze).each do |lang|
lang = lang.split(';'.freeze)[0] lang = lang.split(';'.freeze)[0]
break locale = lang if strings.has_key?(lang) break locale = lang if find_locale_files(lang).any?
end end
locale locale
end end
end end
def get_locale def get_locale
strings[locale] strings(locale)
end end
def t(msg, options={}) def t(msg, options={})

View file

@ -1,3 +1,4 @@
# encoding: utf-8
require_relative 'helper' require_relative 'helper'
require 'sidekiq' require 'sidekiq'
require 'sidekiq/web' require 'sidekiq/web'
@ -29,6 +30,18 @@ class TestWeb < Sidekiq::Test
end end
end end
it 'can show text with any locales' do
rackenv = {'HTTP_ACCEPT_LANGUAGE' => 'ru,en'}
get '/', {}, rackenv
assert_match(/Панель управления/, last_response.body)
rackenv = {'HTTP_ACCEPT_LANGUAGE' => 'es,en'}
get '/', {}, rackenv
assert_match(/Panel de Control/, last_response.body)
rackenv = {'HTTP_ACCEPT_LANGUAGE' => 'en-us'}
get '/', {}, rackenv
assert_match(/Dashboard/, last_response.body)
end
describe 'busy' do describe 'busy' do
it 'can display workers' do it 'can display workers' do