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.
Related
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.
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.
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.
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.
I'm having some trouble with a search I'm trying to implement. I need for a user to be able to enter a search query into a web interface and for the back-end Java to search for the query in a number of fields. An example of this might be best:
Say I have a List containing "Person" objects. Say each object holds two String fields about the person:
FirstName: Jack
Surname: Smith
FirstName Mary
Surname: Jackson
If a user enters, "jack", I need the search to match both objects, the first on Surname, and the second on FirstName.
I've been looking at using a MultiFieldQueryParser but can't get the fields set up right. Any help on this or pointing to a good tutorial would be greatly appreciated.
MultiFieldQueryParser is what you want, as you say.
Make sure:
The field names are always used consistently
The same Analyzer is used on both fields, and also on the query parser
You won't find partial words by default, so if you search for jack you won't find jackson. (You can search for jack* in that case.)
Regarding field name, I always set up an enum for my field names, then use e.g. MyFieldEnum.firstname.name() when passing field names to Lucene, so that if I make a spelling mistake the compiler can catch it, and it's also a good place to put Javadoc so you can see what the fields are for, and also a place where you can see the complete list of fields you wish to support in your Lucene documents.