<commit_before>#Play Language

[![Apache-2.0 license](http://img.shields.io/badge/license-Apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)

[![Build Status](https://travis-ci.org/hmrc/play-language.svg)](https://travis-ci.org/hmrc/play-language) [ ![Download](https://api.bintray.com/packages/hmrc/releases/play-language/images/download.svg) ](https://bintray.com/hmrc/releases/play-language/_latestVersion)

Play library to provide common language support and switching functionality for Play projects.

##Endpoints

This library adds a new endpoint:

```
 /language/:lang     - Switches the current language to the lang, if defined in languageMap.
```

##Setup

Add the library to the project dependencies:

``` scala
resolvers += Resolver.bintrayRepo("hmrc", "releases")
libraryDependencies += "uk.gov.hmrc" %% "play-language" % "[INSERT VERSION]"
```

## Configuration - Play-2.3 (version 2.2.0)

Create your own custom LanguageController:

``` scala
object CustomLanguageController extends LanguageController with RunMode {
  
  /** Converts a string to a URL, using the route to this controller. **/
  def langToCall(lang: String): Call = controllers.routes.CustomLanguageController.switchToLanguage(lang)

  /** Provides a fallback URL if there is no referer in the request header. **/
  override protected def fallbackURL: String = current.configuration.getString(s"$env.language.fallbackUrl").getOrElse("/")

  /** Returns a mapping between strings and the corresponding Lang object. **/
  override def languageMap: Map[String, Lang] = Map("english" -> Lang("en"),
                                                    "cymraeg" -> Lang("cy-GB"))
}
```

Add the following to the application conf file for each language you support:

```
application.langs="en,cy"
```

Add the following to your application's custom routes file. 

```
GET     /language/:lang       uk.gov.hmrc.project.CustomLanguageController.switchToLanguage(lang: String)
```

When you want to show a language switch to the user, use the language selection template.

``` scala
@language_selection(CustomLanguageController.languageMap, CustomLanguageController.langToCall, Some("custom-class"))
```

Add an implicit Lang object to each view you wish to support multiple languages.

``` scala
@()(implicit lang: Lang)
```

## Configuration - Play-2.5 (version 3.0.0)

Create your own custom LanguageController:

``` scala
package uk.gov.hmrc.project.controllers

import javax.inject.Inject

import play.api.Play
import play.api.i18n.{Lang, MessagesApi}
import play.api.mvc.Call
import uk.gov.hmrc.play.config.RunMode
import uk.gov.hmrc.play.language.LanguageController

class CustomLanguageController @Inject()(implicit val messagesApi: MessagesApi) extends LanguageController with RunMode {

  /** Converts a string to a URL, using the route to this controller. **/
  def langToCall(lang: String): Call = uk.gov.hmrc.exampleplay25.controllers.routes.CustomLanguageController.switchToLanguage(lang)

  /** Provides a fallback URL if there is no referer in the request header. **/
  override protected def fallbackURL: String = Play.current.configuration.getString(s"$env.language.fallbackUrl").getOrElse("/")

  /** Returns a mapping between strings and the corresponding Lang object. **/
  override def languageMap: Map[String, Lang] = Map("english" -> Lang("en"),
    "cymraeg" -> Lang("cy"))
}
```

Add the following to the application conf file for each language you support:

```
application.langs="en,cy"
```

Add the following to your application's custom routes file.

```
GET     /language/:lang       @uk.gov.hmrc.project.controllers.CustomLanguageController.switchToLanguage(lang: String)
```

When you want to show a language switch to the user, use the language selection template.

``` scala
@import uk.gov.hmrc.project.controllers.CustomLanguageController
@import play.api.Application<commit_msg>PAYE-1075 Update README<commit_after>#Play Language

[![Apache-2.0 license](http://img.shields.io/badge/license-Apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)

[![Build Status](https://travis-ci.org/hmrc/play-language.svg)](https://travis-ci.org/hmrc/play-language) [ ![Download](https://api.bintray.com/packages/hmrc/releases/play-language/images/download.svg) ](https://bintray.com/hmrc/releases/play-language/_latestVersion)

Play library to provide common language support and switching functionality for Play projects.

##Endpoints

This library adds a new endpoint:

```
 /language/:lang     - Switches the current language to the lang, if defined in languageMap.
```

##Setup

Add the library to the project dependencies:

``` scala
resolvers += Resolver.bintrayRepo("hmrc", "releases")
libraryDependencies += "uk.gov.hmrc" %% "play-language" % "[INSERT VERSION]"
```

## Configuration - Play-2.3 (version 2.2.0)

Create your own custom LanguageController:

``` scala
object CustomLanguageController extends LanguageController with RunMode {
  
  /** Converts a string to a URL, using the route to this controller. **/
  def langToCall(lang: String): Call = controllers.routes.CustomLanguageController.switchToLanguage(lang)

  /** Provides a fallback URL if there is no referer in the request header. **/
  override protected def fallbackURL: String = current.configuration.getString(s"$env.language.fallbackUrl").getOrElse("/")

  /** Returns a mapping between strings and the corresponding Lang object. **/
  override def languageMap: Map[String, Lang] = Map("english" -> Lang("en"),
                                                    "cymraeg" -> Lang("cy-GB"))
}
```

Add the following to the application conf file for each language you support:

```
application.langs="en,cy"
```

Add the following to your application's custom routes file. 

```
GET     /language/:lang       uk.gov.hmrc.project.CustomLanguageController.switchToLanguage(lang: String)
```

When you want to show a language switch to the user, use the language selection template.

``` scala
@language_selection(CustomLanguageController.languageMap, CustomLanguageController.langToCall, Some("custom-class"))
```

Add an implicit Lang object to each view you wish to support multiple languages.

``` scala
@()(implicit lang: Lang)
```

## Configuration - Play-2.5 (version 3.0.0)

Create your own custom LanguageController:

``` scala
package uk.gov.hmrc.project.controllers

import javax.inject.Inject

import play.api.Play
import play.api.i18n.{Lang, MessagesApi}
import play.api.mvc.Call
import uk.gov.hmrc.play.config.RunMode
import uk.gov.hmrc.play.language.LanguageController

class CustomLanguageController @Inject()(implicit val messagesApi: MessagesApi) extends LanguageController with RunMode {

  /** Converts a string to a URL, using the route to this controller. **/
  def langToCall(lang: String): Call = uk.gov.hmrc.exampleplay25.controllers.routes.CustomLanguageController.switchToLanguage(lang)

  /** Provides a fallback URL if there is no referer in the request header. **/
  override protected def fallbackURL: String = Play.current.configuration.getString(s"$env.language.fallbackUrl").getOrElse("/")

  /** Returns a mapping between strings and the corresponding Lang object. **/
  override def languageMap: Map[String, Lang] = Map("english" -> Lang("en"),
    "cymraeg" -> Lang("cy"))
}
```

Add the following to the application conf file for each language you support:

```
play.i18n.langs = ["en", "cy"]
```

Add the following to your application's custom routes file.

```
GET     /language/:lang       @uk.gov.hmrc.project.controllers.CustomLanguageController.switchToLanguage(lang: String)
```

When you want to show a language switch to the user, use the language selection template.

``` scala
@import uk.gov.hmrc.project.controllers.CustomLanguageController
@import play.api.Application
{'fraction_non_alphanumeric': 0.12201659221442246, 'fraction_numerical': 0.005360561582641991, 'mean_word_length': 4.4950911640953715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3966661', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2235, 'n_words': 665}