Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90adcda
commit cd69eec
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/assets/stylesheets/govuk_publishing_components/components/_translation-nav.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.gem-c-translation-nav { | ||
@include responsive-top-margin; | ||
@include core-16; | ||
margin-bottom: $gutter; | ||
border-bottom: 1px solid $border-colour; | ||
} | ||
|
||
.gem-c-translation-nav__list { | ||
@extend %contain-floats; | ||
list-style: none; | ||
margin-left: -$gutter-one-third; | ||
margin-right: -$gutter-one-third; | ||
} | ||
|
||
.gem-c-translation-nav__list-item { | ||
float: left; | ||
padding-left: $gutter-one-third; | ||
padding-right: $gutter-one-third; | ||
margin-bottom: $gutter-one-third; | ||
border-right: 1px solid $border-colour; | ||
height: 16px; | ||
|
||
.direction-rtl & { | ||
direction: rtl; | ||
float: right; | ||
text-align: start; | ||
border-left: 1px solid $border-colour; | ||
border-right: 0; | ||
} | ||
} | ||
|
||
.gem-c-translation-nav__list-item:last-child { | ||
border-right: 0; | ||
border-left: 0; | ||
} |
16 changes: 16 additions & 0 deletions
16
app/views/govuk_publishing_components/components/_translation-nav.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<% translations ||= [] %> | ||
<% if translations.length > 1 %> | ||
<nav role="navigation" class="gem-c-translation-nav" aria-label="<%= t("common.translations") %>"> | ||
<ul class="gem-c-translation-nav__list"> | ||
<% translations.each.with_index do |translation, i| %> | ||
<li class="gem-c-translation-nav__list-item"> | ||
<% if translation[:active] %> | ||
<span lang="<%= translation[:locale] %>"><%= translation[:text] %></span> | ||
<% else %> | ||
<%= link_to translation[:text], translation[:base_path], hreflang: translation[:locale], lang: translation[:locale], rel: "alternate" %> | ||
<% end %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
</nav> | ||
<% end %> |
61 changes: 61 additions & 0 deletions
61
app/views/govuk_publishing_components/components/docs/translation-nav.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
name: Translation navigation | ||
description: A list of links to available translations | ||
body: The active property indicates the current language. | ||
accessibility_criteria: | | ||
The component must: | ||
- be [a landmark with a navigation role](https://accessibility.blog.gov.uk/2016/05/27/using-navigation-landmarks/) | ||
- have an accessible name in the current language, eg "Translations" | ||
The translation links must: | ||
- [identify the language of the text](https://www.w3.org/TR/UNDERSTANDING-WCAG20/meaning-other-lang-id.html#meaning-other-lang-id-examples-head) | ||
[Watch a screen reader pronounce text differently based on lang attribute](https://bit.ly/screenreaderpronunciation) | ||
shared_accessibility_criteria: | ||
- link | ||
examples: | ||
default: | ||
data: | ||
translations: | ||
- locale: 'en' | ||
base_path: '/en' | ||
text: 'English' | ||
active: true | ||
- locale: 'hi' | ||
base_path: '/hi' | ||
text: 'हिंदी' | ||
multiple_translations: | ||
data: | ||
translations: | ||
- locale: 'en' | ||
base_path: '/en' | ||
text: 'English' | ||
active: true | ||
- locale: 'fr' | ||
base_path: '/fr' | ||
text: 'Français' | ||
- locale: 'hi' | ||
base_path: '/hi' | ||
text: 'हिंदी' | ||
- locale: 'ja' | ||
base_path: '/ja' | ||
text: '日本語' | ||
- locale: 'ur' | ||
base_path: '/ur' | ||
text: 'اردو' | ||
- locale: 'zh' | ||
base_path: '/zh' | ||
text: '中文' | ||
right_to_left: | ||
data: | ||
translations: | ||
- locale: 'en' | ||
base_path: '/en' | ||
text: 'English' | ||
- locale: 'ar' | ||
base_path: '/ar' | ||
text: 'العربية' | ||
active: true | ||
context: | ||
right_to_left: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "rails_helper" | ||
|
||
describe "Translation nav", type: :view do | ||
def component_name | ||
"translation-nav" | ||
end | ||
|
||
def multiple_translations | ||
[ | ||
{ | ||
locale: 'en', | ||
base_path: '/en', | ||
text: 'English', | ||
active: true | ||
}, | ||
{ | ||
locale: 'hi', | ||
base_path: '/hi', | ||
text: 'हिंदी', | ||
} | ||
] | ||
end | ||
|
||
it "renders nothing when no translations are given" do | ||
assert_empty render_component({}) | ||
end | ||
|
||
it "renders nothing when only one translation given" do | ||
assert_empty render_component( | ||
translations: [ | ||
{ | ||
locale: 'en', | ||
base_path: '/en', | ||
text: 'English', | ||
active: true | ||
} | ||
] | ||
) | ||
end | ||
|
||
it "renders all items in a list" do | ||
render_component(translations: multiple_translations) | ||
assert_select ".gem-c-translation-nav__list-item", count: multiple_translations.length | ||
end | ||
|
||
it "renders an active item as text without a link" do | ||
render_component(translations: multiple_translations) | ||
assert_select ".gem-c-translation-nav__list-item :not(a)", text: "English" | ||
assert_select "a[href=\"/en\"]", false, "An active item should not be a link" | ||
end | ||
|
||
it "renders inactive items as a link with locale, base path and text" do | ||
render_component(translations: multiple_translations) | ||
assert_select ".gem-c-translation-nav__list-item a[lang='hi'][href='/hi']", text: "हिंदी" | ||
end | ||
|
||
it "identifies the language of the text" do | ||
render_component(translations: multiple_translations) | ||
assert_select "span[lang='en']", text: "English" | ||
assert_select "a[lang='hi']", text: "हिंदी" | ||
end | ||
|
||
it "identifies the language of the target page" do | ||
render_component(translations: multiple_translations) | ||
assert_select "a[hreflang='hi']", text: "हिंदी" | ||
end | ||
|
||
it "is labelled as translation navigation" do | ||
render_component(translations: multiple_translations) | ||
assert_select "nav[role='navigation'][aria-label='Translations']" | ||
end | ||
end |