From 68cba4d8626884acd128b06d29039a0016eacfda Mon Sep 17 00:00:00 2001 From: Grayson Wright Date: Mon, 11 Apr 2016 21:54:07 -0700 Subject: [PATCH] Make translated labels work on index, show pages Problem: At the moment, developers can customize a field's label on form pages with the following translation structure: ``` en: helpers: label: customer: name: Full Name ``` Many developers expect this label change to take effect on the `show` and `index` pages, as well as the form page. Solution: Translate attribute labels with I18n before displaying them on `show` and `index` pages. --- .../administrate/application_controller.rb | 1 + .../application/_collection.html.erb | 10 ++++++--- .../administrate/application/show.html.erb | 7 ++++++- app/views/fields/date_time/_show.html.erb | 2 +- app/views/fields/has_many/_show.html.erb | 2 +- spec/features/index_page_spec.rb | 20 ++++++++++++++++++ spec/features/show_page_spec.rb | 21 +++++++++++++++++++ spec/support/features/page_elements.rb | 4 ++++ 8 files changed, 61 insertions(+), 6 deletions(-) diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index 41033075dc..95c42ed25d 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -107,6 +107,7 @@ def permitted_attributes delegate :resource_class, :resource_name, :namespace, to: :resource_resolver helper_method :namespace + helper_method :resource_name def resource_resolver @_resource_resolver ||= diff --git a/app/views/administrate/application/_collection.html.erb b/app/views/administrate/application/_collection.html.erb index c99a942c54..c500e55479 100644 --- a/app/views/administrate/application/_collection.html.erb +++ b/app/views/administrate/application/_collection.html.erb @@ -22,13 +22,17 @@ to display a collection of resources in an HTML table. <% collection_presenter.attribute_types.each do |attr_name, attr_type| %> - + cell-label--<%= collection_presenter.ordered_html_class(attr_name) %> " scope="col"> <%= link_to(params.merge( collection_presenter.order_params_for(attr_name) )) do %> - <%= attr_name.to_s.titleize %> + <%= t( + "helpers.label.#{resource_name}.#{attr_name}", + default: attr_name.to_s, + ).titleize %> <% if collection_presenter.ordered_by?(attr_name) %> diff --git a/app/views/administrate/application/show.html.erb b/app/views/administrate/application/show.html.erb index fe0ccbae19..b4e453b12f 100644 --- a/app/views/administrate/application/show.html.erb +++ b/app/views/administrate/application/show.html.erb @@ -31,7 +31,12 @@ as well as a link to its edit page.
<% page.attributes.each do |attribute| %> -
<%= attribute.name.titleize %>
+
+ <%= t( + "helpers.label.#{resource_name}.#{attribute.name}", + default: attribute.name.titleize, + ) %> +
<%= render_field attribute %>
diff --git a/app/views/fields/date_time/_show.html.erb b/app/views/fields/date_time/_show.html.erb index 58854d7483..c90f23493a 100644 --- a/app/views/fields/date_time/_show.html.erb +++ b/app/views/fields/date_time/_show.html.erb @@ -17,5 +17,5 @@ as a localized date & time string. %> <% if field.data %> - <%= l field.data %> + <%= l(field.data, default: field.data) %> <% end %> diff --git a/app/views/fields/has_many/_show.html.erb b/app/views/fields/has_many/_show.html.erb index 05ba784a90..0ce8acb0e0 100644 --- a/app/views/fields/has_many/_show.html.erb +++ b/app/views/fields/has_many/_show.html.erb @@ -36,5 +36,5 @@ from the associated resource class's dashboard. <% end %> <% else %> - <%= t("administrate.fields.has_many.none") %> + <%= t("administrate.fields.has_many.none", default: "–") %> <% end %> diff --git a/spec/features/index_page_spec.rb b/spec/features/index_page_spec.rb index aa82d1d40f..fde9c0725c 100644 --- a/spec/features/index_page_spec.rb +++ b/spec/features/index_page_spec.rb @@ -38,6 +38,26 @@ expect(current_path).to eq(new_admin_customer_path) end + it "displays translated labels" do + custom_label = "Newsletter Subscriber" + + translations = { + helpers: { + label: { + customer: { + email_subscriber: custom_label, + }, + }, + }, + } + + with_translations(:en, translations) do + visit admin_customers_path + + expect(page).to have_table_header(custom_label) + end + end + it "paginates records based on a constant" do customers = create_list(:customer, 2) diff --git a/spec/features/show_page_spec.rb b/spec/features/show_page_spec.rb index 8700eb0882..b23c651846 100644 --- a/spec/features/show_page_spec.rb +++ b/spec/features/show_page_spec.rb @@ -58,4 +58,25 @@ expect(page).to have_header("Edit #{displayed(customer)}") end + + it "displays translated labels" do + custom_label = "Newsletter Subscriber" + customer = create(:customer) + + translations = { + helpers: { + label: { + customer: { + email_subscriber: custom_label, + }, + }, + }, + } + + with_translations(:en, translations) do + visit admin_customer_path(customer) + + expect(page).to have_css(".attribute-label", text: custom_label) + end + end end diff --git a/spec/support/features/page_elements.rb b/spec/support/features/page_elements.rb index 7e5e435a7e..383d7eef60 100644 --- a/spec/support/features/page_elements.rb +++ b/spec/support/features/page_elements.rb @@ -6,4 +6,8 @@ def have_header(title) def have_label(title) have_css("label", text: title) end + + def have_table_header(title) + have_css("th", text: title) + end end