Java software development learning notes (2)

xiaoxiao2021-03-06  41

1. Related knowledge 1. The Java programming language is the first programming language that supports software localization from the beginning. All strings use unicode

1.2 To localize: Number 123,456.78 UK; 123.456, 78 German currency date 3/22/61 United States; 22.03.1961 Germany March 22,1961; English 22. Marz 1961 German; March 22, 1961 Chinese Time Text - > Graphical user interface (centrally above)

1.3 Locale class Locale, simply refers to a sign language of language and region special combination, national and variants (language_country_variant) predefined locale object getDefault, getavailableloacles 1.4 information format Java.Text.MessageFormat 2. Resource package 2.1 resources loading ResourceBundle currentResources packet = ResourceBundle.getBundle ( "ProgramResources", currentLocal); getBundle will try to load one of the following classes, until a successful loading date: ProgramResources_language_country_variant ProgramResources_language_country ProgramResources_language ProgramResources unsuccessful, instead of using default loacal currentLocal reload If it is not successful, you will throw MissingResourceException exception GetBundle to find a class, will continue to find the next class to establish a resource hierarchy. Each level in the resource hierarchy does not necessarily exist 2.2 Resources Search String ButtonLabel = CURRENTRESOURES.GETSTING ("AddButton"); a resource has not been retrieved in subclats, which will be from the established resource hierarchical structure Retrieval in the parent class

2.3 The classification of resources can be classified according to the parameters of the resource search, and different resources can be placed in different resource packages. At the same time, the resource object can be stored any type of object, not only the string color backcolor = (color) CurrentResources. GetObject ("Backcolor"); Processing for this 2.4.2

2.4 Resource Package 2.4.1 Establishing your own resource packages must be inherited in ResourceBundle, and implement the following 2 methods: enumection getKeys (); object handlegetObject (stirng key); example: public class programsources 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 objects {public (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};} is quite invisible for each resource package. The following methods can be taken.

ListResourceBundle class and PropertityResourceBundle use ListResourceBundle 2.4.2 JDK classes offered, you can put all their resources into an array of objects, then it can be to find 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 .bouE}, {"defaultsie", double [] {216, 279}}}} If your settings are string type, then you can use the more published PropertyResourceBundle mechanism: put all strings into one properties Document, each line is a pair of keywords / values ​​to Butt on = Rechnen backColor = black defaultSie = 210X297 button = Compute backColor = blue defaultSie = 216X279 following format file named properties like this ProgramStrings.properties ProgramStrings_de.properties ProgramStrings_en_US.properties Resource loading ResourceBundle bundle = ResourceBundle.getBundle ( "ProgramStrings", Local); getBundle identifies a similar property file and convert it into PropertyResourceBundle.

Don't use PropertyResourceBundle insufficient: You need to analyze the best solutions to individual strings in the program: Place string resources into the properties file and use ListResourceBundle to use resource objects that are not strings. Note: Conversion: The file used to store properties is usually 7-bit ASCII files, you need to use Native2ASCII according to the link bit Unicode character default: programstrings.properties placed American English string and information to load local resource files failure Loading the default resources can be seen 3. Localization of graphical user interfaces requires noticeable things do not give the tag Label to determine the length of the string of different languages ​​in the resource package.

4. Our packaging basic class: XStringManager String Manager class, get string extensions from the resource file: Other places for strings XResourceManager: URL and objects, including: URL and objects, etc. XComponentBuilder: Unified creation View component object XerrorManager: get an error message according to the error code

转载请注明原文地址:https://www.9cbs.com/read-69083.html

New Post(0)