简体   繁体   中英

how to get a locale object in java?

I am using Java with framework Spring and Liferay. With liferay I know how to get a locale (object where there are some information: language, country...) but now I am in a java class without conection with liferay and I don't know how to get a locale object to get the language.

For example, I have the next method in a test class of my web page:

private void checkEnglishText(String contentXml) {
    CarContentGenerator esCarGeneratorObject = new      CarContentGenerator();
    HashMap<String, CarContentVo> hm = esCarGeneratorObject.getCar(contentXml);
    CarContentVo ccvo = hm.get("EPPA0418");
    Assert.assertEquals(ccvo.getCarCodes(), "empress");
}

here I am calling to CarContentGenerator constructor, this constructor is it:

public CarContentGenerator() {
    link = new LinkVo();
    links = new ArrayList<LinkVo>();
    itinerary = new ArrayList<LinkVo>();
    lpackageId = new ArrayList<String>();
    contentVo = new ContentVo();
    mapCar = new HashMap<String, CarContentVo>();
    this.locale = "en_US";
}

as you can see, I am giving the value "en_US" to the variable locale. "en_US" means that my page

will be showed in English and if I write "es_ES" it will be showed in Spanish, so.. my dought

is.. Do anyone know how to find out the language (in my case "en_US" or "es_EN") in which is my

web page? (get it from request or something)

There is a java class java.lang.Locale , which you could obtain an instance of via code like the following:

final Locale american = new Locale("en", "US");
final Locale spanish = new Locale("es", "ES");

So you should be able to use this as your locale object when you don't have a connection to Liferay. These Locales can be passed into various JDK classes that support locale-sensitive operations (such as NumberFormat and Currency for example), as well as being directly queryable for basic information.

As for your last question - I believe that's too broad to answer currently, without any more information. At the moment this question is simply "how do I communicate a few bytes' worth of static information between 'my web page' and 'my code'?" There are literally hundreds of possible ways to do this, with the best/easiest one depending on lots of boring details like your startup scripts, which technologies are used in various places, etc. If you need more help than that I believe you've have to post a very concrete question filling in all those gaps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM