Real-World Solutions for Remote Localization in Jetpack Compose Apps

Mert Toptas
4 min readJan 23, 2025

--

Introduction

In a mobile application, the strings visible to the user can be categorized into two main types. The first type includes dynamic strings fetched from a remote service, which can be updated or modified. For instance, product names, prices, and categories in an e-commerce app fall into this category. The second type consists of static strings that are predefined and remain constant, such as titles or description fields.

In Android applications, these static strings are typically defined in the strings.xml file and stored locally. When changes are required, they are updated in the code and released in the next app version.

However, managing all strings remotely introduces unique challenges, such as ensuring efficient updates, maintaining data consistency, and quickly displaying the updated text to users. This article will explore real-world solutions for handling remote localization in Jetpack Compose applications.

Table of Contents

  • Why Remote Localization?
  • Implementation a Dynamic String Management System
  • Setting Up Remote String Integration in Your Application
  • Conclusion

Section 1: Why Remote Localization?

In most applications, static strings are commonly used for managing text content. However, there are scenarios where either client requirements or project needs dictate that all strings should be managed remotely.

With a static approach, a new app release is required every time a string needs to be updated. This involves modifying the code, preparing a new version, and waiting for updates to be reviewed and published on the Play Store. This process can be time-consuming and impractical in many cases.

By managing strings remotely, changes can be made instantly without needing a new app version. This enables quick updates, ensures flexibility, and allows for seamless content management to meet evolving requirements.

Section 2: Implementing a Dynamic String Management System

To implement remote string management in an Android application, a utility like RemoteString can be highly effective. Below are the key steps and features derived from the provided code:

  1. Initialization:
    RemoteString must be initialized with the application’s R.string class. This allows the class to map resource IDs (resId) to their corresponding keys (resKey).
RemoteString.init(R.string::class.java)

2. Fetching Strings Dynamically:
Using the getString function, you can retrieve string values dynamically. If a remote value exists, it will be used; otherwise, the default value strings.xml is returned.

val dynamicString = RemoteString.getString(context, R.string.welcome_message)

3. Updating Strings from Remote Data:
The update method takes a JSON string JSONObject as input and updates the string values stored in SharedPreferences. This allows for seamless updates without requiring a new app release.

val json = """{"welcome_message": "Welcome to our app!"}"""
RemoteSting.update(context, json)

4. Fallback Support:
If a remote string is not available, the app gracefully falls back to the default value defined in the strings.xml file.

5. Clearing Data:
The utility includes methods to clear specific or all stored values, ensuring flexibility in managing string resources.

Full Code here;

Section 3: Setting Up Remote String Integration in Your Application

Step 1: Initialize RemoteString in the Application Class

@HiltAndroidApp
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
RemoteString.init(RString::class.java)
}
}

RemoteString.init(RString::class.java): Initializes RemoteString and references the strings.xml file used in the project (here RString). This is necessary to be able to dynamically manage string resources.

Step 2: Updating Local Strings from Remote Data

To demonstrate how to update strings from remote with RemoteString, the RemoteString.clearAll, and RemoteString.update functions are used. These operations are ideal for taking incoming JSON data and updating strings locally.

RemoteString.clearAll(context)
RemoteString.update(context, JSONObject(data))

Step 3: Create the common_string.xml (name optional)

<resources>
<!-- Example for a static button label -->
<string name="btn_submit">Submit</string>

<!-- Example for a dynamic welcome message -->
<string name="welcome_message">Welcome, %1$s!</string>

<!-- Example for an error message -->
<string name="error_connection">Connection error. Please try again.</string>

<!-- Example for a price label -->
<string name="price_label">Price: %1$.2f USD</string>
</resources>

This setup creates the basic infrastructure for dynamic string management and allows you to easily manage both static and remotely updatable strings.

Use of bonus context extensions;

@Composable
fun getRemoteString(@StringRes resId: Int, args: ImmutableList<Any?>? = null): String {
val context = LocalContext.current
return args?.let { context.getRemoteString(resId, it) } ?: context.getRemoteString(resId)
}

Step 4: Using RemoteString in a Compose Screen

@Composable
fun WelcomeScreen(context: Context, userName: String) {
// Screen Layout
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
// Dynamic Welcome Message
Text(
text = getRemoteString(R.string.welcome_message, listOf(userName)),
style = MaterialTheme.typography.h5,
modifier = Modifier.padding(bottom = 16.dp)
)

// Submit Button
Button(onClick = { /* Handle click */ }) {
Text(text = getRemoteString(R.string.btn_submit)
}
}
}

Conclusion

In this article, we explored how to manage dynamic strings in Android using Jetpack Compose and remote data sources. By leveraging the RemoteString utility, we can efficiently update and manage strings without requiring a new app release. This approach offers greater flexibility, especially in scenarios where real-time string updates are needed, such as A/B testing or dynamic content changes. By combining static resources with remote configuration, we can provide a seamless and scalable solution for internationalization and localization in modern Android applications.

--

--

Mert Toptas
Mert Toptas

Written by Mert Toptas

ex part-time law full-time mobile application#Flutter #Kotlin Android Developer at @Loodos linkedin.com/in/mertcantoptas/

No responses yet