From 06a5a1f00992dbe715ed30e4df76bd8a58ce95c1 Mon Sep 17 00:00:00 2001 From: Peter Kehinde Olofinmoyin <33905131+KOlofinmoyin@users.noreply.github.com> Date: Tue, 6 Oct 2020 12:25:48 +0100 Subject: [PATCH] Translate model name label on Dashboard index (#1777) Previously, the "New [model]" button on the index page wasn't translated, this uses `display_resource_name` to translate that correctly. Fixes #1677, #1072 Closes #1627 This isn't perfect for all languages, so #1783 will cover incorrect downcasing. Co-authored-by: Pablo Brasero Co-authored-by: Rachael Ewins --- Gemfile | 1 + Gemfile.lock | 59 +++++++++++++++++++ .../administrate/application_helper.rb | 12 ++-- .../administrate/application/_form.html.erb | 2 +- .../administrate/application/index.html.erb | 2 +- .../administrate/application_helper_spec.rb | 20 ++++++- 6 files changed, 87 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 11ebba0240..4c877e990f 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem "administrate-field-image" gem "faker" gem "front_matter_parser" gem "globalid" +gem "kaminari-i18n" gem "pg" gem "redcarpet" gem "sentry-raven" diff --git a/Gemfile.lock b/Gemfile.lock index f7ad613f68..6e94c59a2c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -15,6 +15,23 @@ PATH GEM remote: https://rubygems.org/ specs: + actioncable (6.0.3.3) + actionpack (= 6.0.3.3) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailbox (6.0.3.3) + actionpack (= 6.0.3.3) + activejob (= 6.0.3.3) + activerecord (= 6.0.3.3) + activestorage (= 6.0.3.3) + activesupport (= 6.0.3.3) + mail (>= 2.7.1) + actionmailer (6.0.3.3) + actionpack (= 6.0.3.3) + actionview (= 6.0.3.3) + activejob (= 6.0.3.3) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) actionpack (6.0.3.3) actionview (= 6.0.3.3) activesupport (= 6.0.3.3) @@ -22,17 +39,31 @@ GEM rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (6.0.3.3) + actionpack (= 6.0.3.3) + activerecord (= 6.0.3.3) + activestorage (= 6.0.3.3) + activesupport (= 6.0.3.3) + nokogiri (>= 1.8.5) actionview (6.0.3.3) activesupport (= 6.0.3.3) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (6.0.3.3) + activesupport (= 6.0.3.3) + globalid (>= 0.3.6) activemodel (6.0.3.3) activesupport (= 6.0.3.3) activerecord (6.0.3.3) activemodel (= 6.0.3.3) activesupport (= 6.0.3.3) + activestorage (6.0.3.3) + actionpack (= 6.0.3.3) + activejob (= 6.0.3.3) + activerecord (= 6.0.3.3) + marcel (~> 0.3.1) activesupport (6.0.3.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) @@ -129,19 +160,28 @@ GEM activerecord kaminari-core (= 1.2.1) kaminari-core (1.2.1) + kaminari-i18n (0.5.0) + kaminari + rails kgio (2.11.3) launchy (2.5.0) addressable (~> 2.7) loofah (2.7.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + marcel (0.3.3) + mimemagic (~> 0.3.2) method_source (0.9.2) + mimemagic (0.3.5) mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.2) momentjs-rails (2.20.1) railties (>= 3.1) multipart-post (2.1.1) + nio4r (2.5.4) nokogiri (1.10.10) mini_portile2 (~> 2.4.0) parser (2.7.0.4) @@ -159,6 +199,21 @@ GEM rack-test (1.1.0) rack (>= 1.0, < 3) rack-timeout (0.6.0) + rails (6.0.3.3) + actioncable (= 6.0.3.3) + actionmailbox (= 6.0.3.3) + actionmailer (= 6.0.3.3) + actionpack (= 6.0.3.3) + actiontext (= 6.0.3.3) + actionview (= 6.0.3.3) + activejob (= 6.0.3.3) + activemodel (= 6.0.3.3) + activerecord (= 6.0.3.3) + activestorage (= 6.0.3.3) + activesupport (= 6.0.3.3) + bundler (>= 1.3.0) + railties (= 6.0.3.3) + sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -238,6 +293,9 @@ GEM addressable (>= 2.3.6) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) + websocket-driver (0.7.3) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) xpath (3.2.0) nokogiri (~> 1.8) yard (0.9.25) @@ -263,6 +321,7 @@ DEPENDENCIES front_matter_parser globalid i18n-tasks (= 0.9.31) + kaminari-i18n launchy pg pry-rails diff --git a/app/helpers/administrate/application_helper.rb b/app/helpers/administrate/application_helper.rb index d02ff069d6..44d09c3833 100644 --- a/app/helpers/administrate/application_helper.rb +++ b/app/helpers/administrate/application_helper.rb @@ -1,6 +1,7 @@ module Administrate module ApplicationHelper PLURAL_MANY_COUNT = 2.1 + SINGULAR_COUNT = 1 def application_title if Rails::VERSION::MAJOR <= 5 @@ -43,10 +44,10 @@ def model_from_resource(resource_name) dashboard.try(:model) || resource_name.to_sym end - def display_resource_name(resource_name) + def display_resource_name(resource_name, opts = {}) dashboard_from_resource(resource_name).resource_name( - count: PLURAL_MANY_COUNT, - default: default_resource_name(resource_name), + count: opts[:singular] ? SINGULAR_COUNT : PLURAL_MANY_COUNT, + default: default_resource_name(resource_name, opts), ) end @@ -81,8 +82,9 @@ def clear_search_params private - def default_resource_name(resource_name) - resource_name.to_s.pluralize.gsub("/", "_").titleize + def default_resource_name(name, opts = {}) + resource_name = (opts[:singular] ? name.to_s : name.to_s.pluralize) + resource_name.gsub("/", "_").titleize end end end diff --git a/app/views/administrate/application/_form.html.erb b/app/views/administrate/application/_form.html.erb index f9b83cf75c..944b77ae71 100644 --- a/app/views/administrate/application/_form.html.erb +++ b/app/views/administrate/application/_form.html.erb @@ -21,7 +21,7 @@ and renders all form fields for a resource's editable attributes. <%= t( "administrate.form.errors", pluralized_errors: pluralize(page.resource.errors.count, t("administrate.form.error")), - resource_name: display_resource_name(page.resource_name).singularize + resource_name: display_resource_name(page.resource_name, singular: true) ) %> diff --git a/app/views/administrate/application/index.html.erb b/app/views/administrate/application/index.html.erb index 5e1796ef8f..3661059561 100644 --- a/app/views/administrate/application/index.html.erb +++ b/app/views/administrate/application/index.html.erb @@ -44,7 +44,7 @@ It renders the `_table` partial to display details about the resources. <%= link_to( t( "administrate.actions.new_resource", - name: page.resource_name.titleize.downcase + name: display_resource_name(page.resource_name, singular: true).downcase ), [:new, namespace, page.resource_path], class: "button", diff --git a/spec/helpers/administrate/application_helper_spec.rb b/spec/helpers/administrate/application_helper_spec.rb index 71bf07a9d5..92808dbc4a 100644 --- a/spec/helpers/administrate/application_helper_spec.rb +++ b/spec/helpers/administrate/application_helper_spec.rb @@ -8,6 +8,12 @@ expect(displayed).to eq("Customers") end + it "can return singular of the model name" do + displayed = display_resource_name(:customer, singular: true) + + expect(displayed).to eq("Customer") + end + it "handles string arguments" do displayed = display_resource_name("customer") @@ -27,8 +33,8 @@ end context "when translations are defined" do - it "uses the plural of the defined translation" do - translations = { + let(:translations) do + { activerecord: { models: { customer: { @@ -38,13 +44,23 @@ }, }, } + end + it "uses the plural of the defined translation as default" do with_translations(:en, translations) do displayed = display_resource_name(:customer) expect(displayed).to eq("Users") end end + + it "uses the singular of the defined translation" do + with_translations(:en, translations) do + displayed = display_resource_name(:customer, singular: true) + + expect(displayed).to eq("User") + end + end end context "using custom dashboards" do