Spring MVC

Spring MVC Internationalization & Localization with Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In this chapter we will discuss about Internationalization &lpar;I18N&rpar; and Localization &lpar;L10N&rpar; in Spring 3&period;0 MVC&period;<&sol;p>&NewLine;<p><i><b>What is i18n and L10n&quest;<&sol;b><&sol;i><br &sol;>&NewLine;In computing&comma; internationalization and localization are means of adapting computer software to different languages and regional differences&period; Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes&period; Localization is the process of adapting internationalized software for a specific region or language by adding locale-specific components and translating text&period;<&sol;p>&NewLine;<p>The terms are frequently abbreviated to the numerous i18n &lpar;where 18 stands for the number of letters between the first i and last n in internationalization&rpar; and L10n respectively&comma; due to the length of the words&period; The capital L in L10n helps to distinguish it from the lowercase i in i18n&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>Now lets we create an application which support multiple languages&period; We will add two languages support to our application&colon; <b>English <&sol;b>and <b>German<&sol;b>&period; Depending on the locale setting of users browser&comma; the appropriate language will be selected&period;<&sol;p>&NewLine;<p><i><b>Step 1&colon;<&sol;b><&sol;i> First we create the properties file for the different languages&period;<&sol;p>&NewLine;<ol>&NewLine;<li>messages&lowbar;en&period;properties<&sol;li>&NewLine;<li>messages&lowbar;de&period;properties<&sol;li>&NewLine;<&sol;ol>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>resources&sol;messages&lowbar;en&period;properties<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">emp&period;label&period;id&equals;Employee Id &NewLine;emp&period;label&period;name&equals;Employee Name &NewLine;emp&period;label&period;age&equals;Employee Age &NewLine;emp&period;label&period;salary&equals;Salary &NewLine;emp&period;label&period;address&equals;Address &NewLine; &NewLine;label&period;menu&equals;Menu &NewLine;label&period;title&equals;Employee Management System &NewLine; &NewLine;label&period;footer&equals;© dineshonjava&period;com &NewLine;<&sol;pre>&NewLine;<p><b>resources&sol;messages&lowbar;de&period;properties<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">emp&period;label&period;id&equals;Impelyee Id &NewLine;emp&period;label&period;name&equals;Impelyee Vorname &NewLine;emp&period;label&period;age&equals;Impelyee iage &NewLine;emp&period;label&period;salary&equals;shalery &NewLine;emp&period;label&period;address&equals;Adrrezz &NewLine; &NewLine;label&period;menu&equals;Menü &NewLine;label&period;title&equals;Impelyee Managemenot Sistom &NewLine; &NewLine;label&period;footer&equals;© dineshonjava&period;com &NewLine;<&sol;pre>&NewLine;<p><i><b>Step 2&colon; Configure i18n &amp&semi; l10n to Spring3MVC<&sol;b><&sol;i><br &sol;>&NewLine;we need to declare these files in spring configuration file&period; We will use class <i><b>org&period;springframework&period;context&period;support&period;ReloadableResourceBundleMessageSource<&sol;b><&sol;i> to define the message resources&period; Also&comma; note that we will provide a feature where user will be able to select language for the application&period; This is implemented by using <i><b>org&period;springframework&period;web&period;servlet&period;i18n&period;LocaleChangeInterceptor<&sol;b><&sol;i> class&period; The LocaleChangeInterceptor class will intercept any changes in the locale&period; These changes are then saved in cookies for future request&period; <i><b>org&period;springframework&period;web&period;servlet&period;i18n&period;CookieLocaleResolver<&sol;b><&sol;i>class will be used to store the locale changes in cookies&period;<br &sol;>&NewLine;Add following code in the <i><b>sdnext-servlet&period;xml<&sol;b><&sol;i> file&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;bean class&equals;"org&period;springframework&period;context&period;support&period;ReloadableResourceBundleMessageSource" id&equals;"messageSource"&gt&semi; &NewLine; &lt&semi;property name&equals;"basename" value&equals;"classpath&colon;messages"&gt&semi;&lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;property name&equals;"defaultEncoding" value&equals;"UTF-8"&gt&semi;&lt&semi;&sol;property&gt&semi; &NewLine;&lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;i18n&period;LocaleChangeInterceptor" id&equals;"localeChangeInterceptor"&gt&semi; &NewLine; &lt&semi;property name&equals;"paramName" value&equals;"lang"&gt&semi;&lt&semi;&sol;property&gt&semi; &NewLine;&lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;i18n&period;CookieLocaleResolver" id&equals;"localeResolver"&gt&semi; &NewLine; &lt&semi;property name&equals;"defaultLocale" value&equals;"en"&gt&semi;&lt&semi;&sol;property&gt&semi; &NewLine;&lt&semi;&sol;bean&gt&semi; &NewLine; &NewLine;&lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;mvc&period;annotation&period;DefaultAnnotationHandlerMapping" id&equals;"handlerMapping"&gt&semi; &NewLine; &lt&semi;property name&equals;"interceptors"&gt&semi; &NewLine; &lt&semi;ref bean&equals;"localeChangeInterceptor"&gt&semi;&lt&semi;&sol;ref&gt&semi; &NewLine; &lt&semi;&sol;property&gt&semi; &NewLine;&lt&semi;&sol;bean&gt&semi; &NewLine;<&sol;pre>&NewLine;<p><b>Note <&sol;b>that in above configuration we have defined basename property in <i><b>messageSource <&sol;b><&sol;i>bean to <i><b>classpath&colon;messages<&sol;b><&sol;i>&period; By this&comma; spring will identify that the message resource <i><b>message&lowbar; <&sol;b><&sol;i>will be used in this application&period;<br &sol;>&NewLine;Change in the JSPs Template&colon;<br &sol;>&NewLine;WebRoot&sol;WEB-INF&sol;views&sol;header&period;jsp<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat;taglib uri&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;tags" prefix&equals;"spring"&percnt;&gt&semi;<&sol;pre>&NewLine;<h3>&lt&semi;spring&colon;message code&equals;&&num;8221&semi;emp&period;label&period;title&&num;8221&semi;&gt&semi;&lt&semi;&sol;spring&colon;message&gt&semi;<&sol;h3>&NewLine;<p>&lt&semi;span style&equals;&&num;8221&semi;float&colon; right&semi;&&num;8221&semi;&gt&semi; &lt&semi;a href&equals;&&num;8221&semi;&quest;lang&equals;en&&num;8221&semi;&gt&semi;en&lt&semi;&sol;a&gt&semi; &vert; &lt&semi;a href&equals;&&num;8221&semi;&quest;lang&equals;de&&num;8221&semi;&gt&semi;de&lt&semi;&sol;a&gt&semi; &lt&semi;&sol;span&gt&semi;<&sol;p>&NewLine;<p>WebContent&sol;WEB-INF&sol;views&sol;menu&period;jsp<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat;taglib uri&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;tags" prefix&equals;"spring"&percnt;&gt&semi; &NewLine; &NewLine;&lt&semi;spring&colon;message code&equals;"emp&period;label&period;menu"&gt&semi;&lt&semi;&sol;spring&colon;message&gt&semi;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Spring MVC Related Posts<&sol;b><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Web Tutorial<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interview Questions<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-mvc&sol;"><span style&equals;"color&colon; red&semi;">MVC Design Pattern<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;what-is-dispatcherservlet-in-spring-and-its-uses&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC DispatcherServlet <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC WebApplicationContext and Root Application Context<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;stereotype-annotations-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;Controller Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestmapping-annotation-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestMapping Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-annotation-in-spring-mvc-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestParam Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC ContextLoaderListener<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-vs-pathvariable-annotations-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestParam and &commat;PathVariable annotations<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-30-mvc-hello-world-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Hello World Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-exception-handling-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Exception Handling Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-with-hibernate-crud-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC with Hibernate CRUD Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-tiles-plugin-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Tiles Plugin with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-and-interceptor-with&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interceptor with example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;integration-spring-mvc3-and-mongodb&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC with MongoDB CRUD Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-internationalization&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Internationalization &amp&semi; Localization with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-and-interceptor-with&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;whats-new-in-spring-batch-2xx&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '613'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago