如何从语言选择器更改整个Spring BOOT应用程序的语言



大家好,我得到了Spring启动应用程序,现在我正在尝试实现i18n。我已经创建了一个Configuration类,它实现了WebMvcConfigurer并为LocalResolver、MessageSource和Interceptors。当我从浏览器更改语言时,它可以完美地改变应用程序所有视图的语言。但现在的问题是,我写了一些语言选择器来更改语言,而它只为一个视图更改语言。如何实现为应用程序的所有视图分配选定的语言?非常感谢。

这是我的配置

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean(name = "localeResolver")
public LocaleResolver getLocaleResolver()  {
CookieLocaleResolver resolver= new CookieLocaleResolver();
resolver.setCookieDomain("myAppLocaleCookie");
// 60 minutes
resolver.setCookieMaxAge(60*60);
return resolver;
}
@Bean(name = "messageSource")
public MessageSource getMessageResource()  {
ReloadableResourceBundleMessageSource messageResource= new ReloadableResourceBundleMessageSource();
// Read i18n/messages_xxx.properties file.
// For example: i18n/messages_en.properties
messageResource.setBasename("classpath:i18n/messages");
messageResource.setDefaultEncoding("UTF-8");
return messageResource;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
}
}

这是我的家庭控制器映射

@GetMapping("/international")
public String getInternationalPage() {

return "home";
}

和宁静的我的百里香视图

<select id="locales">
<option value="choose language"></option>
<option value="en" th:text="english"></option>
<option value="hy" th:text="armenian"></option>
</select>

<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>

使用

@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}

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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
@ComponentScan(basePackages = "com.baeldung.internationalization.config")
public class MvcConfig implements WebMvcConfigurer {
@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());
}
}

创建这些语言文件

messages.properties
messages_vi.properties
messages_fr.properties
messages_kr.properties
...

示例源代码https://github.com/eugenp/tutorials/blob/master/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/internationalization/config/MvcConfig.java#L19

https://www.baeldung.com/spring-boot-internationalization

相关内容

  • 没有找到相关文章

最新更新