Skip to main content

Spring Boot Internationalization with Default Locale for Message Strings

How to add Spring Boot Internationalization with Default Locale for Storing Message Strings such as Validation Messages in a message.properties file 


 This article will show you how to use a properties file to define static text in your application such as for validation message.

All the code is appended at the bottom of the article. 

You can also view all the code here: Link to Github Gist here

Add Default locale and Message Source beans to your Application class 



 Add these classes to set your default locale and configure the location of your message properties file

 
Add beans to your Spring Boot Application class
Add locale and MessageSource beans to your Application Class


Add a Service and Interface to retrieve message text 


 This service will pull the default locale from the session and then get the message text from your properties file using the messageSource.

Create Service to get messages by locale
Add a service to get the msgs in your props file


In your controller, use the Message service to get the message text 


 You will inject the message service in the controller and then pass in the id to get the value from the props file


Inject svc and get message from i18n props file
Inject service into your controller and get the message



Create the message.properties file in the locale folder 


 In resources, create the locale folder and then create a file called messages_en_US.properties

Create localized message props file
Create message properties file



Review the code here


You can view all the code here: Link to Github Gist here

Popular posts from this blog

Change Port on a Spring Boot Application when using intelliJ

Change Port on a Spring Boot Application when using intelliJ  Two ways to change the port that the embedded Tomcat is using in a Spring Boot Application. The first way is to modify the application properties in the resources folder: Update application props in Spring Boot Application There are a couple issues with this approach.  You might only want this port change in your local environment and if you check it in by accident you might break things. Alternatively, you can use a command line parameter. Go to Run -> Edit Configurations -> VM options Then enter in the following argument: -Dserver.port=8090 Then click apply and OK and attempt to run the application again. Update port using VM option

How to set up a SQL Server 2008 Local Database

Setting up and Configuring your Microsoft SQL Server 2008 Local Database Download SQL Server Download SQL Server Express.  Express is the Free edition of MS SQL Server http://www.microsoft.com/en-us/download/details.aspx?id=1695 Install your SQL Server Instance Click the Start button -> Click Microsoft SQL Server 2008 -> Click Configuration Tools -> Select SQL Server Installation Center Select Installation -> Select New SQL Server stand-alone installation Select SQL Server stand-alone installation Get your Local SQL Server Service Up and Running Open up the SQL Server Configuration Manager Click the Start button -> Click Microsoft SQL Server 2008 -> Click Configuration Tools -> Select SQL Server Configuration Manager Setup SQL Server Config Manager Start up the SQL Services You want to ensure the following services are in the "Running" State and then you want to set the following services to automatic St...

Building Microservices with JHipster

Check out this article on Medium and LinkedIn Pulse, for a brief overview of microservices as a primer for working with JHipster to build web applications with a Microservices backend. Building microservices with Jhipster JHipster is sponsored by Ippon Technologies(https://www.ippon.tech/). Need cost-effective fractional engineering with US expertise? Fintech and Blockchain Specialized Fractional Engineering