DEV Community

Dinoy Raj
Dinoy Raj

Posted on

List Formatter in Android : Android Internationalization part 1

List formattter

When you are addressing a larger audience of distinct linguistic, and cultural behaviours it is essential that your application content evolve according to the preferences that the user locale demands. especially dealing with strings or human-readable text that is displayed in the user interface, the aspect of translating and formatting it to adapt the usage corresponding to the user default locale turns out to be vital as it ensures a smooth user experience.

Need for list formatter

While generating a message from an arbitrarily sized list of terms/strings/units we might have to reformat them as conjunctions or disjunctions. The choice and positioning of separators and connectors ( conjunction/ disjunction ) can differ depending on the locale

for example, consider a list of string

val language = ["Kotlin", "Java", "XML"]

where you want to generate a message by the conjunction of all items in language list, like “Kotlin, Java, and XML used in Android development ”

Looks pretty straightforward to join string using a separator (, ) and connect the penultimate one with a connector but actual structure and usage varies according to locale (refer table below)

Locale (region) Result of formatting *
ja_JP ( japan ) Kotlin、Java、Xml separator itself changed
en_US ( US ) Kotlin, Java, and Xml separator before conjunction
en_GB ( UK ) Kotlin, Java and Xml separator not present before conjunction
de_DE ( Germany ) Kotlin, Java und Xml conjunction word
zh_TW ( Taiwan ) Kotlin、Java和Xml conjunction word and spacing in between
ko_KR ( Korea ) Kotlin, Java 및 Xml conjunction word and spacing in between

ListFormatter Class

ListFormatter is an immutable java class inside package android.icu.text used to format list of items with a separator and connectors appropriate for a given locale

create an instance of list formatter using getInstance() method either by passing the required locale along with width and type for formatting. By default it format accoording to default locale along with conjunction as type and wide as width

getInstance(Localelocale,ListFormatter.Typetype,ListFormatter.Widthwidth)

val formatter = ListFormatter.getInstance(Locale.getDefault())
Enter fullscreen mode Exit fullscreen mode

Overview about parameters

Locale

use Locale.getDefault() to get the default locale based on user device or custom locale depends upon your use case

Type

specified according to the type of meaning expressed by the list, Type Enum inside ListFormatter class ( ListFormatter.Type )

Value example *
AND Kotlin, Java, and Xml Conjunction formatting
OR Kotlin, Java, or Xml disjunction formatting
UNITS 1 cm, 2kg, 3km used for formatting list of values with units

Width

controls the verbosity level of connectors, determines the size (width) of the string returned

Value * Example
WIDE Kotlin, Java, and Xml Use list formatting with full word (no abbrevations)
SHORT Kotlin, Java, & Xml Use list formatting of typical length
NARROW Kotlin, Java, Xml Use list formatting of shortest possible length

To sum up

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun List<String?>?.formatter(type: ListFormatter.Type, width: Width): String {
var formattedValue = ""
try {
ListFormatter.getInstance()
val formatter = ListFormatter.getInstance(Locale.getDefault(), type, width)
formattedValue = formatter.format(this)
} catch (e: Exception) {
Log.d("ListFormatter", "error in formatting list")
}
return formattedValue
}
view raw formatter.kt hosted with ❤ by GitHub

Got any questions? Leave a comment below or connect me through Twitter.. :)

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay