SlideShare a Scribd company logo
Grails - Internationalization
Nakul Ashish Pant
Agenda
Introduction
Why Internationalization
Localizing Messages
Using Parameterized Messages
messageSource / g.message
Summary
Introduction
Web applications are really easy to distribute.Web Applications need to adapt
and behave differently under different situations
For a request from Spain the application might want to display messages in
spanish and in english when request is made from New York
The adaptations made by the application may involve more complexity than
simply displaying different versions of text. An application may need to impose
different business rules based on the origin of a particular request.
Let’s see what Grails has in
its arsenal for this problem?
Localizing Messages
One way of providing this capability is to have a separate version of the
application for each language you want to target. That approach has lots of
problems.
Maintaining all those different versions and trying to keep them all in sync
would be an awful lot of work. A much better idea is to have a single version of
the application that is flexible enough to display messages in various
languages using localized messages.
Localizing messages Continued ..
To support localized messages in your Grails application, you should be defining all
user messages in a properties file.
User messages should not be hard-coded in GSP pages, GSP templates, or
anywhere else.
Having messages in a properties file allows you to maintain all of them all in a single
place.
It also lets you take advantage of the localization capabilities provided by Grails.
I18n directory structure
I18n Directory Structure Continued ..
When a Grails app is created, the project includes a number of localized property
files in the grails-app/i18n/ directory
The messages.properties file in the grails-app/i18n/ directory contains default
validation messages in English. These messages are used when validation fails in a
domain class or command object.
In addition to the default messages.properties file, this directory has several other
properties files that contain the same messages in other languages. For example,
“es” is the language code for Spanish, so messages_es.properties contains
validation messages in Spanish.
I18n Directory Structure Continued ..
Example of a properties file - application.properties
app.grails.version=2.4.4
app.name=smartly
app.servlet.version=3.0
app.version=0.1
Property files are plain text files, which contain name-value pairs.
Retrieving Messages
// GroovyMessages.groovy
def messages = ResourceBundle.getBundle('messages')
def appName = messages.getString('app.name')
println "application name is ${appName}"
Retrieving Messages - message Tag
<body>
...
<g:message code="app.smartly.welcome"/>
... </body>
The code attribute tells the message tag which property value should be retrieved.
By default, Grails will decide which version of the property file to use based on the
locale of the current web request.
A simple way to test your Grails application’s localization is to include a request
parameter named lang and assign it a valid language code, such as “es” for
Spanish
Using URLMapping
A URL Mapping for Localization
class UrlMappings {
static mappings = {
"/store/$lang"(controller:'store')
// ...
}
}
This mapping will map all requests to a URL such as http://localhost:
8080/smartly/en/ or http://localhost:8080/smartly/es/, where “en” and “es” could be
any valid language code.
Demo
g:message
Using Parameterized Messages
Often a user message may consist of more than simple static text.
The message may need to include some data that is not known until runtime.
Defining a Parameterized Message
# messages.properties
gtunes.purchased.songs=You have purchased ({0}) songs.
Continued
java.text.MessageFormat class uses a zero-based index
{0} in the message is a placeholder for the value of the first parameter.
If the message had multiple parameters, they would be represented in the
value of the message with placeholders like {0}, {1}, {2}, and so on.
The message tag supports an optional parameter named args, and if that
parameter is assigned a value, its value will be treated as a list of
parameters that need to be applied to the message.
Continued
gtunes.purchased.songs=You have purchased [{0}] songs
<div>
<g:message code="gtunes.purchased.songs" args="[97]"/>
</div>
Demo
parameterized messages!
Using messageSource
Grails provides a bean named messageSource that can be injected into any
Grails artefact, including controllers, taglibs, other beans, and so on.
The messageSource bean is an instance of the org.springframework.context.
MessageSource interface
This interface defines three overloaded versions of the getMessage method for
retrieving messages from the source.
Using messageSource Continued ..
String getMessage(String code, Object[] args, Locale locale)
String getMessage(String code, Object[] args, String defaultMessage, Locale locale)
String getMessage(MessageSourceResolvable resolvable, Locale locale)
Since the messageSource bean participates in Grails’s dependency autowiring
process, all you need to do to get a reference to the bean is declare a property named
messageSource in your Grails artefact.
Using messageSource Continued ..
package com.gtunes
class StoreService {
def messageSource
def someServiceMethod() {
def msg = messageSource.getMessage('gtunes.my.music', null, null)
// ...
}
...
}
Note that the second and third arguments are null. The second argument is an Object
[], which would be used to pass parameters to a parameterized message. The third
argument is a java.util.Locale, which may be specified to retrieve a message for any
Locale other than the default Locale for this request.
Using messageSource Continued ..
Example -
class StoreService {
def messageSource
def someServiceMethod() {
def msg = messageSource.getMessage('gtunes.my.music',null,Locale.ITALIAN)
// …
}
...
}
Using g.message
From within a Controller or TagLib artefact, a simpler way to retrieve messages
is to invoke the
message GSP tag as a method on the special g namespace variable available
in those artefacts
def index() {
def msg = g.message(code:'gtunes.my.music')
} //All tag libraries are accessible using the very same technique.
Demo
messageSource/g.message
Summary
Retrieving messages from a property file is a snap in a Grails application. The
message tag is very easy to access and use from GSP pages and GSP
templates. The messageSource bean is easily accessible from wherever the
application may need it. All of these enhancements are built on top of proven
and well-understood Java-platform tools, including java.text. MessageFormat
and org.springframework.context.MessageSource.
Questions ?
Thank You !

More Related Content

What's hot

JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
Colin DeCarlo
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
Eyal Vardi
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Javascript
JavascriptJavascript
Javascript
Priyanka Pradhan
 
JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
Krzysztof Szafranek
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
The Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfsThe Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfs
Vincent Chien
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
BoneyGawande
 
Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...
Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...
Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...
Fermin Galan
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
Pamela Fox
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
Web services in java
Web services in javaWeb services in java
Web services in java
maabujji
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjects
Dirkjan Bussink
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
The Exciting Future Of React
The Exciting Future Of ReactThe Exciting Future Of React
The Exciting Future Of React
kristijanmkd
 
Knative Outro
Knative OutroKnative Outro
Knative Outro
Joe Searcy
 
Java9 Beyond Modularity - Java 9 más allá de la modularidad
Java9 Beyond Modularity - Java 9 más allá de la modularidadJava9 Beyond Modularity - Java 9 más allá de la modularidad
Java9 Beyond Modularity - Java 9 más allá de la modularidad
David Gómez García
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the TrenchesXavier Noria
 
Web Api vs MVC
Web Api vs MVCWeb Api vs MVC

What's hot (20)

JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
RicoAjaxEngine
RicoAjaxEngineRicoAjaxEngine
RicoAjaxEngine
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
The Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfsThe Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfs
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
 
Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...
Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...
Orion Context Broker NGSI-v2 Overview for Developers That Already Know NGSI-v...
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
 
Javascript
JavascriptJavascript
Javascript
 
Web services in java
Web services in javaWeb services in java
Web services in java
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjects
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
 
The Exciting Future Of React
The Exciting Future Of ReactThe Exciting Future Of React
The Exciting Future Of React
 
Knative Outro
Knative OutroKnative Outro
Knative Outro
 
Java9 Beyond Modularity - Java 9 más allá de la modularidad
Java9 Beyond Modularity - Java 9 más allá de la modularidadJava9 Beyond Modularity - Java 9 más allá de la modularidad
Java9 Beyond Modularity - Java 9 más allá de la modularidad
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
 
Web Api vs MVC
Web Api vs MVCWeb Api vs MVC
Web Api vs MVC
 

Viewers also liked

Grails Controllers
Grails ControllersGrails Controllers
Grails Controllers
NexThoughts Technologies
 
Command objects
Command objectsCommand objects
Command objects
NexThoughts Technologies
 
G pars
G parsG pars
Bootcamp linux commands
Bootcamp linux commandsBootcamp linux commands
Bootcamp linux commands
NexThoughts Technologies
 
Spring boot
Spring bootSpring boot
Twilio
TwilioTwilio
Gorm
GormGorm
Java reflection
Java reflectionJava reflection
Java reflection
NexThoughts Technologies
 
MetaProgramming with Groovy
MetaProgramming with GroovyMetaProgramming with Groovy
MetaProgramming with Groovy
NexThoughts Technologies
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
NexThoughts Technologies
 
Grails services
Grails servicesGrails services
Grails services
NexThoughts Technologies
 
Groovy DSL
Groovy DSLGroovy DSL
Grails with swagger
Grails with swaggerGrails with swagger
Grails with swagger
NexThoughts Technologies
 
Grails domain classes
Grails domain classesGrails domain classes
Grails domain classes
NexThoughts Technologies
 
RESTEasy
RESTEasyRESTEasy
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
Introduction to thymeleaf
Introduction to thymeleafIntroduction to thymeleaf
Introduction to thymeleaf
NexThoughts Technologies
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
NexThoughts Technologies
 
Apache tika
Apache tikaApache tika
Advanced criteria queries
Advanced criteria queriesAdvanced criteria queries
Advanced criteria queries
NexThoughts Technologies
 

Viewers also liked (20)

Grails Controllers
Grails ControllersGrails Controllers
Grails Controllers
 
Command objects
Command objectsCommand objects
Command objects
 
G pars
G parsG pars
G pars
 
Bootcamp linux commands
Bootcamp linux commandsBootcamp linux commands
Bootcamp linux commands
 
Spring boot
Spring bootSpring boot
Spring boot
 
Twilio
TwilioTwilio
Twilio
 
Gorm
GormGorm
Gorm
 
Java reflection
Java reflectionJava reflection
Java reflection
 
MetaProgramming with Groovy
MetaProgramming with GroovyMetaProgramming with Groovy
MetaProgramming with Groovy
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
 
Grails services
Grails servicesGrails services
Grails services
 
Groovy DSL
Groovy DSLGroovy DSL
Groovy DSL
 
Grails with swagger
Grails with swaggerGrails with swagger
Grails with swagger
 
Grails domain classes
Grails domain classesGrails domain classes
Grails domain classes
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Introduction to thymeleaf
Introduction to thymeleafIntroduction to thymeleaf
Introduction to thymeleaf
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Apache tika
Apache tikaApache tika
Apache tika
 
Advanced criteria queries
Advanced criteria queriesAdvanced criteria queries
Advanced criteria queries
 

Similar to Grails internationalization-160524154831

WordPress Theme Workshop: Internationalization
WordPress Theme Workshop: InternationalizationWordPress Theme Workshop: Internationalization
WordPress Theme Workshop: Internationalization
David Bisset
 
BarCamp KL H20 Open Social Hackathon
BarCamp KL H20 Open Social HackathonBarCamp KL H20 Open Social Hackathon
BarCamp KL H20 Open Social Hackathonmarvin337
 
Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core Module
Katie Gulley
 
S/MIME
S/MIMES/MIME
S/MIME
maria azam
 
Android application structure
Android application structureAndroid application structure
Android application structureAlexey Ustenko
 
Localization and Shared Preferences in android
Localization and Shared Preferences in androidLocalization and Shared Preferences in android
Localization and Shared Preferences in android
Aly Arman
 
AndroidLab_IT.pptx
AndroidLab_IT.pptxAndroidLab_IT.pptx
AndroidLab_IT.pptx
AhmedKedir9
 
Android coding guide lines
Android coding guide linesAndroid coding guide lines
Android coding guide lineslokeshG38
 
concept of oops
concept of oopsconcept of oops
concept of oops
prince sharma
 
JSON Logger Baltimore Meetup
JSON Logger Baltimore MeetupJSON Logger Baltimore Meetup
JSON Logger Baltimore Meetup
ManjuKumara GH
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and outputallenbailey
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsSaurabh Narula
 
Service Layer in PHP | StudySection
Service Layer in PHP | StudySectionService Layer in PHP | StudySection
Service Layer in PHP | StudySection
Study Section
 
03 android application structure
03 android application structure03 android application structure
03 android application structure
Sokngim Sa
 
The smartpath information systems c plus plus
The smartpath information systems  c plus plusThe smartpath information systems  c plus plus
The smartpath information systems c plus plus
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
Golang proto buff_ixxo
Golang proto buff_ixxoGolang proto buff_ixxo
Golang proto buff_ixxo
www.ixxo.io
 
Drupal 8 customized checkout system
Drupal 8 customized checkout systemDrupal 8 customized checkout system
Drupal 8 customized checkout system
Ahmad Hassan
 
Net framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil ChinnakondaNet framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil Chinnakonda
talenttransform
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
SudhanshiBakre1
 

Similar to Grails internationalization-160524154831 (20)

WordPress Theme Workshop: Internationalization
WordPress Theme Workshop: InternationalizationWordPress Theme Workshop: Internationalization
WordPress Theme Workshop: Internationalization
 
BarCamp KL H20 Open Social Hackathon
BarCamp KL H20 Open Social HackathonBarCamp KL H20 Open Social Hackathon
BarCamp KL H20 Open Social Hackathon
 
Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core Module
 
S/MIME
S/MIMES/MIME
S/MIME
 
Android application structure
Android application structureAndroid application structure
Android application structure
 
Adobe Flex4
Adobe Flex4 Adobe Flex4
Adobe Flex4
 
Localization and Shared Preferences in android
Localization and Shared Preferences in androidLocalization and Shared Preferences in android
Localization and Shared Preferences in android
 
AndroidLab_IT.pptx
AndroidLab_IT.pptxAndroidLab_IT.pptx
AndroidLab_IT.pptx
 
Android coding guide lines
Android coding guide linesAndroid coding guide lines
Android coding guide lines
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
JSON Logger Baltimore Meetup
JSON Logger Baltimore MeetupJSON Logger Baltimore Meetup
JSON Logger Baltimore Meetup
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and output
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
 
Service Layer in PHP | StudySection
Service Layer in PHP | StudySectionService Layer in PHP | StudySection
Service Layer in PHP | StudySection
 
03 android application structure
03 android application structure03 android application structure
03 android application structure
 
The smartpath information systems c plus plus
The smartpath information systems  c plus plusThe smartpath information systems  c plus plus
The smartpath information systems c plus plus
 
Golang proto buff_ixxo
Golang proto buff_ixxoGolang proto buff_ixxo
Golang proto buff_ixxo
 
Drupal 8 customized checkout system
Drupal 8 customized checkout systemDrupal 8 customized checkout system
Drupal 8 customized checkout system
 
Net framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil ChinnakondaNet framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil Chinnakonda
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 

More from NexThoughts Technologies

Alexa skill
Alexa skillAlexa skill
GraalVM
GraalVMGraalVM
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
NexThoughts Technologies
 
Apache commons
Apache commonsApache commons
Apache commons
NexThoughts Technologies
 
HazelCast
HazelCastHazelCast
MySQL Pro
MySQL ProMySQL Pro
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
NexThoughts Technologies
 
Swagger
SwaggerSwagger
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
NexThoughts Technologies
 
Arango DB
Arango DBArango DB
Jython
JythonJython
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
NexThoughts Technologies
 
Smart Contract samples
Smart Contract samplesSmart Contract samples
Smart Contract samples
NexThoughts Technologies
 
My Doc of geth
My Doc of gethMy Doc of geth
My Doc of geth
NexThoughts Technologies
 
Geth important commands
Geth important commandsGeth important commands
Geth important commands
NexThoughts Technologies
 
Ethereum genesis
Ethereum genesisEthereum genesis
Ethereum genesis
NexThoughts Technologies
 
Ethereum
EthereumEthereum
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
NexThoughts Technologies
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
NexThoughts Technologies
 
Google authentication
Google authenticationGoogle authentication
Google authentication
NexThoughts Technologies
 

More from NexThoughts Technologies (20)

Alexa skill
Alexa skillAlexa skill
Alexa skill
 
GraalVM
GraalVMGraalVM
GraalVM
 
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
 
Apache commons
Apache commonsApache commons
Apache commons
 
HazelCast
HazelCastHazelCast
HazelCast
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
 
Swagger
SwaggerSwagger
Swagger
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Arango DB
Arango DBArango DB
Arango DB
 
Jython
JythonJython
Jython
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Smart Contract samples
Smart Contract samplesSmart Contract samples
Smart Contract samples
 
My Doc of geth
My Doc of gethMy Doc of geth
My Doc of geth
 
Geth important commands
Geth important commandsGeth important commands
Geth important commands
 
Ethereum genesis
Ethereum genesisEthereum genesis
Ethereum genesis
 
Ethereum
EthereumEthereum
Ethereum
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Google authentication
Google authenticationGoogle authentication
Google authentication
 

Recently uploaded

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 

Grails internationalization-160524154831

  • 2. Agenda Introduction Why Internationalization Localizing Messages Using Parameterized Messages messageSource / g.message Summary
  • 3. Introduction Web applications are really easy to distribute.Web Applications need to adapt and behave differently under different situations For a request from Spain the application might want to display messages in spanish and in english when request is made from New York The adaptations made by the application may involve more complexity than simply displaying different versions of text. An application may need to impose different business rules based on the origin of a particular request.
  • 4. Let’s see what Grails has in its arsenal for this problem?
  • 5. Localizing Messages One way of providing this capability is to have a separate version of the application for each language you want to target. That approach has lots of problems. Maintaining all those different versions and trying to keep them all in sync would be an awful lot of work. A much better idea is to have a single version of the application that is flexible enough to display messages in various languages using localized messages.
  • 6. Localizing messages Continued .. To support localized messages in your Grails application, you should be defining all user messages in a properties file. User messages should not be hard-coded in GSP pages, GSP templates, or anywhere else. Having messages in a properties file allows you to maintain all of them all in a single place. It also lets you take advantage of the localization capabilities provided by Grails.
  • 8. I18n Directory Structure Continued .. When a Grails app is created, the project includes a number of localized property files in the grails-app/i18n/ directory The messages.properties file in the grails-app/i18n/ directory contains default validation messages in English. These messages are used when validation fails in a domain class or command object. In addition to the default messages.properties file, this directory has several other properties files that contain the same messages in other languages. For example, “es” is the language code for Spanish, so messages_es.properties contains validation messages in Spanish.
  • 9. I18n Directory Structure Continued .. Example of a properties file - application.properties app.grails.version=2.4.4 app.name=smartly app.servlet.version=3.0 app.version=0.1 Property files are plain text files, which contain name-value pairs.
  • 10. Retrieving Messages // GroovyMessages.groovy def messages = ResourceBundle.getBundle('messages') def appName = messages.getString('app.name') println "application name is ${appName}"
  • 11. Retrieving Messages - message Tag <body> ... <g:message code="app.smartly.welcome"/> ... </body> The code attribute tells the message tag which property value should be retrieved. By default, Grails will decide which version of the property file to use based on the locale of the current web request. A simple way to test your Grails application’s localization is to include a request parameter named lang and assign it a valid language code, such as “es” for Spanish
  • 12. Using URLMapping A URL Mapping for Localization class UrlMappings { static mappings = { "/store/$lang"(controller:'store') // ... } } This mapping will map all requests to a URL such as http://localhost: 8080/smartly/en/ or http://localhost:8080/smartly/es/, where “en” and “es” could be any valid language code.
  • 14. Using Parameterized Messages Often a user message may consist of more than simple static text. The message may need to include some data that is not known until runtime. Defining a Parameterized Message # messages.properties gtunes.purchased.songs=You have purchased ({0}) songs.
  • 15. Continued java.text.MessageFormat class uses a zero-based index {0} in the message is a placeholder for the value of the first parameter. If the message had multiple parameters, they would be represented in the value of the message with placeholders like {0}, {1}, {2}, and so on. The message tag supports an optional parameter named args, and if that parameter is assigned a value, its value will be treated as a list of parameters that need to be applied to the message.
  • 16. Continued gtunes.purchased.songs=You have purchased [{0}] songs <div> <g:message code="gtunes.purchased.songs" args="[97]"/> </div>
  • 18. Using messageSource Grails provides a bean named messageSource that can be injected into any Grails artefact, including controllers, taglibs, other beans, and so on. The messageSource bean is an instance of the org.springframework.context. MessageSource interface This interface defines three overloaded versions of the getMessage method for retrieving messages from the source.
  • 19. Using messageSource Continued .. String getMessage(String code, Object[] args, Locale locale) String getMessage(String code, Object[] args, String defaultMessage, Locale locale) String getMessage(MessageSourceResolvable resolvable, Locale locale) Since the messageSource bean participates in Grails’s dependency autowiring process, all you need to do to get a reference to the bean is declare a property named messageSource in your Grails artefact.
  • 20. Using messageSource Continued .. package com.gtunes class StoreService { def messageSource def someServiceMethod() { def msg = messageSource.getMessage('gtunes.my.music', null, null) // ... } ... } Note that the second and third arguments are null. The second argument is an Object [], which would be used to pass parameters to a parameterized message. The third argument is a java.util.Locale, which may be specified to retrieve a message for any Locale other than the default Locale for this request.
  • 21. Using messageSource Continued .. Example - class StoreService { def messageSource def someServiceMethod() { def msg = messageSource.getMessage('gtunes.my.music',null,Locale.ITALIAN) // … } ... }
  • 22. Using g.message From within a Controller or TagLib artefact, a simpler way to retrieve messages is to invoke the message GSP tag as a method on the special g namespace variable available in those artefacts def index() { def msg = g.message(code:'gtunes.my.music') } //All tag libraries are accessible using the very same technique.
  • 24. Summary Retrieving messages from a property file is a snap in a Grails application. The message tag is very easy to access and use from GSP pages and GSP templates. The messageSource bean is easily accessible from wherever the application may need it. All of these enhancements are built on top of proven and well-understood Java-platform tools, including java.text. MessageFormat and org.springframework.context.MessageSource.