Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:16:23+00:00 2026-05-31T10:16:23+00:00

Controller: def test = Action { implicit request => import play.api.i18n._ val msg =

  • 0

Controller:

def test = Action { implicit request =>
    import play.api.i18n._
    val msg = Messages("error.invalid")
    implicit val langInController = lang(request)
    Ok(views.html.test(langInController, msg))
}

View:

@(langInController: play.api.i18n.Lang, msg:String)(implicit request: Request[_])
<div>Lang from controller: @langInController, Message: @msg</div>
<div>Message from view: @play.api.i18n.Messages("error.required")</div>

Messages resource, conf/messages.zh-CN:

error.required=该字段必填

Trying

  1. Uses an English Firefox which sends the request header Accept-Language:en-us,en;q=0.5 to visit the test action. The result is:

    Language from controller: Lang(en,), Message: This field is required
    Message in view: 该字段必填
    
  2. Uses an Chinese Google Chrome which sends the request header Accept-Language:zh-CN,zh;q=0.8 to visit it. The result is:

    Language: Lang(zh,CN), Message: 该字段必填
    Message in view: 该字段必填
    

From the tests, we know that:

  1. The implicit language in the controller is from the request header’s Accept-Language
  2. The implicit language used in template is determined by the OS

Environment:

  1. Play 2 is latest play2.1-SNAPSHOT from GitHub (2012-03-16)
  2. My OS is Windows 7 x64 Chinese version

I think Play 2 should use the same implicit language for controllers and views. I can fix it by adding something in Build.sbt:

val main = PlayProject(...) (
    templatesImport ++= Seq("utilis.TemplateMixin._")
)

Where the TemplateMixin is just:

object TemplateMixin extends play.api.mvc.Controller

(It extends Controller and is just to reuse some methods, like implicit def lang(request).)

But I think it should be done by the Play framework.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:16:24+00:00Added an answer on May 31, 2026 at 10:16 am

    The play.api.i18n.Messages(key) function takes an additional implicit parameter of type Lang. So when you write Messages("foo") it is expanded to Messages("foo")(l), where l is a value of type Lang taken from the current implicit scope.

    There’s always an available default implicit lang (which has a low priority), using your jvm default locale.

    But when you are inside a Controller, an implicit value with a higher priority can be found if there is an implicit request. This value looks in the Accept-Language header of the request.

    When you are inside a template, the default implicit lang will be used unless your template imports another implicit lang.

    That’s why, in your example, messages computed from the Controller use the Accept-Language request header and messages computed from the View use your jvm default locale.

    If you add an implicit parameter of type Lang to your template, this parameter will have a higher priority than the default lang and will be used to compute messages:

    @(langInController: Lang, msg:String)(implicit request: RequestHeader, lang: Lang)
    
    <div>Lang from controller: @langInController, Message: @msg</div>
    <div>Message from view: @Messages("error.required")</div>
    

    When you’ll call the template from a Controller action, its implicit lang will be passed, so the same lang will be used by both your Views and your Controllers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In rails 3.1, I implement as: 1. Define test action in post controller: def
I need to know how to test this controller action def create_mobile if mobile_user
def filters = { forUser(controller:'user', action:'*') { before = { user=springSecurityService.getCurrentUser() log.info(came to filter
I want to test that my controller action is rendering a partial. I've poked
I have the following controller action and test. I'm new to testing with Shoulda
Below is my action in the controller. I am trying to test this action
I have two actions in my controller : def find @item = Item.find(params[:id]) render
I have the following application_controller method: def current_account @current_account ||= Account.find_by_subdomain(request.subdomain) end Should I
In my messages_controller I have the following private method: def find_message_or_404(slug) message = user.messages.find_by_slug(slug)
I have a controller like this : @Secured(['ROLE_USER','IS_AUTHENTICATED_FULLY']) def userprofile(){ def user = User.get(springSecurityService.principal.id)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.