Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detection of contextual title translation #1447

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/presenters/content_item/title_and_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ContentItemPresenter
include ContentItem::Withdrawable
include ApplicationHelper

attr_reader :content_item,
:requested_content_item_path,
Expand Down
1 change: 1 addition & 0 deletions app/views/content_items/publication.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<div class="govuk-grid-column-two-thirds">
<%= 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" %>
</div>
Expand Down
19 changes: 19 additions & 0 deletions test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/presenters/specialist_document_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down