Android’s LocaleData class

Accessing miscellaneous data in the locale bundles

Thomas Künneth
Tommis Programming Blog
2 min readMar 7, 2020

--

A bookend featuring a globe
A bookend featuring a globe

In this post I continue my exploration of lesser known Android features. This time we take a look at package android.icu.util, which got a few new classes in Android Pie. android.icu.util.LocaleData is, as the documentation currently puts it, a class for accessing miscellaneous data in the locale bundles.

Well.

Let’s take a look.

ULocale locale = ULocale.forLocale(Locale.GERMANY);
LocaleData ld = LocaleData.getInstance(locale);
Log.i(TAG, ld.getDelimiter(LocaleData.QUOTATION_START));
Log.i(TAG, ld.getDelimiter(LocaleData.QUOTATION_END));
Log.i(TAG, ld.getDelimiter(LocaleData.ALT_QUOTATION_START));
Log.i(TAG, ld.getDelimiter(LocaleData.ALT_QUOTATION_END));

So, we can now get delimiter strings that tell us how to start and stop quotations. Also, we can query paper sizes like this:

LocaleData.PaperSize ps = LocaleData.getPaperSize(locale);
Log.i(TAG, String.format(“width: %d, height: %d”,
ps.getWidth(),
ps.getHeight()));

Currently the documentation does not specify the unit of the result. However, as for Germany the returned values are 210 and 297, we can assume millimeters in the metric system. We can obtain the measurement system as follows:

LocaleData.MeasurementSystem ms 
= LocaleData.getMeasurementSystem(locale);

Currently the documentation says Enumeration for representing the measurement systems, which is funny as android.icu.util.LocaleData.MeasurementSystem extends java.lang.Object. This is a pity as currently toString() provides nothing useful. You end up doing something like this:

if (LocaleData.MeasurementSystem.SI == ms) {
Log.i(TAG, “SI”);
} else if (LocaleData.MeasurementSystem.UK == ms) {
Log.i(TAG, “UK”);
} else if (LocaleData.MeasurementSystem.US == ms) {
Log.i(TAG, “US”);
} else {
Log.i(TAG, “unknown”);
}

SI represents the metric system. UK is a mix of metric and imperial units used in Great Britain. And US is the measurement system followed in the United States of America. You can get the source of a small demo in this Github repo.

A previous (less complete) version of this article was published on my Blogger.com blog Tommis Blog. I deleted the weblog in the wake of the GDPR (General Data Protection Regulation), so the original post is no longer available (or only through the Wayback machine of the Internet Archive).

If not stated otherwise images are © Thomas Künneth

--

--

Thomas Künneth
Tommis Programming Blog

Google Developer Expert for Android. Author. Speaker. Listener. Loves writing.