diff --git a/CHANGELOG.md b/CHANGELOG.md index db516850eb58efa8b960b3eca86c14ca5f238d20..5e0c2df109c95106de2156b81ae6e713f47af709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,16 @@ ### Upcoming Release +* [#492] [FEATURE] Translate attribute labels on show and index pages. + To customize an attribute label, add translations according to the structure: + ``` + en: + helpers: + label: + customer: + name: Full Name + ``` + ### 0.2.0 (April 20, 2016) * [#476] [CHANGE] Extract `Administrate::Field::Image` into its own gem. diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index 755426449c4a5f008fda9c1fdeff88b756389de7..a496574f40a0a703a93195fd5e2c72ae4f48a912 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -109,6 +109,7 @@ module Administrate 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 c99a942c54a4f9b8ed8e1ac94c0b6165f5936f24..c500e554795bdfcde8e5f8d6ccb6938087fdacb7 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. <thead> <tr> <% collection_presenter.attribute_types.each do |attr_name, attr_type| %> - <th class="cell-label cell-label--<%= attr_type.html_class %> - cell-label--<%= collection_presenter.ordered_html_class(attr_name) %> + <th class="cell-label + cell-label--<%= attr_type.html_class %> + 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) %> <span class="cell-label__sort-indicator cell-label__sort-indicator--<%= collection_presenter.ordered_html_class(attr_name) %>"> diff --git a/app/views/administrate/application/show.html.erb b/app/views/administrate/application/show.html.erb index fe0ccbae1956712a769174d1173047c9351b25ad..b4e453b12fa98682c292c8ecb8498908d917c2e4 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. <dl> <% page.attributes.each do |attribute| %> - <dt class="attribute-label"><%= attribute.name.titleize %></dt> + <dt class="attribute-label"> + <%= t( + "helpers.label.#{resource_name}.#{attribute.name}", + default: attribute.name.titleize, + ) %> + </dt> <dd class="attribute-data attribute-data--<%=attribute.html_class%>" ><%= render_field attribute %></dd> diff --git a/app/views/fields/date_time/_show.html.erb b/app/views/fields/date_time/_show.html.erb index 58854d74830ebf524b54e8294420a3a97782748a..c90f23493ad17a2234a76d3ae62c564fc1b5b335 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 05ba784a908dd5214c0bab50403e08a0ec65662e..0ce8acb0e02cc9a3db7b64b629070397ba8159ce 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/docs/customizing_dashboards.md b/docs/customizing_dashboards.md index c0e9c9bd0d6b8f9555b9ce1290ba07d678eed4e4..eb1fb2e565df90b20c53046f9b03c0258fc899f7 100644 --- a/docs/customizing_dashboards.md +++ b/docs/customizing_dashboards.md @@ -76,4 +76,15 @@ if the value is stored by the number of cents: ) ``` +To change the user-facing label for an attribute, +define a custom I18n translation: + +```yaml +en: + helpers: + label: + customer: + name: Full Name +``` + [define your own]: /adding_custom_field_types diff --git a/spec/features/form_spec.rb b/spec/features/form_spec.rb index 23830247020ab84a95f5eb8e3c3ce868c99addbc..414d322c0e2e63910a0fc30e86314c040a318c40 100644 --- a/spec/features/form_spec.rb +++ b/spec/features/form_spec.rb @@ -8,4 +8,24 @@ describe "edit form" do expect(page).to have_css("form.form") end + + it "displays translated labels" do + custom_label = "Newsletter Subscriber" + + translations = { + helpers: { + label: { + customer: { + email_subscriber: custom_label, + }, + }, + }, + } + + with_translations(:en, translations) do + visit new_admin_customer_path + + expect(page).to have_label(custom_label) + end + end end diff --git a/spec/features/index_page_spec.rb b/spec/features/index_page_spec.rb index aa82d1d40f46eb40ab11fba4ab86a092daf7fb63..fde9c0725c32802abded2eee7d5bd633e6640122 100644 --- a/spec/features/index_page_spec.rb +++ b/spec/features/index_page_spec.rb @@ -38,6 +38,26 @@ describe "customer index page" do 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 8700eb08825f2cd1031466b153a815d06ae13b14..b23c651846a13e3fe6552bb4fa3c19d3cf2b6b29 100644 --- a/spec/features/show_page_spec.rb +++ b/spec/features/show_page_spec.rb @@ -58,4 +58,25 @@ RSpec.describe "customer show page" do 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 15eae2e00134842433104d46ea8b53b9da6f1a88..383d7eef60932bd7d77005b92496da0451faebd5 100644 --- a/spec/support/features/page_elements.rb +++ b/spec/support/features/page_elements.rb @@ -2,4 +2,12 @@ module Features def have_header(title) have_css("h1", text: title) end + + def have_label(title) + have_css("label", text: title) + end + + def have_table_header(title) + have_css("th", text: title) + end end