Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate pt-br date format #136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/sidekiq/statistic/locales/pt-br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ pt-br: # <---- change this to your locale code
LastRun: Última execução
EmptyLogs: Nenhum resultado
SearchLogs: Pesquisar no arquivo de logs
date:
wenderjean marked this conversation as resolved.
Show resolved Hide resolved
formats:
default: "%d/%m/%Y"
datetime: "%d/%m/%Y - %T"
2 changes: 1 addition & 1 deletion lib/sidekiq/statistic/views/statistic.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<td class="worker">
<a href="<%= root_path %>statistic/<%= worker[:name] %>"><%= worker[:name] %></a>
</td>
<td class="worker"><%= format_date worker[:runtime][:last] %></td>
<td class="worker"><%= format_date worker[:runtime][:last], 'datetime' %></td>
<td class="worker"><%= worker[:queue] %></td>
<td class="worker"><%= worker[:number_of_calls][:success] %></td>
<td class="worker"><%= worker[:number_of_calls][:failure] %></td>
Expand Down
4 changes: 2 additions & 2 deletions lib/sidekiq/statistic/views/worker.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
</thead>
<% @worker_statistic.each do |worker| %>
<tr>
<td class="worker"><%= format_date worker[:date], '%e %B %Y' %></td>
<td class="worker"><%= format_date worker[:runtime][:last] %></td>
<td class="worker"><%= format_date worker[:date] %></td>
<td class="worker"><%= format_date worker[:runtime][:last], 'datetime' %></td>
<td class="worker"><%= worker[:success] %></td>
<td class="worker"><%= worker[:failure] %></td>
<td class="worker"><%= worker[:total] %></td>
Expand Down
8 changes: 6 additions & 2 deletions lib/sidekiq/statistic/web_extension_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module WebExtensionHelper

def format_date(date_to_format, format = nil)
time = date_to_format ? convert_to_date_object(date_to_format) : Time.now
time.strftime(format || '%T, %e %B %Y')
time.strftime(date_format(format))
end

def calculate_date_range(params)
Expand All @@ -24,9 +24,13 @@ def calculate_date_range(params)

private

def date_format(format)
get_locale.dig('date', 'formats', format || 'default') || '%m/%d/%Y'
end

def convert_to_date_object(date)
date.is_a?(String) ? Time.parse(date) : Time.at(date)
end
end
end
end
end
41 changes: 41 additions & 0 deletions test/test_sidekiq/web_extension_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# encoding: utf-8

require 'minitest_helper'

module Sidekiq
class Helper < Sidekiq::WebAction
include Sidekiq::Statistic::WebExtensionHelper
end

describe 'WebExtensionHelper' do
include Rack::Test::Methods

describe '.format_date' do
let(:header) { { 'HTTP_ACCEPT_LANGUAGE' => 'pt-br' } }
let(:datetime) { Time.now }
let(:helper) { Helper.new(header, {}) }

describe "when doesn't have translation" do
before { header['HTTP_ACCEPT_LANGUAGE'] = 'xx-xx' }

it 'return date with en format' do
assert_equal helper.format_date(datetime), datetime.strftime('%m/%d/%Y')
end
end

describe 'when have translation' do
it 'return date with default format' do
default_format = helper.get_locale.dig('date', 'formats', 'default')
assert_equal helper.format_date(datetime), datetime.strftime(default_format)
end

it 'return date with datetime format' do
datetime_format = helper.get_locale.dig('date', 'formats', 'datetime')
assert_equal helper.format_date(datetime, 'datetime'), datetime.strftime(datetime_format)
end
end
end
end
end