File tree Expand file tree Collapse file tree
java/com/baeldung/internationalization Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1212 <parent >
1313 <groupId >org.springframework.boot</groupId >
1414 <artifactId >spring-boot-starter-parent</artifactId >
15- <version >1.5.1 .RELEASE</version >
15+ <version >1.5.2 .RELEASE</version >
1616 <relativePath /> <!-- lookup parent from repository -->
1717 </parent >
1818
Original file line number Diff line number Diff line change 1+ package com .baeldung .internationalization ;
2+
3+ import javax .annotation .security .RolesAllowed ;
4+
5+ import org .springframework .boot .SpringApplication ;
6+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
7+
8+ @ SpringBootApplication
9+ public class InternationalizationApp {
10+ @ RolesAllowed ("*" )
11+ public static void main (String [] args ) {
12+ System .setProperty ("security.basic.enabled" , "false" );
13+ SpringApplication .run (InternationalizationApp .class , args );
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .internationalization .config ;
2+
3+ import java .util .Locale ;
4+
5+ import org .springframework .context .annotation .Bean ;
6+ import org .springframework .context .annotation .ComponentScan ;
7+ import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .web .servlet .LocaleResolver ;
9+ import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
10+ import org .springframework .web .servlet .config .annotation .InterceptorRegistry ;
11+ import org .springframework .web .servlet .config .annotation .WebMvcConfigurerAdapter ;
12+ import org .springframework .web .servlet .i18n .LocaleChangeInterceptor ;
13+ import org .springframework .web .servlet .i18n .SessionLocaleResolver ;
14+
15+ @ Configuration
16+ @ EnableWebMvc
17+ @ ComponentScan (basePackages = "com.baeldung.internationalization.config" )
18+ public class MvcConfig extends WebMvcConfigurerAdapter {
19+
20+ @ Bean
21+ public LocaleResolver localeResolver () {
22+ SessionLocaleResolver slr = new SessionLocaleResolver ();
23+ slr .setDefaultLocale (Locale .US );
24+ return slr ;
25+ }
26+
27+ @ Bean
28+ public LocaleChangeInterceptor localeChangeInterceptor () {
29+ LocaleChangeInterceptor lci = new LocaleChangeInterceptor ();
30+ lci .setParamName ("lang" );
31+ return lci ;
32+ }
33+
34+ @ Override
35+ public void addInterceptors (InterceptorRegistry registry ) {
36+ registry .addInterceptor (localeChangeInterceptor ());
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .internationalization .config ;
2+
3+ import org .springframework .stereotype .Controller ;
4+ import org .springframework .web .bind .annotation .GetMapping ;
5+
6+ @ Controller
7+ public class PageController {
8+
9+ @ GetMapping ("/international" )
10+ public String getInternationalPage () {
11+ return "international" ;
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ greeting =Hello! Welcome to our website!
2+ lang.change =Change the language
3+ lang.eng =English
4+ lang.fr =French
Original file line number Diff line number Diff line change 1+ greeting =Bonjour! Bienvenue sur notre site!
2+ lang.change =Changez la langue
3+ lang.eng =Anglais
4+ lang.fr =Francais
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html xmlns:th ="http://www.thymeleaf.org ">
3+ < head >
4+ < meta charset ="ISO-8859-1 " />
5+ < title > Home</ title >
6+ < script src ="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js "> </ script >
7+ < script type ="text/javascript ">
8+ $ ( document ) . ready ( function ( ) {
9+ $ ( "#locales" ) . change ( function ( ) {
10+ var selectedOption = $ ( '#locales' ) . val ( ) ;
11+ if ( selectedOption != '' ) {
12+ window . location . replace ( 'international?lang=' + selectedOption ) ;
13+ }
14+ } ) ;
15+ } ) ;
16+ </ script >
17+ </ head >
18+ < body >
19+ < h1 th:text ="#{greeting} "> </ h1 >
20+
21+ < br /> < br />
22+ < span th:text ="#{lang.change} "> </ span > :
23+ < select id ="locales ">
24+ < option value =""> </ option >
25+ < option value ="en " th:text ="#{lang.eng} "> </ option >
26+ < option value ="fr " th:text ="#{lang.fr} "> </ option >
27+ </ select >
28+ </ body >
29+ </ html >
You can’t perform that action at this time.
0 commit comments