Setting implicit language in i18n Play 2.4 Scala template

Blackbird

Hello Stackoverflow community!

I have i18n working in my Play 2.4 application, so that's a good start.

What I want to do now is to override the implicit Lang sent to a template. For example, if the IP address is located in Sweden, I want to set the implicit Lang to Swedish regardless of what preferred language is set in the browser. How to do this?

This is my code:

My Application.scala controller:

package controllers

import javax.inject.Inject

import play.api.i18n.{I18nSupport, Lang, MessagesApi}
import play.api.mvc._

class Application @Inject() (val messagesApi: MessagesApi) extends Controller with I18nSupport {
  def index = Action { implicit request =>
    if (isIpAddressLocatedInSweden) {
      implicit val langFromIp = Lang.apply("sv")
      Logger.info("Language set to Swedish")
    }

    Ok(views.html.index())
  }

  private def isIpAddressLocatedInSweden: Boolean = {
    [...]
  }
}

My index.scala.html view:

@()(implicit messages: Messages, lang: Lang)

@main("Page Title") {
<span>@lang.toString()</span>
<h1>@Messages("home.title")</h1>
}

Unfortunately, the result is:

  • The <span> element contains the preferred browser language: "Lang(en,)"
  • The <h1> element contains the value I've written in messages.en

Thanks for any help!

Blackbird

This is the cleanest way I got it to work:

Code of my Application.scala controller:

class Application @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
  def index = Action { implicit request =>
    val lang = if (isIpAddressLocatedInSweden) {
      Lang.apply("sv")
    } else {
      Lang.preferred(request.acceptLanguages)
    }

    Ok(views.html.index()).withLang(lang)
  }

  private def isIpAddressLocatedInSweden: Boolean = {
    [...]
  }
}

The code of the index.scala.html view is unchanged.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting implicit language in i18n Play 2.4 Scala template

From Dev

django i18n - template translation but language specific

From Dev

Play framework 2.5 scala i18n

From Dev

Play Framework 2.4.0 and I18n with Scala

From Dev

Render scala template outside of controller? (Play 2)

From Dev

JSP i18n works on each page separately after setting default language in filter

From Dev

Scala and Play: how can I use my implicit object?

From Dev

Play Framework Scala Template

From Dev

How to use language properties with Spring MVC 4 and jQuery I18N?

From Dev

translate content using where with 2 languages ​​in order (language = "enus" or i18n = "enus")

From Dev

Setting implicit instance variable for Scala's object

From Dev

Add <script> to the <head> from scala template tags in Play Framework 2

From Dev

Rails 4 I18n incorrectly recognize locale :'language-conuntry' as :'language' like :'zh-CN' as :zh

From Dev

Dealing with Options in Scala Play Template

From Dev

Play scala template syntax for a string

From Dev

Play scala template with google Multimap

From Dev

Play framework 2.4 internationalization and i18n.Messages - Scala

From Dev

Language selector in Play 2.4 & Scala 2.11.6

From Dev

Scala Play 2.5 Form convention and implicit Messages (MessagesApi) access

From Dev

Scala Play No implicit format for Seq[Option[models.ProcessStepTemplatesModel]] available

From Dev

How to resolve ambiguous implicit values in Scala for parsing JSON in Play Json?

From Dev

In Scala, Is tuple syntax part of language itself or an implicit constructor call on TupleN?

From Dev

In Scala, Is tuple syntax part of language itself or an implicit constructor call on TupleN?

From Dev

Setting message i18n dynamically in Grails

From Dev

Setting RequireJS i18n locale dynamically

From Dev

Scala + Play: how to pass a parameter into a route in a template?

From Dev

Get scala template in a variable in Play Framework

From Dev

play framework passing sub class to scala template

From Dev

using bootstrap with play framework scala template

Related Related

  1. 1

    Setting implicit language in i18n Play 2.4 Scala template

  2. 2

    django i18n - template translation but language specific

  3. 3

    Play framework 2.5 scala i18n

  4. 4

    Play Framework 2.4.0 and I18n with Scala

  5. 5

    Render scala template outside of controller? (Play 2)

  6. 6

    JSP i18n works on each page separately after setting default language in filter

  7. 7

    Scala and Play: how can I use my implicit object?

  8. 8

    Play Framework Scala Template

  9. 9

    How to use language properties with Spring MVC 4 and jQuery I18N?

  10. 10

    translate content using where with 2 languages ​​in order (language = "enus" or i18n = "enus")

  11. 11

    Setting implicit instance variable for Scala's object

  12. 12

    Add <script> to the <head> from scala template tags in Play Framework 2

  13. 13

    Rails 4 I18n incorrectly recognize locale :'language-conuntry' as :'language' like :'zh-CN' as :zh

  14. 14

    Dealing with Options in Scala Play Template

  15. 15

    Play scala template syntax for a string

  16. 16

    Play scala template with google Multimap

  17. 17

    Play framework 2.4 internationalization and i18n.Messages - Scala

  18. 18

    Language selector in Play 2.4 & Scala 2.11.6

  19. 19

    Scala Play 2.5 Form convention and implicit Messages (MessagesApi) access

  20. 20

    Scala Play No implicit format for Seq[Option[models.ProcessStepTemplatesModel]] available

  21. 21

    How to resolve ambiguous implicit values in Scala for parsing JSON in Play Json?

  22. 22

    In Scala, Is tuple syntax part of language itself or an implicit constructor call on TupleN?

  23. 23

    In Scala, Is tuple syntax part of language itself or an implicit constructor call on TupleN?

  24. 24

    Setting message i18n dynamically in Grails

  25. 25

    Setting RequireJS i18n locale dynamically

  26. 26

    Scala + Play: how to pass a parameter into a route in a template?

  27. 27

    Get scala template in a variable in Play Framework

  28. 28

    play framework passing sub class to scala template

  29. 29

    using bootstrap with play framework scala template

HotTag

Archive