Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/main/resources/messages
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ common.signOut=Sign out

report.a.problem.link=Is this page not working properly?

language.toggle.hidden.english=Change language to English
language.toggle.hidden.cymraeg=Newid yr iaith ir Gymraeg
language.toggle.aria.label=Language switcher

# Attorney Banner

attorney.banner.link=Return to your account
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/messages.cy
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pertax.attorney.banner.link=Yn ôl i’ch cyfrif eich hun
pertax.attorney.banner.user=Rydych o fewn cyfrif <span class="heading-small">{0}</span>.
attorney.banner.nan=enw heb ei ddiffinio

language.toggle.hidden.english=Change language to English
language.toggle.hidden.cymraeg=Newid yr iaith ir Gymraeg
language.toggle.aria.label=Switcher iaith

######################################################
# MESSAGES BELOW THIS LINE STILL REQUIRE TRANSLATION #
######################################################
Expand All @@ -66,3 +70,4 @@ error.address.blank=This address line field must not be blank
error.address.invalid.character=This line contains an invalid character. Valid characters are: A-Z a-z 0-9 - ’ , / & space
error.postcode.length.violation=Postcode is incorrect
error.postcode.invalid.character=This line contains an invalid character. Valid characters are: A-Z a-z 0-9 space

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@*
* Copyright 2021 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*@

@(langMap: Map[String, Lang], langToCall: String => Call, customClass: Option[String] = None, appName: Option[String] = None)(implicit messages: Messages)

<nav class="@if(customClass.isDefined) {@customClass.get} hmrc-language-select" aria-label="@messages("language.toggle.aria.label")">
<ul class="hmrc-language-select__list language-toggle" style="display: block;" >
@langMap.map { case (key: String, value: Lang) =>
@if(messages.lang.code != value.code) {
<li class="hmrc-language-select__list-item" style="display: inline-block;" >
<a id="@key-switch" href="@langToCall(key)" hreflang="@value.code" lang="@value.code" rel="alternate" target="_self" data-sso="false" data-journey-click="@{appName.map(name => s"$name:language: ${value.code}")}">
<span class="visually-hidden">@messages(s"language.toggle.hidden.$key")</span>
<span aria-hidden="true">@key.capitalize</span>
</a>
</li>
} else {
<li class="hmrc-language-select__list-item" style="display: inline-block;" >
Copy link
Contributor

@oscarduignan oscarduignan Apr 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rashadmughal I'll see if I can find an alternative for you, but the use of inline style attributes here feels like something we should avoid to prevent unexpected conflicts in the future

<span aria-current="true">@key.capitalize</span>
</li>
}
@if(key != langMap.last._1) {
@Html(" | ")
}
}
</ul>
</nav>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2021 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.play.views.layouts

import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.Application
import play.api.i18n.{Lang, Messages, MessagesApi}
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc.{Call, PathBindable}
import play.api.test.Helpers._
import uk.gov.hmrc.play.views.html.layouts.LanguageSelection

import java.util.Locale

class LanguageSelectionSpec extends PlaySpec with GuiceOneAppPerSuite {
override implicit lazy val app: Application = new GuiceApplicationBuilder()
.configure(Map("play.i18n.langs" -> Seq("en", "cy", "es")))
.build()
val messagesApi: MessagesApi = app.injector.instanceOf[MessagesApi]
val messagesEnglish: Messages = messagesApi.preferred(Seq(Lang(new Locale("en"))))
val messagesWelsh: Messages = messagesApi.preferred(Seq(Lang(new Locale("cy"))))
val messagesSpanish: Messages = messagesApi.preferred(Seq(Lang(new Locale("es"))))
val EnglishLangCode = "en"
val WelshLangCode = "cy"

val English: Lang = Lang(EnglishLangCode)
val Welsh: Lang = Lang(WelshLangCode)

def languageMap: Map[String, Lang] = Map("english" -> English, "cymraeg" -> Welsh)
def langToUrl(lang: String): Call = Call("GET", "/language/" + implicitly[PathBindable[String]].unbind("lang", lang))

"Language selection template view" should {

"give a link to switch to Welsh when current language is English" in {
val html = LanguageSelection.render(languageMap, langToUrl, None, None, messagesEnglish)
contentType(html) must be("text/html")
contentAsString(html) must include(messagesEnglish("id=\"cymraeg-switch\""))
contentAsString(html) must include("/language/cymraeg")
}

"show correct current language message when current language is English" in {
val html = LanguageSelection.render(languageMap, langToUrl, None, None, messagesEnglish)
contentType(html) must be("text/html")
contentAsString(html) must include("<span aria-current=\"true\">English</span>")
}

"give a link to switch to English when current language is Welsh" in {
val html = LanguageSelection.render(languageMap, langToUrl, None, None, messagesWelsh)
contentType(html) must be("text/html")
contentAsString(html) must include(messagesEnglish("id=\"english-switch\""))
contentAsString(html) must include("/language/english")
}

"show correct current language message when current language is Welsh" in {
val html = LanguageSelection.render(languageMap, langToUrl, None, None, messagesWelsh)
contentType(html) must be("text/html")
contentAsString(html) must include("<span aria-current=\"true\">Cymraeg</span>")
}

"show a custom class if it is set" in {
val html = LanguageSelection.render(languageMap, langToUrl, Some("float--right"), None, messagesWelsh)
contentType(html) must be("text/html")
contentAsString(html) must include("class=\"float--right")
}

"show a data-journey-click attribute for GA if it is set and language is Welsh" in {
val html = LanguageSelection.render(languageMap, langToUrl, None, Some("appName"), messagesWelsh)
contentType(html) must be("text/html")
contentAsString(html) must include("data-journey-click=\"appName:language: en\"")
}

"show a data-journey-click attribute for GA if it is set and language is English" in {
val html = LanguageSelection.render(languageMap, langToUrl, None, Some("appName"), messagesEnglish)
contentType(html) must be("text/html")
contentAsString(html) must include("data-journey-click=\"appName:language: cy\"")
}

"show correct current language message when current language is Spanish" in {
val Spanish = Lang("es")

val mockLanguageMap = Map("english" -> English, "cymraeg" -> Welsh, "español" -> Spanish)

val html = LanguageSelection.render(mockLanguageMap, langToUrl, None, None, messagesSpanish)
contentType(html) must be("text/html")
contentAsString(html) must include("<span aria-current=\"true\">Español</span>")
}
}
}