Skip to content

Commit a794db3

Browse files
lor6KevinGilmore
authored andcommitted
internationalization app (eugenp#1394)
1 parent dbc2c49 commit a794db3

7 files changed

Lines changed: 105 additions & 1 deletion

File tree

‎spring-boot/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
greeting=Hello! Welcome to our website!
2+
lang.change=Change the language
3+
lang.eng=English
4+
lang.fr=French
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
greeting=Bonjour! Bienvenue sur notre site!
2+
lang.change=Changez la langue
3+
lang.eng=Anglais
4+
lang.fr=Francais
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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>

0 commit comments

Comments
 (0)