Skip to content

Commit

Permalink
Add translate_hash to handle missing translation keys that return has…
Browse files Browse the repository at this point in the history
…hes; only show translatable/available locales in dropdown (#891)

* Update README.md (#902)

* Add `translate_hash` to handle missing translation keys that return hashes; only show translatable/available locales in dropdown

---------

Co-authored-by: Anton Nefedenkov <[email protected]>
  • Loading branch information
bensheldon and ain2108 authored Apr 2, 2023
1 parent e12b22c commit 1311339
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/controllers/good_job/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ def current_locale
def good_job_available_locales
@_good_job_available_locales ||= GoodJob::Engine.root.join("config/locales").glob("*.yml").map { |path| File.basename(path, ".yml").to_sym }.uniq
end
helper_method :good_job_available_locales
end
end
9 changes: 9 additions & 0 deletions app/helpers/good_job/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,14 @@ def render_icon(name)
partial = lookup_context.find_template("good_job/shared/icons/#{name}", [], true)
partial.render(self, {})
end

def translate_hash(key, **options)
translation_exists?(key, **options) ? translate(key, **options) : {}
end

def translation_exists?(key, **options)
true if good_job_available_locales.include?(I18n.locale)
I18n.exists?(scope_key_by_partial(key), **options)
end
end
end
6 changes: 3 additions & 3 deletions app/views/good_job/shared/_filter.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<option value="" <%= "selected='selected'" if params[:queue_name].blank? %>>All queues</option>

<% filter.queues.each do |name, count| %>
<option value="<%= name.to_param %>" <%= "selected='selected'" if params[:queue_name] == name %>><%= name %> (<%= number_with_delimiter(count, t('good_job.number.format')) %>)</option>
<option value="<%= name.to_param %>" <%= "selected='selected'" if params[:queue_name] == name %>><%= name %> (<%= number_with_delimiter(count, translate_hash('good_job.number.format')) %>)</option>
<% end %>
</select>
</div>
Expand All @@ -26,7 +26,7 @@
<option value="" <%= "selected='selected'" if params[:job_class].blank? %>>All jobs</option>

<% filter.job_classes.each do |name, count| %>
<option value="<%= name.to_param %>" <%= "selected='selected'" if params[:job_class] == name %>><%= name %> (<%= number_with_delimiter(count, t('good_job.number.format')) %>)</option>
<option value="<%= name.to_param %>" <%= "selected='selected'" if params[:job_class] == name %>><%= name %> (<%= number_with_delimiter(count, translate_hash('good_job.number.format')) %>)</option>
<% end %>
</select>
</div>
Expand Down Expand Up @@ -56,7 +56,7 @@
<li class="nav-item">
<%= link_to filter.to_params(state: name), class: "nav-link #{"active" if params[:state] == name}" do %>
<%= t(name, scope: 'good_job.status') %>
<span class="badge bg-primary rounded-pill <%= "bg-secondary" if count == 0 %>"><%= number_with_delimiter(count, t('good_job.number.format')) %></span>
<span class="badge bg-primary rounded-pill <%= "bg-secondary" if count == 0 %>"><%= number_with_delimiter(count, translate_hash('good_job.number.format')) %></span>
<% end %>
</li>
<% end %>
Expand Down
7 changes: 4 additions & 3 deletions app/views/good_job/shared/_navbar.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
<%= link_to jobs_path, class: ["nav-link", ("active" if controller_name == 'jobs')] do %>
<%= t(".jobs") %>
<% jobs_count = GoodJob::Job.count %>
<span class="badge bg-secondary rounded-pill"><%= number_to_human(jobs_count, **I18n.t("good_job.number.human.decimal_units")) %></span>
<span class="badge bg-secondary rounded-pill"><%= number_to_human(jobs_count, **translate_hash("good_job.number.human.decimal_units")) %></span>
<% end %>
</li>
<li class="nav-item">
<%= link_to batches_path, class: ["nav-link", ("active" if controller_name == 'batches')] do %>
<%= "Batches" %>
<% batches_count = GoodJob::BatchRecord.migrated? ? GoodJob::BatchRecord.all.size : 0 %>
<span class="badge bg-secondary rounded-pill"><%= number_to_human(batches_count, units: I18n.t("good_job.number.human.decimal_units.units"), precision: 3) %></span>
<span class="badge bg-secondary rounded-pill"><%= number_to_human(batches_count, **translate_hash("good_job.number.human.decimal_units")) %></span>
<% end %>
</li>
<li class="nav-item">
Expand Down Expand Up @@ -49,7 +49,8 @@
</a>

<ul class="dropdown-menu" aria-labelledby="localeOptions">
<% I18n.available_locales.reject { |locale| locale == I18n.locale }.each do |locale| %>
<% possible_locales = I18n.enforce_available_locales ? (good_job_available_locales & I18n.available_locales) : good_job_available_locales %>
<% possible_locales.reject { |locale| locale == I18n.locale }.each do |locale| %>
<li><%= link_to locale, url_for(locale: locale), class: "dropdown-item" %></li>
<% end %>
</ul>
Expand Down
9 changes: 9 additions & 0 deletions spec/support/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.around do |example|
Rails.logger.debug { "\n\n---- START EXAMPLE: #{example.full_description} (#{example.location})" }
example.run
Rails.logger.debug { "---- END EXAMPLE: #{example.full_description} (#{example.location})\n\n" }
end
end

0 comments on commit 1311339

Please sign in to comment.