Internacionalización
Con nuestra aplicación queremos llegar al máximo número de personas posibles, por lo tanto es importante que esté en su propio idioma. Vamos a internacionalizar nuestra aplicación.
Los ficheros de mensajes se definen dentro de la carpeta conf
conf/messages
hello=Hello!
back=Back
Las traducciones de los ficheros can con la extensión de su código ISO.
conf/messages.es
hello=Hola!
back=Atrás
Si desde un controlador queremos averiguar el idioma del usuario
String lang = Lang.get();
Si queremos cambiar el idioma
Lang.change("ja");
Para mostrar los mensajes en las plantillas se utiliza
<h1>&{'hello'}</h1>
Ejercicio
- Internacionaliza alguna parte de la aplicación. Por ejemplo el menú y el pie de página
- Añade un menú desplegable en el menú que permita cambiar el idioma. Utiliza el dropdown de Twitter Bottstrap
- Crea una acción que permita cambiar de idioma. La acción debe redirigir a la misma página donde estaba.
Solución
Crea un nuevo controlador que se va a encargar de cambiar el idioma.
apps/controllers/Language.java
import play.i18n.Lang;
import play.mvc.Controller; public class Language extends Controller { }
1
2
3
4
5
6
7
8
9
10
11
12
13
package controllers;
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">change</span><span class="o">(</span><span class="n">String</span> <span class="n">locale</span><span class="o">,</span> <span class="n">String</span> <span class="n">from</span><span class="o">){</span>
<span class="n">Lang</span><span class="o">.</span><span class="na">change</span><span class="o">(</span><span class="n">locale</span><span class="o">);</span>
<span class="n">redirect</span><span class="o">(</span><span class="n">from</span><span class="o">);</span>
<span class="o">}</span>
El parámetro from sirve para redirigir a la página de origen
Añade en la plantilla en menú con los enlaces a la acción
app/views/main.html
1
2
3
4
5
6
7
<li class=“dropdown”>
<a href=“#” class=“dropdown-toggle”>&{‘idioma’}</a>
<ul class=“dropdown-menu”>
<li><a href=“@{Language.change(‘en’, request.url)}”>&{‘ingles’}</a></li>
<li><a href=“@{Language.change(‘es’, request.url)}”>&{‘espanol’}</a></li>
</ul>
</li>
Modifica el fichero de configuración para especificar los idiomas aceptados
conf/application.conf
application.langs=en,es
Crea los ficheros de idiomas
conf/messages.es
inicio=Inicio
estadisticas=Estadísticas
idioma=Idioma
ingles=Inglés
espanol=Español
salir=Salir
tutorial=Tutorial
codigoFuente=Código Fuente
conf/messages.en
inicio=Home
estadisticas=Stats
idioma=Language
ingles=English
espanol=Spanish
salir=Exit
tutorial=Tutorial
codigoFuente=Sources
Modifica la plantilla para añadir los mensajes. Fíjate en la utilización de &{} para añadir los mensajes.
apps/views/main.html
<html>
<head>
<title>#{get ‘title’ /}</title>
<meta charset=“utf-8”> </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<span class="nt"><link</span> <span class="na">rel=</span><span class="s">"shortcut icon"</span> <span class="na">type=</span><span class="s">"image/png"</span> <span class="na">href=</span><span class="s">"@{'/public/images/favicon.png'}"</span><span class="nt">></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"@{'/public/javascripts/jquery-1.5.2.min.js'}"</span> <span class="na">type=</span><span class="s">"text/javascript"</span> <span class="na">charset=</span><span class="s">"${_response_encoding}"</span><span class="nt">></script></span>
<span class="nt"><link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css"</span><span class="nt">></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"http://twitter.github.com/bootstrap/1.3.0/bootstrap-dropdown.js"</span><span class="nt">></script></span>
<span class="nt"><link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">media=</span><span class="s">"screen"</span> <span class="na">href=</span><span class="s">"@{'/public/stylesheets/main.css'}"</span><span class="nt">></span>
#{get 'moreStyles' /}
#{get 'moreScripts' /}
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"topbar"</span> <span class="na">data-dropdown=</span><span class="s">"dropdown"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"fill"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"container"</span><span class="nt">></span>
<span class="nt"><a</span> <span class="na">class=</span><span class="s">"brand"</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>twitter-play<span class="nt"></a></span>
<span class="nt"><ul</span> <span class="na">class=</span><span class="s">"nav"</span><span class="nt">></span>
<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"@{Timeline.index}"</span><span class="nt">></span><span class="err">&</span>{'inicio'}<span class="nt"></a></li></span>
#{secure.check 'isAdmin'}
<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"@{Timeline.stats}"</span><span class="nt">></span><span class="err">&</span>{'estadisticas'}<span class="nt"></a></li></span>
#{/secure.check}
<span class="nt"><li</span> <span class="na">class=</span><span class="s">"dropdown"</span><span class="nt">></span>
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span> <span class="na">class=</span><span class="s">"dropdown-toggle"</span><span class="nt">></span><span class="err">&</span>{'idioma'}<span class="nt"></a></span>
<span class="nt"><ul</span> <span class="na">class=</span><span class="s">"dropdown-menu"</span><span class="nt">></span>
<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"@{Language.change('en', request.url)}"</span><span class="nt">></span><span class="err">&</span>{'ingles'}<span class="nt"></a></li></span>
<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"@{Language.change('es', request.url)}"</span><span class="nt">></span><span class="err">&</span>{'espanol'}<span class="nt"></a></li></span>
<span class="nt"></ul></span>
<span class="nt"></li></span>
<span class="nt"></ul></span>
<span class="nt"><form</span> <span class="na">action=</span><span class="s">"@{Secure.logout}"</span> <span class="na">class=</span><span class="s">"pull-right"</span><span class="nt">></span>
<span class="nt"><button</span> <span class="na">class=</span><span class="s">"btn"</span> <span class="na">type=</span><span class="s">"submit"</span><span class="nt">></span>${controllers.Security.userConnected().username} - <span class="err">&</span>{'salir'}<span class="nt"></button></span>
<span class="nt"></form></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"container"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"content"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"page-header"</span><span class="nt">></span>
<span class="nt"><h1></span>#{get 'title' /}<span class="nt"></h1></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"row"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"span10"</span><span class="nt">></span>#{doLayout /}<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"span4"</span><span class="nt">></span>#{get 'secondaryContent' /}<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><footer></span>
<span class="nt"><p></span>
<span class="nt"><a</span> <span class="na">href=</span><span class="s">"http://axelhzf.github.com/play-curso/"</span><span class="nt">></span><span class="err">&</span>{'tutorial'}<span class="nt"></a></span> - <span class="nt"><a</span>
<span class="na">href=</span><span class="s">"https://github.com/axelhzf/play-curso"</span><span class="nt">></span><span class="err">&</span>{'codigoFuente'}<span class="nt"></a></span>
<span class="nt"></p></span>
<span class="nt"></footer></span>
<span class="nt"></div></span>
<span class="c"><!-- /container --></span>