• j2ee国际化数据方式


    静态国际化数据,在msg(定义数据名)_zh(语言缩写)_CN(国家缩写).propertices文件中自定义相关数据

    然后进行调用

      /*
         * 静态国际化
         */
        @Test
        public void testInternational_static() throws Exception {
            //当前语言环境
            // Locale locale = Locale.CHINA;
            Locale locale = Locale.US;
            //创建工具类对象ResourceBundle
            ResourceBundle resourceBundle = ResourceBundle.getBundle("com.xinzhi.test.msg"
                    , locale);
            String string = resourceBundle.getString("hello");
            System.out.println(string);
            
        }

    下面是其他类型数据的国际化方法

      /*
         * 数字国际化
         */
        @Test
        public void testInternational_number() throws Exception {
            // Locale locale = Locale.CHINA;
            Locale locale = Locale.US;
            NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
            String format = numberFormat.format(1000000000);
            System.out.println(format);
        }
    
        /*
         * 货币数字国际化
         */
        @Test
        public void testInternational_currency() throws Exception {
            // Locale locale = Locale.CHINA;
            Locale locale = Locale.US;
            NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
            String format = numberFormat.format(100);
            System.out.println(format);
        }
    
        /*
         * 货币数字计算$100*10
         */
        @Test
        public void testInternational_currency2() throws Exception {
            // Locale locale = Locale.CHINA;
            Locale locale = Locale.US;
            String currencyString = "$100";
            int num = 10;
            NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
            Number parse = numberFormat.parse(currencyString);
            System.out.println(parse.intValue() * num);
        }
    
        /*
         * 时间国际化
         */
        @Test
        public void testInternational_time() throws Exception {
            // Locale locale = Locale.CHINA;
            Locale locale = Locale.US;
            int dateStyle = DateFormat.FULL;
            int timeStyle = DateFormat.SHORT;
            DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(dateStyle,
                    timeStyle, locale);
            String format = dateTimeInstance.format(new Date());
            System.out.println(format);
        }

     在jsp中使用国际化:

    将资源文件在msg(定义数据名)_zh(语言缩写)_CN(国家缩写).propertices处理完成后

    在需要国际化的jsp界面中插入如下代码:

    <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>//用于加载jstl
    <fmt:setLocale value="${pageContext.request.locale }"/>//设置使用的语言场景(在请求域中获取)
    <fmt:setBundle basename="com.xinzhi.utils.msg" var="varbundle"/>//拿到资源文件路径,定义资源变量
    <fmt:message key="title" bundle="${varbundle}"></fmt:message>//根据键来获取相应的值

    /**
    * jsp中数字国际化,保留两位有效数字,没有的用0填充,用#.##则表示保留两位数字但是不保留0
    */
    <fmt:formatNumber pattern="0.00" value="100"></fmt:formatNumber>
    /**
    * jsp中日期国际化
    */
    <fmt:formatDate value="2017-05-13" pattern="yyyy-MM-dd"/>
  • 相关阅读:
    linux中ll和du的区别
    django+celery+redis环境搭建
    python中若干错误
    js正则表达式中匹配反引号
    yii学习小结
    linux下DNS设置以及解析顺序
    apache中若干模块的安装
    HTML基础
    selenium
    selenium
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6901185.html
Copyright © 2020-2023  润新知