Comments (8)
Julian's answer @ http://stackoverflow.com/a/9734832 seems reasonably complete:
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.
And his comments go further into detail:
Why can't this be the default behavior? I would imagine most apps would want the implicit Land in the view to be the same one used in the controller. – Ike Mar 16 '12 at 15:01
Making this the default behavior would require to differentiate between templates intended to be used with Java controllers (which can’t provide implicit values) and templates intended to be used with Scala controllers. – Julien Richard-Foy Mar 16 '12 at 15:32
from playframework.
The question is what is to be done here: update the documentation? Differentiate between templates used with Java controllers? It's not clear what the verification is here.
from playframework.
I believe that the solution here is actually quite simple. We can make the implicit conversion that converts an implicit RequestHeader to Lang available in templates.
As for how this works in Java, currently we already differentiate between templates used with Java and Scala, they have different imports. So for Java templates, they get an implicit Lang that comes from the play.mvc.Http.Context.lang, provided by PlayMagicForJava.
So the solution is to create a PlayMagicForScala, factor out the play.api.mvc.Controller.lang implicit conversion so that it is available both there and in PlayMagicForScala, and add PlayMagicForScala to the default templatesImport setting for Scala projects.
Whoever implements this, make sure there are tests that test the following permutations:
- Template takes no parameters
- Template takes an implicit Lang - test that the lang is whatever was passed in to the implicit lang
- Template takes an implicit RequestHeader, test that the lang is whatever came from the request header
- Template takes an implicit Lang and an implicit RequestHeader, test that the lang is whatever was passed in through the implicit Lang.
from playframework.
See also #2628 for performance problems related to Java's current approach.
from playframework.
Also related #3782
from playframework.
Should be ok for me
from playframework.
I believe this should be addressed by the messages refactoring in #6438
from playframework.
Fixed by removal of the default lang in #7319
from playframework.
Related Issues (20)
- Drop Java 11
- UnsupportedOperationException building/running Play 2.9/3.0 project using Webjars on Windows NTFS due to POSIX permissions HOT 9
- CSRF token is not generated in DEV HOT 9
- `playUpdateSecret` should not blindly write in first line HOT 3
- `DependencyInjectedRoutingDslTest` and other tests are failed by `LoggerContext` HOT 1
- akka migration/changes documentation HOT 9
- Set `shouldRunSequentially` to `false` by default or just remove `runSynchronized`
- Set up dependabot to update GitHub actions HOT 1
- POST request fails with java.lang.IllegalStateException: Sink.asPublisher(fanout = false) only supports one subscriber HOT 2
- Play 2.9.0 and JDK 17.0.9 com.typesafe.sslconfig.ssl.FakeKeyStore cannot access class sun.security HOT 2
- [Feature] Add option to disable Javascript routers HOT 10
- Evolutions generated in wrong place in `PlayService` project HOT 6
- InjectedController trait not working with scala 3 HOT 9
- The link for guiding user to source in document is broken HOT 2
- COPY button in document code section would move along with the scroll bar HOT 3
- The link for Zenexity has been broken in Play Philosophy page HOT 1
- sbt-heroku plugin no longer maintained HOT 3
- `TestServer.isRunning` throws an exception when the server is not running. HOT 3
- Loss `Result` attributes by any modification HOT 4
- Requests with Large POST Body Immediately Goes into Delay Cancellation Right After the Server Received the Request HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from playframework.