How to get Local country from the language? - java

How to find country from its Locale-language ?
e.g; if I set the local language to en then I expect to get US as country.
Locale locale = new Locale("en");
locale.getCountry() // returns null
The issue is that I only have the country code such as: fr,de,en and now I just want to find the right country locale from these codes.

en is English. en_US is US English. en_GB is, I think, UK English. Presumably using en_US would do what you want. There's a list of all the supported locales here: http://www.oracle.com/technetwork/java/javase/locales-137662.html
You can't get the country from "en" because as you can see, multiple countries may share a language. Which one would you pick?

One option would be to get all avaialble locales:
Locale[] locales = Locale.getAvailableLocales();
then insert them into a HashMap and define the language as key. Now its possible to search for the country via the language key.

Apache Commons Lang has a method for that:
LocaleUtils#countriesByLanguage(String)
You can call getCountry() on each of the returned Locales to get the country code. As Eric already pointed out, for many languages there are several countries.

Related

How to get a country's official name from a Java Locale?

In ISO-3166, when it comes to the country, there is a difference between "short name" and "full name".
For example, Denmark (link to iso.org reference : https://www.iso.org/obp/ui/#iso:code:3166:DK).
Its country code would be DK and DNK (alpha-2 and alpha-3), its numeric code is 208, its short name is 'Denmark', and its full name is 'the Kingdom of Denmark'.
For my current project, I have all the information I need except full name.
Does anyone know of a way to get that kind of data?
Java's in-built localization doesn't support official country names.
Here's the entry for Denmark in the localization data
DK=Denmark
You can use a REST API like https://restcountries.eu/, or I found this CSV file which seems to contain what you want. You will need to parse it.

How to get list of nationalities in Java

Does someone know a way to retrieve a list of nationalities in Java ? Few precisions : I do not need a list of countries but a list of nationalities. I do not need a list of languages, but a list of nationalities. I've tried to twist Locale API, without result.
And icing on the cake, I need to display nationalities in a specific languages.For example, with Brazil, I need to display a 'brazilian' in english, a 'brésilien' in french and a 'brasilero' in spanish.
Does someone have an idea ?
Java doesn't contain a list of nationalities as far as I'm aware. The Locale class just gives a list of regions as stated in the javadoc. It would be helpful in your situation, but not for a lot.
From what i gather, you're going to have to create your own list of nationalities, and the name of the nationality in each language - not too hard.
To do the language part, internationalization will be helpful. It allows you to get the users region and set a language depending on where the user is from. It also allows formatting of numbers, text and dates.
Look at this stack overflow answer specifically for how to get the names of the nationalities in different languages. Basically, you create a different file for each language and inside the file are key-value pares. So the English file will look a bit like this:
nationality.english = English
nationality.german = German
nationality.russian = Russian
and the German file will be similar to:
nationality.english = Englisch
nationality.german = Deutsche
nationality.russian = Russisch
then depending on what language you want the nationalities displayed in, you just get the text from the language file using the key (e.g. nationality.russian).
To do the nationality part, you can create an enum that contains all the nationalities. For example:
public enum Nationalities{
ENGLISH,
GERMAN;
//And so on
}
See here if you are new to java enums.
You will probably want to add a bit more information in the enum class, such as the county and what the key for the nationality name is.
To pull it all together, you get the region of the user and set the language file for the region. Then for each listed nationality, you get the name of it from the language file.

How to predict correct country name for user provided country name?

I am planning to do some data tuning on my data.
Situation-I have a data which has a field country. It contains user input country names( It might contain spelling mistakes or different country names for same country like US/U.S.A/United States for USA). I have a list of correct country names.
What I want- To predict which closest country it is referring to. For example- If U.S. is given then it will change to USA(correct country name in our list).
Is there any way I can do it using Java or opennlp or any other method?
You can use Getty API . It will give you abbreviations of country name. Just play on this API.
OR
You can also use Levenshtein Distance to get most closest country name.
Try this out. Will help you.
You can try Google's auto complete location api to your text box or select.
if you will use this api then you will get google like auto complete intellisence while typing.
visit link
If you have the city or state information that is sanitized then you could do a look up of the country.
You could also define aliases in your list of country names and point the aliases to the preferred notation. For example, US, United States, USA all are aliases of U.S.A. You could make the program to append to alias database so that it improves as it is being used. You might have do multiple passes over the data and also certain amount of manual work is involved.

Localization design pattern in android

In my database I have a table containing localized cities.
Cities
_id |name_en |name_de |name_it
0 |Rome |Rom |Roma
1 |Munich |München |Monaco
...
Now I want show a ListView where each line exists of all names started by the name in the users language. Also the whole list should be sorted by the city in the users language.
Which is the right design-pattern for this kind of problem?
This is quite a broad question, but here's one general approach:
Get the user's current language ( Get the current language in device )
Query your database with this language code
Bind the returned Cursor to your ListView
Please post your relevant code if you want specific help.
Obviously you need to decide what column to use for SQL request (for both stating which column to retrieve and by which column to sort). So your column names should be public constants. And you need to have a method to return a column name (one of the constants) depending on the current device locale. Use one of the constants as a fallback if the locale does not match any known for the application locales.

Java: Currency to Locale Mapping Possible?

I have a value stored in a DB correlating to a monetary amount, say 10.0. I also have access to the Currency/CurrencyCode. How can I use NumberFormat/DecimalFormat/(other?) to format when I don't know the Locale? According to the docs it will pick a default locale which won't work with a foreign Currency.
JasonTrue is correct, but you can override the currency of the NumberFormat's locale:
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
//the user may have selected a different currency than the default for their locale
Currency currency = Currency.getInstance("GBP");
numberFormat.setCurrency(currency);
numberFormat.format(amount);
The correct behavior, generally speaking, is to format the amount in the User's preferred locale, not the currency's typical locale. On the client side, you'll have the user's preference (Locale.getDefault()); if you are doing something on the web server side, use the Accept-Language or, preferably, the page content's locale to obtain the proper a locale.
The reasoning is this:
An English-US user will understand € 10,000,000.15 but not the suitable-for-Germany equivalent, € 10.000.000,15
The currency itself doesn't contain enough information to infer a suitable locale, anyway.
What if the currency code is EUR? And, while it has taken a beating, USD is still used throughout the world. Inferring the locale from the currency code seems unreliable. Can you introduce an explicit user preference for the locale instead?
The information you are looking for is not part of the built-in Java currency database, so there is not an API for it. You could create your own table for the many cases that are unambiguous.
I would say that if your database is storing a currency value it should be hanging onto the units at the same time. It sounds like you're doing that now. Can you add the Locale to the database at the same time? Could be a decent solution.

Categories