internationalization app (#1394)
This commit is contained in:
parent
dbc2c49fe2
commit
a794db3183
@ -12,7 +12,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>1.5.1.RELEASE</version>
|
<version>1.5.2.RELEASE</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.internationalization;
|
||||||
|
|
||||||
|
import javax.annotation.security.RolesAllowed;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class InternationalizationApp {
|
||||||
|
@RolesAllowed("*")
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.setProperty("security.basic.enabled", "false");
|
||||||
|
SpringApplication.run(InternationalizationApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.internationalization.config;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||||
|
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
@ComponentScan(basePackages = "com.baeldung.internationalization.config")
|
||||||
|
public class MvcConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleResolver localeResolver() {
|
||||||
|
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||||
|
slr.setDefaultLocale(Locale.US);
|
||||||
|
return slr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||||
|
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
||||||
|
lci.setParamName("lang");
|
||||||
|
return lci;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(localeChangeInterceptor());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.internationalization.config;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class PageController {
|
||||||
|
|
||||||
|
@GetMapping("/international")
|
||||||
|
public String getInternationalPage() {
|
||||||
|
return "international";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
4
spring-boot/src/main/resources/messages.properties
Normal file
4
spring-boot/src/main/resources/messages.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
greeting=Hello! Welcome to our website!
|
||||||
|
lang.change=Change the language
|
||||||
|
lang.eng=English
|
||||||
|
lang.fr=French
|
4
spring-boot/src/main/resources/messages_fr.properties
Normal file
4
spring-boot/src/main/resources/messages_fr.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
greeting=Bonjour! Bienvenue sur notre site!
|
||||||
|
lang.change=Changez la langue
|
||||||
|
lang.eng=Anglais
|
||||||
|
lang.fr=Francais
|
29
spring-boot/src/main/resources/templates/international.html
Normal file
29
spring-boot/src/main/resources/templates/international.html
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="ISO-8859-1" />
|
||||||
|
<title>Home</title>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(document).ready(function() {
|
||||||
|
$("#locales").change(function () {
|
||||||
|
var selectedOption = $('#locales').val();
|
||||||
|
if (selectedOption != ''){
|
||||||
|
window.location.replace('international?lang=' + selectedOption);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 th:text="#{greeting}"></h1>
|
||||||
|
|
||||||
|
<br /><br />
|
||||||
|
<span th:text="#{lang.change}"></span>:
|
||||||
|
<select id="locales">
|
||||||
|
<option value=""></option>
|
||||||
|
<option value="en" th:text="#{lang.eng}"></option>
|
||||||
|
<option value="fr" th:text="#{lang.fr}"></option>
|
||||||
|
</select>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user