3

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 ?

Germs
  • 347
  • 6
  • 8

1 Answers1

0

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.

Community
  • 1
  • 1
Luke Melaia
  • 1,470
  • 14
  • 22
  • OK, thank you for your experienced feeback, A correct ResourceBundle should do the job. Tedious but feasible. – Germs Sep 23 '16 at 11:31
  • Don't use enums. They are pretty hard to use in this case. Since you have to translate those entries, it is probably best to use something like resource bundles. – Mihai Nita Oct 15 '16 at 04:44