How to create and use a Language Coordinator in Kotlin

Oscar de la Hera Gomez
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “Language Coordinator.”

A step by step guide on creating and using a localization manager that gathers the relevant language content and makes it available to an Android App.

Please note that the steps carried out in this tutorial can be found in our Open Source Kotlin Android Project's main branch, which can be cloned using the following command:

git clone git@github.com:delasign/kotlin-android-starter-project.git

Step One: Add the Gson dependency

A screenshot of Android Studio showing the build.gradle.kts (Module :app) open. Highlighted is the Gson dependency that we have added to the app. Code available below.

In order to convert the language JSON strings into usable data, we need to use the Gson library.

Navigate to your app level build gradle (i.e. build.gradle.kt (Module :app)) and add the following line to the dependencies:

implementation(com.google.code.gson:gson:2.8.9)

Please note that version 2.8.9 was the latest version available when we wrote this tutorial but may not be the latest version available when you implement this. Please update this string to use the latest version available.

Step Two: Create the Language JSONs

A screenshot of Android Studio showing the language JSONS under a Strings folder within the Assets folder.

Follow the guide below to learn how to create JSON files that are readable by the app.

Add your language strings to the files in a format similar to that found below.

Step Three: Create the UIContent

A screenshot of Android Studio showing the populated UIContent File (Code available Below) side by side with the JSON it mirrors.

Create a new file called UIContent.kt and add a data class that matches your JSON.

We recommend that you place it under models > languageContent.

Step Four: Create the Language Enums

A screenshot of Android Studio showing the Languages.kt file, displaying the languages as Enums.

Create a file called Languages.kt and add the languages you wish to implement as enums.

We recommend that you place it under models > languageContent.

Step Five: Create the Language Coordinator Folder

A screenshot of Android Studio with the coordinator package selected, displaying how if you right click you can create a package via New > Package.

Create a package folder called for the LanguageCoordinator.

We suggest you call it languageCoordinator and place it under coordinators.

Step Six: Create the Language Coordinator Declaration File

A screenshot of Android Studio showing the Language Coordinator declaration file.

In the languageCoordinator folder, create a file called LanguageCoordinator.kt and paste in the code below.

Please note that the updateCurrentContent is created in the next steps.

Step Seven: Create the Language Coordinator Get Extension

A screenshot of Android Studio showing the Language Coordinator+Get declaration file.

In the languageCoordinator folder, create a file called LanguageCoordinator+Get.kt and paste in the code below.

Please follow the tutorial below to learn how to implement the ReadJSONFromAssets functionality.

Step Eight: Create the Language Coordinator Update Extension

A screenshot of Android Studio showing the Language Coordinator+Update file.

In the languageCoordinator folder, create a file called LanguageCoordinator+Update.kt and paste in the code below.

Step Nine: Initialize the LanguageCoordinator

A screenshot of Android Studio showing how to initialize the Language Coordinator in the MainActivity.kt file.

Initialize the LanguageCoordinator by running the following line in the MainActivity.

LanguageCoordinator.shared.initialize()

We recommend that you do this in a function called setupCoordinators in your MainActivity.kt.

Step Ten: Update Content on Resume

A screenshot of Android Studio showing how to update the content of the Language Coordinator when the app comes in back to the foreground in the MainActivity.kt file.

Call the Language Coordinators updateCurrentContent function in the onResume function of the MainActivity.kt's lifecycle to make sure the content updates when the app returns from the background.

Please note that you should send a notification to update the content after coming from the background, to make sure that it updates in the event that the language changes. To learn how to do this follow the tutorial below.

Step Eleven: Implement Content

A screenshot of Android Studio showing how to implement the content of the Language Coordinator in the MainActivity.kt file.

To implement content, first make sure the content exists by using the following line:

val currentContent: UIContent = LanguageCoordinator.shared.currentContent ?: return

Then implement it using dot notation that matches the data class that you created (i.e. data.sample.sampleString or data.sampleTwo.sampleB.aSampleString).

Step Twelve: Test

A screenshot of an Android Device showing the data printed on the screen.

Run the app on a device and confirm that the JSON is read and the data is processed.

Warning: Data classes cannot be obfuscated

A screenshot of Android Studio with the proguard-rule.pro file open. Highlighted is a demonstration of how we stop the obfuscation of our language directory to ensure that the JSON data class works after being released.

To guarantee that the app functions as expected on release, you should add ANY JSON data classes to the projects proguard-rules.pro file.

If you do not do this and you release the app with obfuscated code using the algorithm demonstrated above, your users will encounter a blank screen.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details