Git Product home page Git Product logo

play-language's Introduction

#Play Language

Apache-2.0 license

Build Status Download

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:

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:

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.

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

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

@()(implicit lang: Lang)

Configuration - Play-2.5 (version 3.0.0)

Create your own custom LanguageController:

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.

@import uk.gov.hmrc.project.controllers.CustomLanguageController
@import play.api.Application

@()(implicit request: Request[_], messages: Messages, application: Application)

@clc = @{ Application.instanceCache[CustomLanguageController].apply(application) }

 @uk.gov.hmrc.project.views.html.main_template(title = "Hello from example-play-25-frontend", bodyClasses = None) {
    <h1 id="message">Hello World!</h1>
    <h2>@Messages("some.message")</h2>

    @language_selection(clc.languageMap, clc.langToCall, Some("custom-class"))
 }

In order to show each language text to the user, create a messages.xx file within /conf, where xx is the language code, and put your translations within there, using the same message keys.

License

This code is open source software licensed under the Apache 2.0 License.

play-language's People

Contributors

ryanb93 avatar johno1985 avatar nehadotkannaujia avatar hmrc-web-operations avatar shaileshp0110 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.