Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an IETF BCP 47 language code into a display string?

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.

like image 622
Theo Avatar asked Oct 23 '25 01:10

Theo


1 Answers

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.

like image 154
Mihai Nita Avatar answered Oct 25 '25 16:10

Mihai Nita