3

How can I convert an IETF BCP 47 language code [e.g zh-cmn-Hant-HK] into a display string [e.g. "Mandarin Chinese, Traditional (Hong Kong SAR)"] in Android (Java)?

Some background. I am working with the speech to text APIs. I am calling sendOrderedBroadcast, passing a BroadcastReceiver that receives a list of supported languages thru RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES. The EXTRA_SUPPORTED_LANGUAGES are provided as IETF BCP 47 codes rather than Java locale codes: http://developer.android.com/reference/android/speech/RecognizerIntent.html

What support does Android provide for working with IETF BCP 47 codes? I am especially interested in getting display names as shown in the example above.

Theo
  • 5,963
  • 3
  • 38
  • 56

1 Answers1

3

No support in Android. And in general I don't think you will find such support outside ICU.

So you have two options: ICU4J or ICU4C.

For ICU4J, you should bring your own copy, because it is not there.

ICU4C is present on Android (all the internationalization functionality in Dalvik is implemented on top of it). You can use the NDK (the Native Development Kit) to call the ICU4C from a C/C++ module, and use JNI to access you native module from Java. But although ICU4C is there by default, Android does not exposes it, and it's use is strongly discouraged. Not to mention that the ICU4C data file might be stripped down big time, so the info needed to spell out the bcp 47 code might not be there.

So the safest thing is your own copy of ICU4J. You can either deploy it with your application, or you can expose that info as a web-service for your application to consume.

Mihai Nita
  • 5,547
  • 27
  • 27
  • 1
    Thanks for the tip on ICU4J and ICU4C -- I will use ICU4J. For others looking to convert IETF BCP 47 codes to display strings here are the steps: 1) Call ULocale.forLanguageTag with the IETF BCP47 language code to get a ULocale object 2) Call the getDisplayLanguage method on the ULocale object to get a display string. – Theo Dec 01 '11 at 17:53
  • @Theo `ULocale.forLanguage` asks for min api level 24. – viper Apr 18 '19 at 05:48
  • 1
    @viper: This answer was written before API 24 came out. You need to integrate ICU4J as a library into your project and then import `ULocale` from the ICU4J lib. There's now a version of `ULocale` included in Android since API24, but if you want to support older versions of Android you still need to integrate ICU4J as a lib. – Theo Apr 18 '19 at 14:46
  • Thank you @Theo. I was able to convert language code to language name with `Locale.forLanguageTag()` method. – viper Apr 19 '19 at 03:56