From c409f0f01404b7ccb1792f330ce415aa11314bec Mon Sep 17 00:00:00 2001 From: Max Lehmann Date: Tue, 13 Aug 2019 12:36:06 +0100 Subject: [PATCH] Add detection of contextual title translation This adds a helper method which detects whether the string requested was returned by the current locale's translation file. If not, it returns the default locale. This helper is in turn used on the contextual headings, allowing us to pass the information on to the component that displays the heading, meaning we will be able to markup the contextual header's actual language, supporting screenreaders and making the page more semantic. --- app/helpers/application_helper.rb | 12 ++++++++++++ .../content_item/title_and_context.rb | 1 + app/presenters/content_item_presenter.rb | 1 + app/views/content_items/publication.html.erb | 1 + test/helpers/application_helper_test.rb | 19 +++++++++++++++++++ ...rporate_information_page_presenter_test.rb | 2 +- .../specialist_document_presenter_test.rb | 4 +++- 7 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ca21c3a76..95e4121c5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,6 +3,18 @@ def page_text_direction I18n.t("i18n.direction", locale: I18n.locale, default: "ltr") end + def t_locale_fallback(key, options = {}) + options['locale'] = I18n.locale + options[:fallback] = nil + translation = I18n.t(key, options) + + if translation.nil? || translation.include?("translation missing") + I18n.default_locale + else + return nil + end + end + def wrapper_class "direction-#{page_text_direction}" if page_text_direction end diff --git a/app/presenters/content_item/title_and_context.rb b/app/presenters/content_item/title_and_context.rb index 8619bc418..5a8348b88 100644 --- a/app/presenters/content_item/title_and_context.rb +++ b/app/presenters/content_item/title_and_context.rb @@ -4,6 +4,7 @@ def title_and_context { title: title, context: I18n.t("content_item.schema_name.#{document_type}", count: 1), + context_locale: t_locale_fallback("content_item.schema_name.#{document_type}", count: 1), average_title_length: "long" } end diff --git a/app/presenters/content_item_presenter.rb b/app/presenters/content_item_presenter.rb index d9c9fd35f..f1b2faf80 100644 --- a/app/presenters/content_item_presenter.rb +++ b/app/presenters/content_item_presenter.rb @@ -1,5 +1,6 @@ class ContentItemPresenter include ContentItem::Withdrawable + include ApplicationHelper attr_reader :content_item, :requested_content_item_path, diff --git a/app/views/content_items/publication.html.erb b/app/views/content_items/publication.html.erb index 45b7b8620..365937a0d 100644 --- a/app/views/content_items/publication.html.erb +++ b/app/views/content_items/publication.html.erb @@ -8,6 +8,7 @@
<%= render 'govuk_publishing_components/components/title', context: t("content_item.schema_name.#{@content_item.document_type}", count: 1), + context_locale: t_locale_fallback("content_item.schema_name.#{@content_item.document_type}", count: 1), title: @content_item.title, average_title_length: "long" %>
diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index df2100cda..e4c5560ef 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -12,4 +12,23 @@ class ApplicationHelperTest < ActionView::TestCase self.stubs(:request).returns(ActionDispatch::TestRequest.create("PATH_INFO" => '/foo/bar', "QUERY_STRING" => 'ham=jam&spam=gram')) assert_equal '/foo/bar', current_path_without_query_string end + + test "#t_locale_fallback returns nil for a string with a locale translation" do + fallback = t_locale_fallback("content_item.schema_name.imported", count: 1, locale: :de) + assert_equal nil, fallback + end + + test "#t_locale_fallback returns default locale for a string with no locale translation" do + I18n.with_locale(:de) do + fallback = t_locale_fallback("content_item.schema_name.decision", count: 1, locale: :de) + assert_equal :en, fallback + end + end + + test "#t_locale_fallback returns fallback for irrelevant key" do + I18n.with_locale(:de) do + fallback = t_locale_fallback("blah", count: 1) + assert_equal :en, fallback + end + end end diff --git a/test/presenters/corporate_information_page_presenter_test.rb b/test/presenters/corporate_information_page_presenter_test.rb index 95f749d6b..cf96be6a1 100644 --- a/test/presenters/corporate_information_page_presenter_test.rb +++ b/test/presenters/corporate_information_page_presenter_test.rb @@ -30,7 +30,7 @@ def schema_name test 'has title without context' do assert presented_item.is_a?(ContentItem::TitleAndContext) - title_component_params = { title: "About us" } + title_component_params = { title: "About us", context_locale: :en } assert_equal title_component_params, presented_item.title_and_context end diff --git a/test/presenters/specialist_document_presenter_test.rb b/test/presenters/specialist_document_presenter_test.rb index 9d6a4e451..fd04755cf 100644 --- a/test/presenters/specialist_document_presenter_test.rb +++ b/test/presenters/specialist_document_presenter_test.rb @@ -83,7 +83,9 @@ class PresentedSpecialistDocument < SpecialistDocumentTestCase assert presented_item('aaib-reports').is_a?(ContentItem::TitleAndContext) title_component_params = { title: schema_item('aaib-reports')['title'], - average_title_length: 'long' + context_locale: nil, + average_title_length: 'long', + } assert_equal title_component_params, presented_item('aaib-reports').title_and_context