3

EDIT:

I have a language name in English, nothing else. How do I get the code for it. e.g.:

  • English -> en
  • German -> de
  • Spanish -> es
  • Chinese -> zh
paulgavrikov
  • 1,883
  • 3
  • 29
  • 51

2 Answers2

3

LanguageCode enum contained in com.neovisionaries:nv-i18n:1.11 (or later) has findByName methods to get language codes whose names match a given pattern.

List<LanguageCode> findByName(String regex);
List<LanguageCode> findByName(Pattern pattern);

To get ISO 639-1 codes (2-letter, lower-case alphabets) for English, German, Spanish and Chinese:

String[] names = { "English", "German", "Spanish", "Chinese" };

for (String name : names)
{
    String code = LanguageCode.findByName(name).get(0).name();
    System.out.format("%s -> %s\n", name, code);
}

The above code snippet will output:

English -> en
German -> de
Spanish -> es
Chinese -> zh


GitHub

https://github.com/TakahikoKawasaki/nv-i18n

JavaDoc

http://takahikokawasaki.github.io/nv-i18n/

Maven

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-i18n</artifactId>
    <version>1.11</version>
</dependency>
Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
  • 2
    I would love to give this answer an upvote, but looking at the source code I see that you don't use a well known library, like the Locale in java, as backend. You re-define all the things, which imho is bad: you should then run after all the updates in the ISO 639-1 (which isn't a stable standard) and so on. To confirm this, there aren't major updates since 5 years ago: it's a burden to mantain such library. Did I see it right? – reallynice May 30 '17 at 13:16
0

We have to set the Language name and country name for locale to get this

// Locale(String language, String country)

Locale locale = new Locale("German" , "DE");

// print ISO3 country name for corresponding locale

System.out.println("Name:" + locale.getISO3Country());

This returns ISO3Country name as DEU

Piyush
  • 18,895
  • 5
  • 32
  • 63
Nibin
  • 182
  • 5