Skip to content

Commit

Permalink
Load only required locale files
Browse files Browse the repository at this point in the history
  • Loading branch information
davydovanton committed Apr 9, 2015
1 parent 3288aee commit 6f4821c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/sidekiq/web_helpers.rb
Expand Up @@ -3,19 +3,30 @@
module Sidekiq
# This is not a public API
module WebHelpers
def strings
@@strings ||= begin
def strings(lang)
@@strings ||= {}
@@strings[lang] ||= begin
# Allow sidekiq-web extensions to add locale paths
# so extensions can be localized
settings.locales.each_with_object({}) do |path,global|
Dir["#{path}/*.yml"].each_with_object(global) do |file,hash|
settings.locales.each_with_object({}) do |path, global|
find_locale_files(lang).each do |file|
strs = YAML.load(File.open(file))
hash.deep_merge!(strs)
global.deep_merge!(strs[lang])
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.
def filtering(*)
end
Expand Down Expand Up @@ -55,14 +66,14 @@ def locale
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)
break locale = lang if find_locale_files(lang).any?
end
locale
end
end

def get_locale
strings[locale]
strings(locale)
end

def t(msg, options={})
Expand Down
13 changes: 13 additions & 0 deletions test/test_web.rb
@@ -1,3 +1,4 @@
# encoding: utf-8
require_relative 'helper'
require 'sidekiq'
require 'sidekiq/web'
Expand Down Expand Up @@ -29,6 +30,18 @@ def perform(a, b)
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

it 'can display workers' do
Expand Down

0 comments on commit 6f4821c

Please sign in to comment.