Erlo

技术点18:国际化

2020-11-18 15:00:13 发布   292 浏览  
页面报错/反馈
收藏 点赞

i18n 国际化(了解内容

一、什么是 i18n 国际化?

 

二、国际化相关要素介绍

 

三、国际化资源 properties 测试

配置两个语言的配置文件:

i18n_en_US.properties 英文:

username=username
password=password
sex=sex
age=age

i18n_zh_CN.properties 中文:

username=用户名
password=密码
sex=性别
age=年龄

国际化测试代码:

package com.zixue.i18n;

import org.junit.Test;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author Mr Guo
 * @create 2020-11-18 13:50
 */
public class I18nTest {
    @Test
    public void testLocale(){
        //获取系统默认的语言国家信息
//        Locale locale = Locale.getDefault();
//        System.out.println(locale);//zh_CN

        //获取所有国家的语言信息
//        for (Locale availableLocale : Locale.getAvailableLocales()){
//            System.out.println(availableLocale);
//        }

        //获取中文,中文的常量的Locale对象
        System.out.println(Locale.CHINA);//zh_CN
        //获取英文,英文的常量的Locale对象
        System.out.println(Locale.US);//en_US
    }

    @Test
    public void testi18n(){
        //得到我们需要的Locale对象
        Locale locale = Locale.CHINA;
        //通过指定的baseName和Locale对象,读取相应的配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("i18n", locale);

        System.out.println("username: " + bundle.getString("username"));
        System.out.println("password: " + bundle.getString("password"));
        System.out.println("sex: " + bundle.getString("sex"));
        System.out.println("age: " + bundle.getString("age"));
    }
}

四、通过请求头国际化页面

@ page import="java.util.Locale" %>
@ page import="java.util.ResourceBundle" %>
@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
html>
head>
meta http-equiv="pragma" content="no-cache" />
meta http-equiv="cache-control" content="no-cache" />
meta http-equiv="Expires" content="0" />
meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
title>Insert title heretitle>
head>
body>
    
        //从请求头获取Locale信息:语言
        Locale locale = request.getLocale();
        System.out.println(locale);
        //获取资源包(根据指定的baseName和Locale读取语言信息)
        ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
    %>

    a href="">中文a>|
    a href="">englisha>
    center>
        h1>=i18n.getString("regist")%>h1>
        table>
        form>
            tr>
                td>=i18n.getString("username")%>td>
                td>input name="username" type="text" />td>
            tr>
            tr>
                td>=i18n.getString("password")%>td>
                td>input type="password" />td>
            tr>
            tr>
                td>=i18n.getString("sex")%>td>
                td>input type="radio" />=i18n.getString("boy")%>
                    input type="radio" />=i18n.getString("girl")%>
                td>
            tr>
            tr>
                td>=i18n.getString("email")%>td>
                td>input type="text" />td>
            tr>
            tr>
                td colspan="2" align="center">
                input type="reset" value="reset")%>" />  
                input type="submit" value="submit")%>" />td>
            tr>
            form>
        table>
        br /> br /> br /> br />
    center>
    国际化测试:
    br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
    br /> 2、通过左上角,手动切换语言
body>
html>

 

五、通过显示的选择语言类型进行国际化

  对于普通用户来说,他是不知道在浏览器的设置页面修改语言的。我们可以通过链接的方式让用户自己选择。

@ page import="java.util.Locale" %>
@ page import="java.util.ResourceBundle" %>
@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
html>
head>
meta http-equiv="pragma" content="no-cache" />
meta http-equiv="cache-control" content="no-cache" />
meta http-equiv="Expires" content="0" />
meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
title>Insert title heretitle>
head>
body>
    
        //从请求头获取Locale信息:语言
        Locale locale = null;
        String country = request.getParameter("country");
        if ("cn".equals(country)){
            locale = Locale.CHINA;
        }else if ("usa".equals(country)){
            locale = Locale.US;
        }else{
            locale = request.getLocale();
        }
        //获取资源包(根据指定的baseName和Locale读取语言信息)
        ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
    %>

    a href="i18n.jsp?country=cn">中文a>|
    a href="i18n.jsp?country=usa">englisha>
    center>
        h1>=i18n.getString("regist")%>h1>
        table>
        form>
            tr>
                td>=i18n.getString("username")%>td>
                td>input name="username" type="text" />td>
            tr>
            tr>
                td>=i18n.getString("password")%>td>
                td>input type="password" />td>
            tr>
            tr>
                td>=i18n.getString("sex")%>td>
                td>input type="radio" />=i18n.getString("boy")%>
                    input type="radio" />=i18n.getString("girl")
登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认