java,WEB项目如何通过配置让其语言国际化

java,WEB项目如何通过配置让其语言国际化,第1张

可以使用用一些流行框架来解决国际化问题。

在Struts2中,有两种进行国际化 *** 作方式:

在Strutsproperties文件中设置处理各种字符编码或者语言的属性文件。并由此扩展这些属性文件,

让Web系统使用形成各种语言或信息格式化的配置信息。

直接装载Struts2提供的自带信息资源包也可以实现。

import javautil;

import javatext;

public class timeText {

/

@param args

/

public static void main(String[] args) {

// TODO Auto-generated method stub

Date now = new Date();

Calendar cal = CalendargetInstance();

DateFormat d1 = DateFormatgetDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)

String str1 = d1format(now);

DateFormat d2 = DateFormatgetDateTimeInstance();//获取系统时间格式

String str2 = d2format(now); //将时间格式转换成字符串

DateFormat d3 = DateFormatgetTimeInstance();

String str3 = d3format(now);

DateFormat d4 = DateFormatgetInstance(); //使用SHORT风格显示日期和时间

String str4 = d4format(now);

DateFormat d5 = DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL); //显示日期,周,时间(精确到秒)

String str5 = d5format(now);

DateFormat d6 = DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG); //显示日期。时间(精确到秒)

String str6 = d6format(now);

DateFormat d7 = DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT); //显示日期,时间(精确到分)

String str7 = d7format(now);

DateFormat d8 = DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM); //显示日期,时间(精确到分)

String str8 = d8format(now);//与SHORT风格相比,这种方式最好用

Systemoutprintln("用Date方式显示时间: " + now);//此方法显示的结果和CalendargetInstance()getTime()一样

Systemoutprintln("用DateFormatgetDateInstance()格式化时间后为:" + str1);

Systemoutprintln("用DateFormatgetDateTimeInstance()格式化时间后为:" + str2);

Systemoutprintln("用DateFormatgetTimeInstance()格式化时间后为:" + str3);

Systemoutprintln("用DateFormatgetInstance()格式化时间后为:" + str4);

Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL)格式化时间后为:" + str5);

Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG)格式化时间后为:" + str6);

Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT)格式化时间后为:" + str7);

Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM)格式化时间后为:" + str8);

}

}

运行结果:

用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008

用DateFormatgetDateInstance()格式化时间后为:2008-6-16

用DateFormatgetDateTimeInstance()格式化时间后为:2008-6-16 20:54:53

用DateFormatgetTimeInstance()格式化时间后为:20:54:53

用DateFormatgetInstance()格式化时间后为:08-6-16 下午8:54

用DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL)格式化时间后为

:2008年6月16日 星期一 下午08时54分53秒 CST

用DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG)格式化时间后为

:2008年6月16日 下午08时54分53秒

用DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT)格式化时间后

为:08-6-16 下午8:54

用DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM)格式化时间

后为:2008-6-16 20:54:53

或者直接获取毫秒,但是感觉与你问题无关

问题:关于一些大型项目中会使用不同语言版本的视图,比如在一个Java Web开发中,在struts中九提供了改种文件的配置项及在View中通过标签<bean:message value="">来根据浏览器不同语言形式来展示页面,实现多语言的互 *** 作。

这里自己将所遇到的这类问题,查看相关资料,通过JDK自带的相关API实现这种国际化 *** 作。

使用到的类主要有:import JavautilLocale; import javautilResourceBundle;还需要简单的国家化资源文件(针对不同语言有不同文件 properties文件,键值对)

比如新建一个包comxiaolitest,里面放置两个国际化文件,中文(LDPCproperties)和英文(LDPEproperties)支持:

[java] view plain copy

<p>sysloginindex=indexjsp\u9875</p>

[java] view plain copy

sysloginindex=indexjsp page

上面键值对是指:indexjsp 页

而后建立相关测试类实现如下:

[java] view plain copy

package comxiaolitest;

import javautilLocale;

import javautilResourceBundle;

public class ResourceMessage

{

private static ResourceMessage resourceMessage = null;

private ResourceBundle resourceMessage_zh = null;

private ResourceBundle resourceMessage_en = null;

private Locale local;

//此时该国际化资源文件为下面的完全限定类名访问,LDP为资源文件前缀

private static final String SOURCE_FILE_ZH= "comxiaolitestLDPC";

private static final String SOURCE_FILE_EN= "comxiaolitestLDPE";

private ResourceMessage()

{

//分别将两个文件绑定到相应的语言环境下

resourceMessage_zh = ResourceBundlegetBundle(SOURCE_FILE_ZH, LocaleCHINA);

resourceMessage_en = ResourceBundlegetBundle(SOURCE_FILE_EN,

LocaleENGLISH);

}

/

单例模式

只创建一个实例

@return

@see [类、类#方法、类#成员]

/

public static ResourceMessage getInstance()

{

if (null == resourceMessage)

{

synchronized (ResourceMessageclass)

{

resourceMessage = new ResourceMessage();

}

}

return resourceMessage;

}

public String getMessage(String key)

{

return getMessage(key, local);

}

public String getMessage(String key, Locale local)

{

if (null == local)

{

local = LocalegetDefault();

}

String msg = null;

if (LocaleCHINAequals(local))

{

msg = resourceMessage_zhgetString(key);

}

else if (LocaleENGLISHequals(local))

{

msg = resourceMessage_engetString(key);

}

//如果找不到资源文件,返回key

if (null == msg || ""equals(msg))

{

return key;

}

return msg;

}

/ 测试方法

@param args

@see [类、类#方法、类#成员]

/

public static void main(String[] args)

{

String key = "sysloginindex";

// String value = ResourceMessagegetInstance()getMessage(key, LocaleENGLISH);

//使用当前默认的语言环境获取,这里为中文支持

String value = ResourceMessagegetInstance()getMessage(key);

Systemoutprintln(value);

}

}

结果,根据手动设置的不同语言环境可以打印不同的值:

indexjsp页 和 indexjsp page

javautilLocale locale = LocalegetDefault();这个会根据当前系统的语言生成locale

1、获取本地的locale语言,

2、然后读取资源文件

存放的路径不对, properties 要和class 一起(有包,则在顶层---class输出目录)

另外

String input=scnext();

switch(input){

case "1":

要换成 intinput=scnextInt() , switch 相应修改一下

以上就是关于java,WEB项目如何通过配置让其语言国际化全部的内容,包括:java,WEB项目如何通过配置让其语言国际化、java中如何对时间做国际化处理啊、如何创建java国际化类资源文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9283481.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存