2016-04-10 79 views
6

Tôi đang cố gắng tạo một ứng dụng đa ngôn ngữ bằng cách sử dụng Spring boot và Thymeleaf.I18n trong Spring boot + Thymeleaf

Tôi đã tạo một vài tệp thuộc tính để lưu các thông báo khác nhau nhưng tôi chỉ có thể hiển thị nó bằng ngôn ngữ trình duyệt của mình (tôi đã thử mở rộng để thay đổi ngôn ngữ trình duyệt nhưng dường như chúng không hoạt động). trong trang web của tôi để thực hiện nhiệm vụ này (thay đổi ngôn ngữ), nhưng tôi không biết cách thức hoặc nơi để tìm cách quản lý điều này.

Gonna cho bạn thấy cấu hình của tôi:

Cấu trúc của dự án

Structure of the project


lớp cấu hình i18n

import org.springframework.context.MessageSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.support.ResourceBundleMessageSource; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@Configuration 
@EnableWebMvc 
public class I18nConfiguration extends WebMvcConfigurerAdapter { 

    @Bean 
    public MessageSource messageSource() { 
     ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); 
     messageSource.setBasename("i18n/messages"); 
     messageSource.setDefaultEncoding("UTF-8"); 
     return messageSource; 
    } 

} 

trang Thymleaf HTML

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org" 
    th:with="lang=${#locale.language}" th:lang="${lang}"> 

<head> 
<title>Spring Boot and Thymeleaf example</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body> 
    <h3>Spring Boot and Thymeleaf</h3> 
    <p>Hello World!</p> 
    <p th:text="${nombre}"></p> 
    <h1 th:text="#{hello.world}">FooBar</h1> 
</body> 
</html> 

Messages (file Thuộc tính)

messages_en_US.properties

hello.world = Hello people 

messages_es.properties

hello.world = Hola gente 

Thực sự thông báo được hiển thị bằng tiếng Tây Ban Nha, không chắc chắn làm cách nào tôi thay đổi điều này, vì vậy nếu bạn có thể giúp tôi cảm ơn bạn rất nhiều.

Có một câu hỏi khác xuất hiện trong tâm trí của tôi ... Làm cách nào tôi nhận được thông báo từ Cơ sở dữ liệu thay vì từ tệp thuộc tính?

+0

Câu hỏi ngu ngốc: bạn đặt 'Locale' như thế nào? Bạn đã cấu hình ['LocaleResolver'] (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/LocaleResolver.html) chưa? Làm thế nào bạn chuyển nó sang ngôn ngữ bạn muốn? –

+0

Tôi không, tôi đoán Spring đang lấy nó từ cấu hình trình duyệt. Tôi có nên đặt ngôn ngữ tùy thuộc vào nút bấm không? Không chắc chắn nếu nó sẽ làm việc theo cách này. –

+1

Bắt đầu ở [trên cùng] (http://www.concretepage.com/spring-4/spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example) và tiếp tục.Có 3 thứ bạn cần 1) 'MessageSource', 2)' LocaleResolver' để xác định một yêu cầu cụ thể, miền địa phương cần dùng, và (tùy chọn) 3) 'LocaleChangeInterceptor' để bạn có thể, từ một yêu cầu, đặt ngôn ngữ trong 'LocaleResolver'. –

Trả lời

9

ứng dụng của bạn nên kéo dài WebMvcConfigurerAdapter

@SpringBootApplication 
public class NerveNetApplication extends WebMvcConfigurerAdapter { 

public static void main(String[] args) { 
    SpringApplication.run(NerveNetApplication.class, args); 
} 

@Bean 
public LocaleResolver localeResolver() { 
    return new CookieLocaleResolver(); 
} 



@Bean 
public LocaleChangeInterceptor localeChangeInterceptor() { 
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); 
    lci.setParamName("lang"); 
    return lci; 
} 


@Override 
public void addInterceptors(InterceptorRegistry registry) { 
    registry.addInterceptor(localeChangeInterceptor()); 
} 

}

Sau đó trên trình duyệt của bạn có thể chuyển đổi ngôn ngữ với param lang Ví dụ: http://localhost:1111/?lang=kh mà messages_kh.properites sẽ lưu trữ các nội dung của Tiếng Khmer.

Các vấn đề liên quan