Java Software Development Learning notes (II)

Source: Internet
Author: User
Tags format error code final interface locale resource
Note 1. Related knowledge
The 1.1 Java programming language is the first programming language to support software localization from the outset.
All strings use Unicode

1.2 Content to localize:
Number 123,456.78 England; 123.456,78 Germany
Currency
Date 3/22/61 the United States; 22.03.1961 Germany
March 22,1961; English 22. Marz 1961 German; March 22, 1961 Chinese
Time
Text
-> Graphical user interface (focus above)

1.3 Locale Class
Locale, in short, is a sign of a particular combination of language and region.
Languages, countries and variants (Language_country_variant)
Predefined locale objects
Getdefault, Getavailableloacles

1.4 Format of information
Java.text.MessageFormat

2. Resource Bundle
2.1 Load of resource bundle
ResourceBundle currentresources = Resourcebundle.getbundle ("programresources", currentlocal);

Getbundle will try to load one of the following classes until the load succeeds:
Programresources_language_country_variant
Programresources_language_country
Programresources_language
Programresources

unsuccessful, the default loacal will be used instead of currentlocal for reloading
If it is not successful, the MissingResourceException exception is thrown

Getbundle finds a class, continues to look for the next class, and establishes the resource hierarchy.
Each level in the resource hierarchy does not necessarily exist

2.2 Retrieval of resources
String buttonlabel = currentresources.getstring ("AddButton");
A resource is not retrieved in a subclass and will be retrieved from the parent class in the established resource hierarchy

2.3 Classification of resources
Can be categorized according to the parameters of resource retrieval, or you can put different resources into different resource bundles

Also, a resource object can hold objects of any type, not just strings
Color BackColor = (color) currentresources.getobject ("BackColor");
Processing reference to this 2.4.2


2.4 The establishment of the resource bundle
2.4.1 set up your own resource bundle class
Must inherit from ResourceBundle and implement the following 2 methods:
Enumeration Getkeys ();
Object Handlegetobject (stirng key);
Example:
public class Programresources extends ResourceBundle
Place Getkeys method in common superclass
{
Public enumeration Getkeys ()
{
Return Collections.enumeration (Arrays.aslist (keys));
}

Private string[] keys = {"Button", "BackColor", "Defaultsie"};
}

public class Programresources_de extends Programresources
{
Public Object Handlegetobject (stirng key)
{
if (Key.equals ("button"))
return "Rechnen";
else if (key.equals ("BackColor"))
return color.black;
else if (key.equals ("Defaultsie"))
return new double[] {210, 297};
}
}

public class Programresources_en_us extends Programresources
{
Public Object Handlegetobject (stirng key)
{
if (Key.equals ("button"))
return "Compute";
else if (key.equals ("BackColor"))
return color.blue;
else if (key.equals ("Defaultsie"))
return new double[] {216, 279};
}
}

Writing this code for each resource bundle is quite cumbersome. You can take the following methods.

2.4.2 JDK-supplied classes
ListResourceBundle and Propertityresourcebundle

With the ListResourceBundle class, you can put all of your resources into an array of objects, and then it will be able to find the resources for you:
public class Programresources_de extends ListResourceBundle
{
Public Object [[] getcontents () {return contents;}

private static final object[][] Contents
{
{"button", "Rechnen"},
{"BackColor", Color.Black},
{"Defaultsie", double[] {210, 297}}}
}
}

public class Programresources_en_us extends ListResourceBundle
{
Public Object [[] getcontents () {return contents;}

private static final object[][] Contents
{
{"button", "Compute"},
{"BackColor", Color.Blue},
{"Defaultsie", double[] {216, 279}}}
}
}

If all of your settings are string, then you can use the more published Propertityresourcebundle mechanism:
Put all strings in a property file with a pair of keyword/value pairs per line
Button=rechnen
Backcolor=black
defaultsie=210x297

Button=compute
Backcolor=blue
defaultsie=216x279
and name the property file in the following format
Programstrings.properties
Programstrings_de.properties
Programstrings_en_us.properties
Load resources like the following
ResourceBundle bundle = Resourcebundle.getbundle ("programstrings", local);
Getbundle find a similar property file and convert it to propertityresourcebundle. No direct use of propertityresourcebundle
Deficiencies:
You need to parse individual strings in your program

The best solution:
Put string resources into the property file and use ListResourceBundle for resource objects that are not strings.

Precautions:
Transformation:
The file used to store the property is usually a 7-bit ASCII file that requires Native2ascii to convert bit Unicode characters
Default
Put American English strings and information in Programstrings.properties
To load the default resource when the local resource file fails to read

3. The localization of graphical user interface need to pay attention to things
Do not judge label labels
The length of strings in different languages in the resource bundle should be considered

4. Our Package
Base class: Xstringmanager string manager class, get string from resource file
Extensions: Other places to use strings
Xresourcemanager: For obtaining specified resources, including: URLs and objects, etc.
Xcomponentbuilder: Unified creation of visual component objects
Xerrormanager: Get error message based on error code




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.