永久免费网站系统,找合伙人的网站做淘宝,益保网做推广网站吗?,杭州做网站的公司排行文章目录 i18n概述Java国际化Spring6国际化MessageSource接口使用Spring6国际化 i18n概述
国际化也称作i18n#xff0c;其来源是英文单词 internationalization的首末字符i和n#xff0c;18为中间的字符数。由于软件发行可能面向多个国家#xff0c;对于不同国家的用户其来源是英文单词 internationalization的首末字符i和n18为中间的字符数。由于软件发行可能面向多个国家对于不同国家的用户软件显示不同语言的过程就是国际化。通常来讲软件中的国际化是通过配置文件来实现的假设要支撑两种语言那么就需要两个版本的配置文件。
Java国际化
1Java自身是支持国际化的java.util.Locale用于指定当前用户所属的语言环境等信息java.util.ResourceBundle用于查找绑定对应的资源文件。Locale包含了language信息和country信息Locale创建默认locale对象时使用的静态方法 /*** This method must be called only for creating the Locale.** constants due to making shortcuts.*/private static Locale createConstant(String lang, String country) {BaseLocale base BaseLocale.createInstance(lang, country);return getInstance(base, null);}2配置文件命名规则 basename_language_country.properties 必须遵循以上的命名规则java才会识别。其中basename是必须的语言和国家是可选的。这里存在一个优先级概念如果同时提供了messages.properties和messages_zh_CN.propertes两个配置文件如果提供的locale符合en_CN那么优先查找messages_en_CN.propertes配置文件如果没查找到再查找messages.properties配置文件。最后提示下所有的配置文件必须放在classpath中一般放在resources目录下 3实验演示Java国际化
第一步 引入spring依赖 第二步 在resource目录下创建两个配置文件messages_zh_CN.propertes和messages_en_GB.propertes 第三步 测试
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.ResourceBundle;public class Demo1 {public static void main(String[] args) {System.out.println(ResourceBundle.getBundle(messages,new Locale(en,GB)).getString(test));System.out.println(ResourceBundle.getBundle(messages,new Locale(zh,CN)).getString(test));}
}Spring6国际化
MessageSource接口
spring中国际化是通过MessageSource这个接口来支持的
常见实现类
ResourceBundleMessageSource
这个是基于Java的ResourceBundle基础类实现允许仅通过资源名加载国际化资源
ReloadableResourceBundleMessageSource
这个功能和第一个类的功能类似多了定时刷新功能允许在不重启系统的情况下更新资源的信息
StaticMessageSource
它允许通过编程的方式提供国际化信息一会我们可以通过这个来实现db中存储国际化信息的功能。
使用Spring6国际化
第一步 创建资源文件
国际化文件命名格式基本名称 _ 语言 _ 国家.properties
{0},{1}这样内容就是动态参数 1创建test_en_US.properties
www.test.comwelcome {0},时间:{1}2创建test_zh_CN.properties
www.test.com欢迎 {0},时间:{1}第二步 创建spring配置文件配置MessageSource
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idmessageSourceclassorg.springframework.context.support.ResourceBundleMessageSourceproperty namebasenameslistvaluetest/value/list/propertyproperty namedefaultEncodingvalueutf-8/value/property/bean
/beansimport org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
import java.util.Locale;public class Demo2 {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(beans.xml);//传递动态参数使用数组形式对应{0} {1}顺序Object[] objs new Object[]{atguigu,new Date().toString()};//www.test.com为资源文件的key值,//objs为资源文件value值所需要的参数,Local.CHINA为国际化为语言String strcontext.getMessage(www.test.com, objs, Locale.CHINA);System.out.println(str);}
}