zoukankan
html css js c++ java
国际化问题
在这里面使用JSP来利用property文件应用的一个能显示多国语言的页面,
仅一个JSP和4个properties配置文件
<%
@ page import
=
"
java.io.*
"
%>
<%
@ page import
=
"
java.util.*
"
%>
<%
@ page import
=
"
java.awt.*
"
%>
<%
String
languageDefault
=
Locale.getDefault().getLanguage();
String
countryDefault
=
Locale.getDefault().getCountry();
Locale defaultLocale
=
null
;
String
charset
=
null
;
if
(languageDefault
==
"
en
"
|| languageDefault.equals(
"
en
"
)){
defaultLocale
=
Locale.ENGLISH;
charset
=
"
iso-8859-1
"
;
}
else
if
(countryDefault
==
"
CN
"
|| countryDefault.equals(
"
CN
"
)){
defaultLocale
=
Locale.SIMPLIFIED_CHINESE;
charset
=
"
gb2312
"
;
}
else
if
(countryDefault
==
"
TW
"
|| countryDefault.equals(
"
TW
"
)){
defaultLocale
=
Locale.TRADITIONAL_CHINESE;
charset
=
"
big5
"
;
}
else
{
System.out.println(
"
LanguageDefault not equals en.CountryDefault not equals TW or CN
"
);
}
if
(session.isNew()){
session.setAttribute(
"
locale
"
,defaultLocale);
session.setAttribute(
"
charset
"
,charset);
}
else
{
String
language
=
request.getParameter(
"
language
"
);
if
(language
==
null
|| language.equals(
null
)){
session.setAttribute(
"
locale
"
,defaultLocale);
session.setAttribute(
"
charset
"
,charset);
}
else
{
Locale locale
=
null
;
if
(language.equals(
"
Locale.SIMPLIFIED_CHINESE
"
) || language.equals(
"
Locale.SIMPLIFIED_CHINESE
"
)){
locale
=
Locale.SIMPLIFIED_CHINESE;
charset
=
"
gb2312
"
;
}
else
if
(language.equals(
"
Locale.TRADITIONAL_CHINESE
"
) || language.equals(
"
Locale.TRADITIONAL_CHINESE
"
)){
locale
=
Locale.TRADITIONAL_CHINESE;
charset
=
"
big5
"
;
}
else
if
(language.equals(
"
Locale.ENGLISH
"
) || language.equals(
"
Locale.ENGLISH
"
)){
locale
=
Locale.ENGLISH;
charset
=
"
iso-8859-1
"
;
}
session.setAttribute(
"
locale
"
,locale);
session.setAttribute(
"
charset
"
,charset);
}
}
Locale locale
=
(Locale)session.getAttribute(
"
locale
"
);
ResourceBundle messagesBundle
=
ResourceBundle.getBundle(
"
propertyFiles/uni_property
"
,locale);
//
资源文件放在propertyFiles包下
String
localeCountry
=
locale.getCountry();
String
localeLanguage
=
locale.getLanguage();
System.out.println(
"
locale:
"
+
locale.toString());
System.out.println(
"
localeCountry:
"
+
localeCountry);
System.out.println(
"
localeLanguage:
"
+
localeLanguage);
if
(localeLanguage
==
"
en
"
){
response.setContentType(
"
text/html; charset=ISO8859-1
"
);
}
else
if
(localeCountry
==
"
CN
"
){
response.setContentType(
"
text/html; charset=gb2312
"
);
}
else
if
(localeCountry
==
"
TW
"
){
response.setContentType(
"
text/html; charset=big5
"
);
}
%>
<
html
>
<
head
>
<
meta
name
="GENERATOR"
content
="Microsoft FrontPage 4.0"
>
<
meta
name
="ProgId"
content
="FrontPage.Editor.Document"
>
<
title
>
Internationzational by propery file
</
title
>
<
base
target
="contents"
>
</
head
>
<
body
>
<
table
border
="0"
width
="796"
style
="font-size: 10pt"
cellspacing
="0"
cellpadding
="0"
height
="60"
>
<
tr
rowspan
=5
><
td
colspan
=3
>
</
td
></
tr
>
<
tr
rowspan
=5
><
td
colspan
=3
>
</
td
></
tr
>
<
tr
>
<
td
width
="196"
height
="68"
></
td
>
<
td
width
="470"
height
="64"
>
<
p
align
="center"
><
a
href
="#"
onclick
="href='uni_property.jsp?language=Locale.SIMPLIFIED_CHINESE'"
>
<%
=
messagesBundle.getString(
"
simplifiedChinese
"
)
%>
</
a
>
|
<
a
href
="#"
onclick
="href='uni_property.jsp?language=Locale.TRADITIONAL_CHINESE'"
>
<%
=
messagesBundle.getString(
"
traditionalChinese
"
)
%>
</
a
>
|
<
a
href
="#"
onclick
="href='uni_property.jsp?language=Locale.ENGLISH'"
>
<%
=
messagesBundle.getString(
"
english
"
)
%>
</
a
></
td
>
<
td
width
="130"
height
="56"
>
<
p
align
="center"
>
<%
=
messagesBundle.getString(
"
username
"
)
%>
</
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
四个资源文件如下
#uni_property.properties
username=JSP Developer
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
#uni_property_en.properties
username=JSP Developer
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
#uni_property_zh_CN.properties
username=JSP\u5f00\u53d1\u8005
simplifiedChinese=\u7b80\u4f53\u4e2d\u6587
traditionalChinese=\u7e41\u4f53\u4e2d\u6587
english=\u82f1\u6587
#uni_property_zh_TW.properties
username=JSP\u958b\u767c\u8005
simplifiedChinese=\u7c21\u9ad4\u4e2d\u6587
traditionalChinese=\u7e41\u9ad4\u4e2d\u6587
english=\u82f1\u6587
在同连接上单击会出现不同语言界面,另外需要注意的是
ResourceBundle messagesBundle
=
ResourceBundle.getBundle(
"
propertyFiles/uni_property
"
,locale);
//
资源文件放在propertyFiles包下
该方法查找的起始位置为默认的包,也就是没有名字的包,我们这里放属性文件放在propertyFiles包下,所以为propertyFiles/
另外在属性文件中使用的是Unicode编码,它支持互相转换,处理和显示现在世界上用不同语言写的文本。
查看全文
相关阅读:
JS正则与PHP正则
关于微信扫码支付的流程
Jquery快速入门
phpstorm快捷键大全
CentOS 7.3 下部署基于 Node.js的微信小程序商城
一个故事告诉你比特币的原理及运作机制 (转 2013)
mysql The used table type doesn’t support FULLTEXT indexes 解决方案 (phpstudy 会出现),coten不会
linux下使用 du查看某个文件或目录占用磁盘空间的大小
◆织梦内容管理系统模板标签代码参考
Linux 下挂载新硬盘方法(转)
原文地址:https://www.cnblogs.com/eflylab/p/622149.html
最新文章
leetcode 343. 整数拆分:动态规划(c++)
并查集(Union Find):实现及其优化(c++)
BZOJ 2733: [HNOI2012]永无乡
重逢可持久化线段树
过去的2019
POJ 2279
图的创建与dfs,bfs遍历
树的创建与遍历
堆的实现
利用二叉链表递归和非递归算法求叶子结点的数量
热门文章
算法分析第三次作业
算法分析第二次作业
算法分析第一次作业
冬季训练8 AB题题解
重新安装了环境报错{"error":"could not find driver"}
php操作excel表格的导入和导出
关于使用jwt编写接口时候对token判断时候错误的机制处理
中间件方法必须返回Response对象实例(tp5.1+小程序结合时候出的问题)
关于微信登录授权获取unionid的方法
数据库优化策略及不经过索引的方法
Copyright © 2011-2022 走看看