Internationalization (i18n) in Kotlin: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Internationalization (i18n) in Kotlin: From Basics to Advanced Techniques



Internationalization (abbreviated as i18n — from the English word "internationalization", where 18 is the number of letters between the first "i" and the last "n") is the process of designing software so that it can be easily adapted to different languages and regional standards without making changes to the code. For Kotlin, as a modern multiplatform language, i18n support is especially important, because applications written in it run on Android, the backend (JVM), the web (Kotlin/JS), and native platforms (Kotlin/Native).



In this article, we will look at how to properly organize internationalization in Kotlin projects: from basic approaches using resource files to advanced solutions with helper libraries.



1. Basic Approach: Resource Files (ResourceBundle)

The simplest and most common way is to use the standard Java ResourceBundle mechanism and property files (.properties). This method works great for JVM projects (Android, Spring Boot, console applications).



Step 1. Create localization files

Create files in your project resources named messages.properties (default) and messages_ru.properties (for Russian):



# messages.properties (English, default language)greeting=Hello, {0}!farewell=Goodbye, {0}!app.title=My Application

# messages_ru.properties (Russian)greeting=Привет, {0}!farewell=До свидания, {0}!app.title=Моё приложение


Step 2. Load and use in Kotlin

Create a helper object for working with localization:



import java.text.MessageFormatimport java.util.*

object I18n { private var bundle: ResourceBundle = ResourceBundle.getBundle("messages", Locale.getDefault())

fun setLocale(locale: Locale) { bundle = ResourceBundle.getBundle("messages", locale) }

fun get(key: String, vararg args: Any?): String { val pattern = bundle.getString(key) return MessageFormat.format(pattern, *args) }}

fun main() { // Use English by default println(I18n.get("greeting", "Alice")) // Hello, Alice!

// Switch to Russian I18n.setLocale(Locale("ru")) println(I18n.get("greeting", "Алиса")) // Привет, Алиса!}


2. The kotlin-i18n Library (by JetBrains)

For Kotlin Multiplatform, there is an official library kotlin-i18n that allows you to work with translations uniformly across all platforms. It uses the Gettext PO (Portable Object) format, which is convenient for translators.



Installation (Gradle)

repositories {    mavenCentral()}dependencies {    implementation("org.jetbrains.kotlinx:kotlin-i18n:0.1.0")}


Usage Example

import org.jetbrains.kotlinx.i18n.*

fun main() { // Create a translator by loading from PO files val translator = Translator( bundleName = "messages", locale = Locale("ru") )

// Use with parameter substitution val message = translator.translate( "greeting", "Мир" ) println(message) // Привет, Мир!}


Advantages: support for plural forms, translation contexts, works on all Kotlin platforms.



3. Advanced Approach: The ok-i18n Library

For projects that require flexibility and integration with DI containers (e.g., Koin or Dagger), the ok-i18n library is an excellent choice. It supports JSON format for translations, which is convenient when working with REST APIs or configuration files.



Example JSON translation file (ru.json)

{  "app": {    "title": "Мо                    

Recommendations