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

Brexit countdown language and direction fix #1790

Merged
merged 2 commits into from
Nov 23, 2020
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% navigation = GovukPublishingComponents::Presenters::ContextualNavigation.new(content_item, request) %>
<% shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns) %>

<div class="gem-c-contextual-sidebar">
<% if navigation.show_brexit_cta? && navigation.step_by_step_count.zero? %>
Expand All @@ -16,7 +17,7 @@
"track-dimension": t("components.related_navigation.transition.link_text"),
"track-dimension-index": "29",
},
lang: I18n.locale,
lang: shared_helper.t_locale("components.related_navigation.transition.title"),
} %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% related_nav_helper = GovukPublishingComponents::Presenters::RelatedNavigationHelper.new(local_assigns) %>
<% shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns) %>

<% if related_nav_helper.related_navigation? %>
<% random = SecureRandom.hex(4) %>
Expand All @@ -8,7 +9,7 @@
<h2 id="related-nav-related_items-<%= random %>"
class="gem-c-related-navigation__main-heading"
data-track-count="sidebarRelatedItemSection"
<%= related_nav_helper.t_lang(
<%= shared_helper.t_lang(
"components.related_#{local_assigns[:context]}_navigation.related_content",
default: 'components.related_navigation.related_content'
)
Expand All @@ -28,6 +29,7 @@

<%= render 'govuk_publishing_components/components/related_navigation/section',
related_nav_helper: related_nav_helper,
shared_helper: shared_helper,
section_title: section_title,
links: links,
section_index: section_index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
text ||= nil
url ||= nil
data_attributes ||= {}
lang ||= 'en'
lang ||= "en"
direction ||= "ltr"
css_classes = %w(gem-c-transition-countdown)
css_classes << 'gem-c-transition-countdown--cta' if url
css_classes << 'govuk-link' if url
css_classes << "gem-c-transition-countdown--cta" if url
css_classes << "govuk-link" if url
%>

<% countdown = capture do %>
Expand All @@ -24,11 +25,11 @@
<% end %>

<% if url %>
<%= link_to url, class: css_classes, data: data_attributes, lang: lang do %>
<%= link_to url, class: css_classes, data: data_attributes, lang: lang, dir: direction do %>
<%= countdown %>
<% end %>
<% else %>
<%= tag.div class: css_classes, lang: lang do %>
<%= tag.div class: css_classes, lang: lang, dir: direction do %>
<%= countdown %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
link[:path],
class: related_nav_helper.section_css_class("gem-c-related-navigation__section-link", section_title, link, (index >= section_link_limit)),
rel: link[:rel],
lang: related_nav_helper.t_locale_check(link[:locale]),
lang: shared_helper.t_locale_check(link[:locale]),
data: {
track_category: 'relatedLinkClicked',
track_action: "#{section_index}.#{index} #{related_nav_helper.construct_section_heading(section_title) || t('components.related_navigation.related_content')}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,48 +97,8 @@ def related_navigation?
related_navigation.flat_map(&:last).any?
end

def t_locale(content, options = {})
# Check if the content string has a translation
content_translation_available = translation_present?(content)

# True, return locale
this_locale = I18n.locale if content_translation_available
# If false, return default locale
this_locale = I18n.default_locale unless content_translation_available

# Check if default string passed in
if options[:default].present?
# Check if the default string has a translation
default_translation_available = translation_present?(options[:default])
# If true, return locale
this_locale = I18n.locale if default_translation_available
# If false, return default_locale
this_locale = I18n.default_locale unless default_translation_available
end

this_locale
end

def t_lang(content, options = {})
locale = t_locale(content, options)
"lang=#{locale}" unless locale.eql?(I18n.locale)
end

def t_locale_check(locale)
locale.presence unless locale.to_s.eql?(I18n.locale.to_s)
end

private

def translation_present?(content)
default_string = "This is a string to act as a default for the `I18n.translate` method. Comparing the result reveals if there is a translation in the i18n files."
I18n.translate(
content,
default: default_string,
fallback: false,
) != default_string
end

def related_items
related_quick_links = content_item_details_for("quick_links")

Expand Down
42 changes: 42 additions & 0 deletions lib/govuk_publishing_components/presenters/shared_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,48 @@ def get_heading_level

"span"
end

def t_locale(content, options = {})
# Check if the content string has a translation
content_translation_available = translation_present?(content)

# True, return locale
this_locale = I18n.locale if content_translation_available
# If false, return default locale
this_locale = I18n.default_locale unless content_translation_available

# Check if default string passed in
if options[:default].present?
# Check if the default string has a translation
default_translation_available = translation_present?(options[:default])
# If true, return locale
this_locale = I18n.locale if default_translation_available
# If false, return default_locale
this_locale = I18n.default_locale unless default_translation_available
end

this_locale
end

def t_lang(content, options = {})
locale = t_locale(content, options)
"lang=#{locale}" unless locale.eql?(I18n.locale)
end

def t_locale_check(locale)
locale.presence unless locale.to_s.eql?(I18n.locale.to_s)
end

private

def translation_present?(content)
default_string = "This is a string to act as a default for the `I18n.translate` method. Comparing the result reveals if there is a translation in the i18n files."
I18n.translate(
content,
default: default_string,
fallback: false,
) != default_string
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
RSpec.describe GovukPublishingComponents::Presenters::SharedHelper do
describe "Shared component helper" do
after(:each) do
I18n.locale = :en
end

it "returns a default margin class" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})
margin_class = shared_helper.get_margin_bottom
Expand Down Expand Up @@ -52,5 +56,55 @@
GovukPublishingComponents::Presenters::SharedHelper.new(classes: "js-okay not-cool-man")
}.to raise_error(ArgumentError, "Passed classes must be prefixed with `js-`")
end

it "returns nil if given locale is same as page locale" do
default_locale = I18n.locale
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale_check(default_locale)).to be nil
end

it "returns a locale if different to the page locale" do
locale = "ar"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale_check(locale)).to eq locale
end

it "returns the language attribute if translation is not present" do
I18n.locale = :de

translation_key = "this.is.a.key.that.should.not.be.found.to.test.the.translation"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_lang(translation_key)).to eq "lang=en"
end

it "returns no language attribute if translation is present" do
I18n.locale = :fr

translation_key = "components.contents_list.contents"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_lang(translation_key)).to be nil
end

it "returns the locale if translation is not present" do
I18n.locale = :de

translation_key = "this.is.a.key.that.should.not.be.found.to.test.the.translation"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale(translation_key)).to eq :en
end

it "returns no locale if translation is present and using default locale" do
I18n.locale = :fr

translation_key = "components.contents_list.contents"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale(translation_key)).to be :fr
end
end
end