• java----程序国际化


    主程序

    package com.zy;
    import java.text.MessageFormat;
    import java.util.Locale;
    import java.util.ResourceBundle;
    import java.util.Scanner;
    
    public class Demo {
    	public static void main(String[] args) {
    		//创建一个本地语言环境
    		Locale locale_CN = new Locale("zh","CN");
    		Locale locale_US= new Locale("en","US");
    		//根据操作系统默认选择语言环境
    		//Locale locale_default= Locale.getDefault();
    		
    		//如果后面不加参数,找系统默认的语言的配置文件(info_zh_CN) ,ResourceBundle只能读取配置文件,不能写入
    		ResourceBundle r = ResourceBundle.getBundle("com.property.info");
    		
    		//找英语中的配置文件(info_en_US)
    		//ResourceBundle r = ResourceBundle.getBundle("com.property.info",locale_US);
    		
    		//通过配置文件字段(key)读取的value
    		//System.out.println(r.getString("input.username"));
    		
    		
    		//示例
    		System.out.println(r.getString("System.name"));
    		Scanner input = new Scanner(System.in);
    		System.out.println(r.getString("input.username"));
    		String username = input.nextLine();
    		System.out.println(r.getString("input.password"));
    		String password = input.next();
    		if("admin".equals(username)&&"111".equals(password)){
    			System.out.println(r.getString("login.success"));
    			//动态文本格式化
    			String s = r.getString("welcom");
    			s = MessageFormat.format(s, username);
    			System.out.println(s);
    			
    		}else{
    			System.out.println(r.getString("login.error"));			
    		}
    	}
    
    }

    新建一个包(com.property),里面放入配置文件(语言数据)

    在该包下新建一个文件info_zh_CN.properties

    System.name=u5458u5DE5u7BA1u7406u7CFBu7EDF
    input.username=u8BF7u8F93u5165u7528u6237u540DuFF1A
    input.password=u8BF7u8F93u5165u5BC6u7801uFF1A
    login.success=u767Bu5F55u6210u529F
    login.error=u767Bu5F55u5931u8D25
    welcom=u6B22u8FCEu4F60uFF0C{0}
    

    在该包下新建一个文件info_en_US.properties

    System.name = EMP Manage System
    input.username = Input UserName:
    input.password = Input PassWord:
    login.success = Login Success
    login.error = Login Error
    welcom=WelCom,{0}
    

      

    spring  MVC配置程序国际化

    如果需要配置程序国际化(此时是根据浏览器设置的语言,来进行匹配的)

    1、配置

    1.1、在resource目录下添加 info_zh_CN.properties、info_en_US.properties。等等。

    1.2、在spring-MVC.xml中添加

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="info"></property>
    </bean>

    2、获取国际胡中的值

    在java代码中使用

        @Autowired //自动注入
        private ResourceBundleMessageSource messageSource;
        
        @RequestMapping(value = "/languagetest",method = RequestMethod.GET)
        public String Languagetest(Locale locale){
            System.out.println(locale);//zh_CN,根据浏览器获得
            String message = messageSource.getMessage("input.password",null,locale);//null:args
            System.out.println(message);
            return "login";
        }

    在html中使用:(JSTL格式化标签用来格式化并输出文本),此时用IE浏览器打开,设置不同的语言,显示的文本是不一样的

    <fmt:message key="input.password"></fmt:message>

    3、如果需要配置程序国际化(此时手动(需要传入语言参数)进行中英文切换)

    方式1、如果不需要配置spring-MVC.xml

    @RequestMapping(value = "/languagetest",method = RequestMethod.GET)
    public String Languagetest(Locale locale){}  //请求参数需要有locale
    

    url需要带上locale,指定语言

    http://localhost:8080/languagetest?locale=zh_CH  //中文
    http://localhost:8080/languagetest?locale=en_US  //英文
    

    方式2、配置spring-MVC.xml

    此时就不需要在参数定一个 Locale locale

        <!--配置SessionLocaleResolver-->
        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
        <!--配置拦截器-->
        <mvc:interceptors>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
        </mvc:interceptors>
    

    url需要带上locale,指定语言

    http://localhost:8080/languagetest?locale=zh_CH  //中文
    http://localhost:8080/languagetest?locale=en_US  //英文
    

    工作原理

    测试

    1、配置info_en_US.properties和info_en_US.properties

    2、html(我们使用html来获取配置文件数据)

    <fmt:message key="input.username"></fmt:message>
    

      

    spring boot 配置国际化

    可以查看 MessageSourceAutoConfiguration这个类的源码,理解spring boot帮我们自动配置了spring mvc中需要配置的 ResourceBundleMessageSource,

    1、配置

    1、先在resource目录创建i18n目录,在该目录下添加 info_zh_CN.properties、info_en_US.properties、可以添加一个info.properties(表示默认)等等。

    2、application.properties配置路径(默认的basename是message,所以我们也可以在resources下直接创建message.properties)

    spring.messages.basename=i18n.info

    3、编写国际资源文件(快捷)

    2、获取国际化的值

    1、在html页面上使用

    [(#{email})]   //[()]、[[]]                                                                //直接输出值是thymeleaf语法,#{}获取国际话的值
    <input type="email" class="form-control" placeholder="email" th:placeholder="#{email}">    //使用th:xx 来替换值
    

      

    3、中英文切换

    方式1;注意该类名只能是 LocaleResolver,此时注册到容器中的 id = localeResolver

    @Component
    public class LocaleResolver implements org.springframework.web.servlet.LocaleResolver {
        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String str_locale = request.getParameter("locale");
            Locale locale = Locale.getDefault();  //注意设置了,这个所以我们即使在浏览器中设置语言,也没有效果了,如果需要有效果,可以在请求头中获取语言信息,设置到locale中;
            if (str_locale!=null){
                String[] s = str_locale.split("_");
                locale = new Locale(s[0],s[1]);
            }
            return locale;
        }
    
        @Override
        public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    
        }
    }
    

    方式2;类名可以任意

    public class MyLocaleResolver implements LocaleResolver {
        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String str_locale = request.getParameter("locale");
            Locale locale = Locale.getDefault();
            if (str_locale!=null){
                String[] s = str_locale.split("_");
                locale = new Locale(s[0],s[1]);
            }
            return locale;
        }
    
        @Override
        public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    
        }
    }
    

      方法名必须是localeResolver

    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer {
        @Bean
        public LocaleResolver localeResolver(){
            return new MyLocaleResolver();
        }
    }
    

    测试:每一个url后面都需要加上参数,每一个页面才都可以中英文切换

    http://localhost:8080/login?locale=en_US
    

      

  • 相关阅读:
    C#中处理鼠标和键盘的事件
    C#中处理鼠标和键盘的事件
    C#中处理鼠标和键盘的事件
    mpich2安装
    算法题推箱子
    LINUX终端下windows盘的位置
    Linux头文件和库文件添加环境变量与GCC编译器添加INCLUDE与LIB环境变量
    第九章顺序容器重学C++之《 C++ PRIMER》
    sed中使用变量
    抛出异常
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/10688705.html
Copyright © 2020-2023  润新知