Styling Internationalized Text in Jetpack Compose
In today’s world, many applications require users to accept their terms, conditions, privacy policy, distance sales contract and more. These documents often contain a clickable text in a sentence and it plays a role in navigates to agreement screen, display a popup or bottom sheet when you click it and generally have different color and font weight to represent clickable texts. In this article I’ll try to focus on managing clicks on localized text in your Jetpack Compose applications.

What Jetpack Compose Offers
Jetpack Compose provides a builder class for annotating text, which makes easier the process of styling and appending text. If your app supports only one language, you can easily make clickable texts directly within the annotated strings.
Let’s say we have the string of “By clicking the continue, you agree to our Terms and Privacy Policy.” You could simply annotate “Terms” and “Privacy Policy” together for clickability if you provide just one language. But things get a bit tricky when dealing with other languages. The word order changes, making it hard to use the same structure. You can start with “prefix_terms,” but it’s not always that straightforward.
Like I said above we can use them directly if an an application has one language. Let say default language is en-US.

The Problem About Internationalized Texts
Directly embedding text can cause to incomplete or incorrect sentences due to differences in word order and sentence structure across languages. This causes a problem below, resulting in an incomplete Turkish sentence. Therefore, we need a generic way to handle it.

Firstly, we need tag, annotation and style to use buildAnnotatedString in our case. So, a simple data class to hold them is enough to use in ClickableLocalizedText function.
Secondly we need a function to proceed each item inside LinkAnnotation. We can implement this in 3 steps:
- Build an annotated string by given text, annotation and style.
- Create a ClickableText composable using the annotated string from step 1, and apply the annotation and style.
- When a section of the text is clicked, the function finds the annotation that corresponds to that section and calls the onClick callback using the item of the clicked annotation.
How Can We Use It?
Now, let’s see how to apply this article in practice. Below is the code showing the implementation of the ClickableLocalizedText
function. It allows us to create clickable text elements with given localization and styling in Jetpack Compose applications.
Now we are able to use clickable text for localized strings and get the annotation.

Conclusion
Handling clickable localized text in Jetpack Compose give out some challenges, but by applying dynamic text construction and styling strategies, developers can offer smooth user experience across languages. Using these methods shows that you care about making your apps accessible to everyone, which helps them reach more people around the world.