「国际化java」国际化标准化比值是什么
本篇文章给大家谈谈国际化java,以及国际化标准化比值是什么对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java 多语言国际化
国际化的英文为Internationalization,这个也太长了,所以它又称为I18n(英文单词 internationalization的首末字符i和n,18为中间的字符数)。
除了i18n还有L10n(localization),g11n(globalization),还有m17n(multilingualization),它们的区别是:
i18n支持多种语言,但是同一时间只能是英文和一种选定的语言,例如英文+中文、英文+德文、英文+韩文等等;
L10n(localization),支持2种语言,英文和另外一种语言(例如中文);
g11n(globalization),简单的理解可以认为g11n = i18n + L10n。
m17n(multilingualization)可以在同一时间支持多种语言,例如你可以在一个页面里看到中文、英文、德文和韩文。
为了使不同国家地区的人使用到适应他们环境和语言的软件或网站,国际化成为了Java的必要因素之一。
国际化机制在软件开发过程中,使得软件与特定的语言或地区脱钩。当我们做的软件被移植到其他国家时,不必更改软件本身的代码就可以适应当地区的使用了,所以,国际化是必须的。
那么在Java中如何实现国际化呢?
通过学习后,我也了解了一些必要的入门知识。
首先,我们可以通过测试代码获得本机操作系统的默认语言和区域。
查看默认语言和区域:
测试代码:
1 /**
2 * @author 巨亚红
3 * @date 2014-3-18 下午2:47:06
4 * @版本 V1.0 作者: 时间: 修改:
5 * @param args
6 */
7 public static void main(String[] args) {
8 Locale defaultLocale=Locale.getDefault();
9 System.out.println("country="+defaultLocale.getCountry());
10 System.out.println("language="+defaultLocale.getLanguage());
11 }
测试结果:
如果将默认地区修改为美国的话,测试结果为:
我们需要将硬编码文本转移到外部的资源文件里,建立两个国际化资源文件:
名称=基本名称+Locale
1、设置资源文件。
MessgesBundle_en_US.properties:
k1=hello
k2=good bye
MessgesBundle_zh_CN.properties:
k1=\u4F60\u597D
k2=\u518D\u89C1
2、利用ResourceBundle.getBundle(baseName, locale)来找到MessgesBundle_en_US.properties文件。
ResourceBundle rb=ResourceBundle.getBundle("MessgesBundle", defaultLocale);
3、拿到k1和k2的value值。
System.out.println("k1="+rb.getString("k1"));
System.out.println("k2="+rb.getString("k2"));
结果是:
当前的地区设置成为英语(美国):
当前的地区设置成为中文(简体,中国):
4、占位符的使用。
设置当前Locale
//Locale currentLocale=new Locale("zh", "CN");
Locale currentLocale=new Locale("en", "US");
添加占位符:
利用类MessageFormat
1 Locale currentLocale=new Locale("en", "US");
2 ResourceBundle rb=ResourceBundle.getBundle("MessgesBundle", currentLocale);
3 MessageFormat mf=new MessageFormat(rb.getString("k1"));
4 System.out.println("k1="+mf.format(new Object[]{"Jessica"}));
结果为:
如果改成中文:
1 Locale currentLocale=new Locale("zh", "CN"); //中文
2 ResourceBundle rb=ResourceBundle.getBundle("MessgesBundle", currentLocale);
3 MessageFormat mf=new MessageFormat(rb.getString("k1"));
4 System.out.println("k1="+mf.format(new Object[]{"巨亚红"}));
结果为:
5、设置缺省国际化资源文件。
上面中我们设置的两个Locale已有资源文件,但是如果我们随便设置一个Locale,如果没有它对应的资源文件怎么办?那么就需要设置一个缺省的国际化资源文件了。只要添加一个名为MessgesBundle.properties的资源文件可以了,在里面进行相应的设置就OK了。
注意点:
缺省的Locale是由操作系统决定的。
Locale由语言和国家代码构成。
国际化资源文件是由baseName+Locale构成,如: MessgesBundle_en_US.properties
缺省的国际化资源文件是由baseName.properties命名的,如:MessgesBundle.properties
如果资源文件放在了包里,那么baseName就要加上包名了,否则找不到。
附上出处链接:
java 所说的国际化是什么意思?
国际化就是在你导入struts之后工程目录下的ApplicationResources.properties文件当你在struts-config.xml配置了:message-resources parameter="xxx.xx.xxx.ApplicationResources" /比如简体中文的是:ApplicationResources_zh_CN.properties会自动根据你操作系统的语言环境转换语言,但是在你JSP网页上,必须要用struts标签
java中如何对时间做国际化处理啊
import java.util.*;
import java.text.*;
public class timeText {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();//获取系统时间格式
String str2 = d2.format(now); //将时间格式转换成字符串
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用
System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);
System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);
}
}
运行结果:
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化时间后为:2008-6-16
用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化时间后为:20:54:53
用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为
:2008年6月16日 星期一 下午08时54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为
:2008年6月16日 下午08时54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后
为:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间
后为:2008-6-16 20:54:53
或者直接获取毫秒,但是感觉与你问题无关
java注释怎么做到国际化
使用Struts2的方式实现国际化
1、实现原理
Struts 2国际化是建立在Java国际化的基础之上,一样也是通过提供不同国家/语言环境的消息资源,然后通过ResourceBundle加载指定Locale对应的资源文件,再取得该资源文件中指定key对应的消息---整个过程与Java程序的国际化完全相同,只是Struts2框架对Java程序国际化进行了进一步封装,从而简化了应用程序的国际化。
关于国际化java和国际化标准化比值是什么的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。