zoukankan      html  css  js  c++  java
  • 使用eclipse开发国际化程序

    1.1 什么是i18n

    以下内容引自wikipedia Internationalization_and_localization

    The terms are frequently abbreviated to the numeronyms i18n (where 18 stands for the number of letters between the first i and the last n in the word internationalization, a usage coined at Digital Equipment Corporation in the 1970s or 1980s) and L10n for localization, due to the length of the words. Some writers have the latter acronym capitalized to help distinguish the two.
    简单来说,i18n是Internationalization的缩写,18代表单词中间的18个字母。

    1.2 什么是Locale

    一个Locale对象代表了一个特定的地域、政府、或文化地区,它们使用的文字/语言是有别于其他Locale的。
    Locale对象有三个主要组件:language、country、variant。
    variant代表特定制造商或特定浏览器代码,例如WIN代表Windows,MAC代表Macintosh,POSIX代表POSIX,可以用下划线将两个variant隔开,重要的放前面。
    完整的language、country、variant可以在language-subtag-registry中找到,
    找language: search for "Type: language"
    找country: search for "Type: region"
    找variant: search for "Type: variant"

    Locale有三个构造器:

    Locale(String language)
    // Construct a locale from a language code.
    Locale(String language, String country)
    // Construct a locale from language and country.
    Locale(String language, String country, String variant)
    // Construct a locale from language, country and variant.
    

    例如要构造一个加拿大使用的英语的Locale对象,可以这样写:
    Locale lcoale = new Locale("en", "CA");
    此外,Locale类提供了静态final域,他们返回特定国家或者语言的地区,因此可以通过调用它的静态方法来构造Local对象:
    Locale lcoale = Locale.CANADA_FRENCH;
    完整的静态final域可以在Java的API中找到。

    另外,静态getDefault方法返回用户计算机所在地区:
    Locale lcoale = Locale.getDefault();

    1.3 properties文件

    我们在开发程序时,一般都会将文本元素放在单独的文件中,
    不同语言不同文件,
    然后使用ResourceBundle来读取属性文件,
    文件的命名方式一般为:basename_languageCode_countryCode,
    basename可以自己写,后面的格式时固定的。
    比如basename为MyResource,则可以有以下文件:
    MyResource_zh_CN.properties
    MyResource_en_US.properties
    MyResource_de_DE.properties

    如果没有找到指定Locale文件,会读取默认的MyResource.properties,如果这个文件也没有找到,就会报错。
    然后我们用ResourceBundle读取属性文件:

    public static final ResourceBundle getBundle(String baseName)
    public static final ResourceBundle getBundle(String baseName, Locale locale)
    

    比如:
    ResourceBundle rb = ResourceBundle.getBundle("MyResource", Locale.US);

    1.4 程序实例

    新建一个class输入以下代码:

    package com.amnotgcs;
    
    import java.awt.GridLayout;
    import java.util.Locale;
    import java.util.ResourceBundle;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Main {
    	
    	public static void constructGUI() {
    //		Locale locale = new Locale("en", "US");
    		Locale locale = Locale.getDefault();
    		ResourceBundle rb = ResourceBundle.getBundle("MyResources", locale);
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		JFrame frame = new JFrame("I18N Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new GridLayout(3, 2));
    		frame.add(new JLabel(rb.getString("userName")));
    		frame.add(new JTextField());
    		frame.add(new JLabel(rb.getString("password")));
    		frame.add(new JPasswordField());
    		frame.add(new JButton(rb.getString("login")));
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				constructGUI();
    			}
    		});
    	}
    
    }
    

    配置classpath:

    运行程序:

    可以通过更改本地计算机语言或在程序中指定Locale来更改程序的语言显示:

    有了计划记得推动,不要原地踏步。
  • 相关阅读:
    05,WP8的文件和存储
    04,WP8的async和await
    01,Windows Phone 8 介绍
    开源工作流引擎
     RMS集成的时候会出这样那样的问题
    ASP.NET第一次访问慢的完美解决方案(MVC,Web Api)
    SharePoint2010关闭我的网站(个人网站)
    SMSServer的网关配置
    文件间调用变量(extern,include)[转]
    [转]安装SMSServer作为Windows系统服务
  • 原文地址:https://www.cnblogs.com/amnotgcs/p/15583730.html
Copyright © 2011-2022 走看看