很多网站的用户分布在世界各地,因此网站需要针对不同国家的用户展示不同语言的内容,因此就有了国际化实现的需求,大多数网站都会在网站的头部或尾部设置语言切换链接,这样就可以直接切换成相应的内容。
国际化实现步骤
1.基于不同的语言定义多个properties文件,用于后面根据本地化信息从相应的properties文件中获取数据。
这里我实现的是简体中文和英文的切换,所以定义了两个properties文件,这两个文件均在根目录下
language_en_US.properties
language.cn = \u4e2d\u6587
language.en = English
internationalisation = \u0020Internationalisation
welcome = This is the English environment
introduce= This is I18N Demo
language_zh_CN.properties
language.cn = \u4e2d\u6587
language.en = English
internationalisation = \u56fd\u9645\u5316
welcome = \u8fd9\u662f\u4e2d\u6587\u73af\u5883
introduce= \u8fd9\u662f\u56fd\u9645\u5316\u7684\u4e8b\u4f8b
2.编辑Spring配置文件,在原有基础上添加国际化资源文件和本地化信息
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
3.测试
(1)新建一个controller,用于跳转到指定的国际化测试页面
@Controller
public class Test1 {
@RequestMapping("/hello.action")
public String index() {
System.out.println("进入hello.action");
return "content/hello.jsp";
}
}
(2)新建hello.jsp页面
SpringMVCLanguage:
当前语言: ${pageContext.response.locale }