java本地化

阅读: 评论:0

java本地化

java本地化

一、国际化:缩写i18N(来源于国际化的单词缩写以i开始N结束的18位英文),让开发的程序适应世界上不同国家或地区的过程。

二、本地化:使程序能适应某个地区或语言的使用习惯的过程

1、本地化的目标: 数字,货币,日期,事件,文本

2、本地化的实现:

1)实现本地化数字和货币的步骤:

a.使用Locale类定义需要本地化的语言:

Locale aLocale=new Locale("zh","CN");

b.格式化数字:

NumberFormat numberFormat&#NumberInstance(aLocale);//参数表示确定的国家

c.使用本地的数字的格式,格式化目标数字:

String result=numberFormat.format(number);

用法参考代码:

public class NumberAnCurrentcy {public static void main(String[] args) {//第一个参数语言编码,第二个参数国家编码,都是国际标准Locale aLocale=new Locale("zh","CN");Locale bLocale=new Locale("ru","RU");//俄语Locale cLocale=Locale.FRANCE;//欧洲//整型 int的包装类Integer number=new Integer(123456);//相当于int i=123456NumberAnCurrentcy n=new NumberAnCurrentcy();n.formatNumber(aLocale, number);n.formatNumber(bLocale, number);n.formatNumber(cLocale, number);n.formatCurrentcy(aLocale, number);n.formatCurrentcy(bLocale, number);n.formatCurrentcy(cLocale, number);}//本地化数字private  void formatNumber(Locale aLocale, Integer number) {//NumberFormat用来格式化数字,创建的时候使用静态方法,传的参数是国家和地区或者语言NumberFormat numberFormat&#NumberInstance(aLocale);//参数表示确定的国家//使用本地的数字的格式 格式化目标数字String result=numberFormat.format(number);System.out.println(result);}//本地化货币private  void formatCurrentcy(Locale aLocale, Integer number) {NumberFormat numberFormat&#CurrencyInstance(aLocale);//参数表示确定的国家String result=numberFormat.format(number);Currency c&#Instance(aLocale);//返回本地货币的对象System.out.DisplayName()+":"+result);//getDisplayName方法显示货币的名字}
}

2)实现本地化日期和时间,步骤与实现本地化数字和货币一样,用法参考代码:

public class DataAndTime {public static void main(String[] args) {// 第一个参数语言代码,第二个参数国家代码,国际标准,Locale aLocale = new Locale("zh", "CN");Locale bLocale = new Locale("ru", "RU");// 俄语Locale cLocale = Locale.FRANCE;// 欧洲DataAndTime d=new DataAndTime();d.formatData(aLocale);d.formatData(bLocale);d.formatData(cLocale);d.formatTime(aLocale);d.formatTime(bLocale);d.formatTime(cLocale);}public void formatData(Locale aLocale) {//获取某个国家或地区或者语言的日期格式//第一个参数:日期的显示方式 DEFAULT LONG SHORT MEDIUM//第二个参数是国家或者语言的代码DateFormat dateFormat&#DateInstance(DateFormat.LONG,aLocale);//使用已经确定的日期格式去格式化目标日期System.out.println(dateFormat.format(new Date()));//new Date()当前系统日期}//格式化时间:时分秒public void formatTime(Locale aLocale) {//获取某个国家或地区或者语言的时间格式//第一个参数:时间的显示方式 DEFAULT LONG SHORT MEDIUM//第二个参数是国家或者语言的代码DateFormat Date&#TimeInstance(DateFormat.LONG,aLocale);//使用已经确定的时间格式去格式化目标时间System.out.println(Date.format(new Date()));}
}

3)实现本地化文本步骤:

a.将不同国家和地区的文字保存资源文件里面(**.properties),方法:在对应项目的src上new--file--写文件名(文件名规则:MessageBundle_语言代码_国家代码.properties,如中文:MessageBundle_zh_CN.properties)

b.根据Locale读取对应的文件,显示在界面上

用法参考代码:

public class FormatTextDemo extends JFrame implements ActionListener{JLabel name,password,language;JComboBox selectLanguage;String strName,strPassWord,strLanguage;public FormatTextDemo(){setVisible(true);setSize(300,300);setDefaultCloseOperation(EXIT_ON_CLOSE);getContentPane().setLayout(null);String[] languages={"中文","English"};selectLanguage =new JComboBox(languages);selectLanguage.setBounds(230, 50, 100, 40);getContentPane().add(selectLanguage);selectLanguage.addActionListener(this);language=new JLabel("请选择语言:");language.setBounds(100, 50, 100, 40);name=new JLabel("用户名");name.setBounds(100, 100, 100, 40);password=new JLabel("密码");password.setBounds(100, 150, 100, 40);getContentPane().add(language);getContentPane().add(name);getContentPane().add(password);		}public static void main(String[] args) {new FormatTextDemo();}@Overridepublic void actionPerformed(ActionEvent e) {//判断是否操作下拉列表Source().equals(selectLanguage)){//判断选中哪一个JComboBox jcb= (Source();int index&#SelectedIndex();//获取选择选项的索引switch(index){case 0://英文setText(Locale.US);break;case 1://中文setText(Locale.CHINA);break;default://中文setText(Locale.CHINA);break;}			}		}//根据Locale读取对应的文件public void setText(Locale loacle) {//根据locale读取资源文件//1资源束//第一个参数里面写文件名下划线前面的字符//第二个字符国家或地区<span style="color:#FF0000;">ResourceBundle message&#Bundle("MessageBundle",loacle);</span>//2读取资源文件strName = String("name");strPassWord = String("password");strLanguage = String("language");// 设置界面的文本name.setText(strName);password.setText(strPassWord);language.setText(strLanguage);        }
}
其中:MessageBundle_zh_CN.properties文件内容

#key=value
name=user name:
password=password
language=please select language

MessageBundle_en_US.properties文件内容

#key=value
name=u7528u6237u540D
password=u5BC6u7801
language=u8BF7u9009u62E9u8BEDu8A00



本文发布于:2024-02-01 01:03:34,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170672061432702.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:GLM
标签:java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23