Java Util Tool reads internationalized resource files

Source: Internet
Author: User
Tags java util


Locale resourcebundle Locale Read resource file


Package yycg.util;


Import java.io.Serializable;

Import Java.text.MessageFormat;

Import java.util.ArrayList;

Import Java.util.Iterator;

Import java.util.List;

Import Java.util.Locale;

Import Java.util.ResourceBundle;

Import Java.util.Set;


/**

* Resource file Reading tool class

*

*/

public class Resourcesutil implements Serializable {


Private static final long serialversionuid = -7657898714983901418l;


/**

* System language Environment, default to Chinese en

*/

public static final String LANGUAGE = "zh";


/**

* System national environment, default to China CN

*/

public static final String country = "CN";

private static Locale GetLocale () {

Locale locale = new locale (LANGUAGE, country);

return locale;

}


/**

* Get resource file values based on language, country, resource filename and key name

*

* @param language

* Language

*

* @param country

* Country

*

* @param baseName

* Resource File name

*

* @param section

* Key Name

*

* @return Value

*/

private static string GetProperties (string baseName, string section) {

String retvalue = "";

try {

Locale locale = GetLocale ();

ResourceBundle RB = Resourcebundle.getbundle (baseName, locale);

RetValue = (String) rb.getobject (section);

} catch (Exception e) {

E.printstacktrace ();

TODO Add Processing

}

return retvalue;

}


/**

* Read content from a resource file via key

*

* @param fileName

* Resource File name

*

* @param key

* Index

*

* @return index corresponding to the content

*/

public static string GetValue (string fileName, string key) {

String value = getProperties (Filename,key);

return value;

}


public static list<string> gekeylist (String baseName) {

Locale locale = GetLocale ();

ResourceBundle RB = Resourcebundle.getbundle (baseName, locale);


list<string> reslist = new arraylist<string> ();


Set<string> keyset = Rb.keyset ();

for (iterator<string> it = Keyset.iterator (); It.hasnext ();) {

String lkey = (string) it.next ();

Reslist.add (Lkey);

}


return reslist;


}


/**

* Read content from a resource file via key and format

*

* @param fileName

* Resource File name

*

* @param key

* Index

*

* @param OBJS

* Formatting parameters

*

* @return Formatted content

*/

public static string GetValue (string fileName, String key, object[] objs) {

String pattern = GetValue (fileName, key);

String value = Messageformat.format (pattern, OBJS);

return value;

}


public static void Main (string[] args) {

System.out.println (GetValue ("Resources.messages", "101", New object[]{100,200});

Get locales based on operating system environment

/*locale Locale = Locale.getdefault ();

System.out.println (Locale.getcountry ());//Output Country code

System.out.println (Locale.getlanguage ());//output language code s

Load internationalized Resources (classpath under Resource directory messages.properties, if the Chinese environment would prefer to find messages_zh_cn.properties)

ResourceBundle RB = Resourcebundle.getbundle ("resources.messages", locale);

String RetValue = rb.getstring ("101");//101 is a key in the Messages.properties file

System.out.println (RetValue);

The information is formatted, if the resource has {} parameters that need to be formatted with Messageformat, object[] is the passed parameter, the quantity is determined by the number of {} in the resource file

String value = Messageformat.format (RetValue, New object[]{100,200});

System.out.println (value);

*/


}

}


Java Util Tool reads internationalized resource files

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.