Warning
This library is currently in an experimental phase. The API is subject to change, and it is not recommended for production use at this time.
A simple, type-safe internationalization library for Kotlin Multiplatform projects. No XML files, no complex setup—just clean Kotlin code.
- Unique String Keys - No more collisions, just unique string identifiers for your resources.
- ISO 639-1 standard locale codes - With support for custom locales
- Automatic validation - Catches missing translations at initialization
- Clean API - Minimal boilerplate, maximum clarity
- KMP compatible - Works on JVM, iOS, Android, JS, Native
Add to your build.gradle.kts:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.lazarusmugo:glossarist:<latest-version>")
}
}
}1. Initialize the SDK - N.B THIS IS ONLY REQUIRED FOR ACCESSING STRINGS FROM NON COMPOSE CONTEXT (E.G VIEWMODELS)
// In your App.kt or main function
fun initializeApp() {
Strings.initialize(
I18nConfig(
localeProvider = PlatformLocaleProvider(),
defaultLocale = LocaleCode.Standard.EN,
supportedLocales = setOf(
LocaleCode.Standard.EN, // English
LocaleCode.Standard.SW, // Swahili
LocaleCode.Standard.FR // French
),
enforceTranslations = true // Fail fast if translations missing
)
)
}@TranslationRequired
data class AuthStrings(
val welcomeTitle: String,
val loginButton: String,
val signupButton: String
)val authEnglish = AuthStrings(
welcomeTitle = "Welcome Back",
loginButton = "Log In",
signupButton = "Create Account"
)
val authSwahili = AuthStrings(
welcomeTitle = "Karibu Tena",
loginButton = "Ingia",
signupButton = "Fungua Akaunti"
)
val authFrench = AuthStrings(
welcomeTitle = "Bon Retour",
loginButton = "Se Connecter",
signupButton = "Créer un Compte"
)val authStrings = stringResource(
key = "auth_strings", // Use a unique string key
translations = mapOf(
LocaleCode.Standard.EN to authEnglish,
LocaleCode.Standard.SW to authSwahili,
LocaleCode.Standard.FR to authFrench
)
)@Composable
fun LoginScreen() {
val strings by authStrings
Column {
Text(strings.welcomeTitle)
Button(onClick = {}) {
Text(strings.loginButton)
}
OutlinedButton(onClick = {}) {
Text(strings.signupButton)
}
}
}class LoginViewModel : ViewModel() {
val strings: AuthStrings = Strings.getNonCompose("auth_strings")
init {
println(strings.welcomeTitle)
}
}
The library supports pluralization through the PluralString typealias, which is a function that
takes a count and returns a string. This gives you full control over the pluralization logic.
First, define a PluralString in your data class:
data class NotificationStrings(
val unreadMessages: PluralString
)Then, provide the pluralization logic in your translations:
val notificationsEnglish = NotificationStrings(
unreadMessages = { count ->
when (count) {
0 -> "You have no new messages"
1 -> "You have 1 new message"
else -> "You have $count new messages"
}
}
)
val notificationsSwahili = NotificationStrings(
unreadMessages = { count ->
when (count) {
0 -> "Huna ujumbe mpya"
1 -> "Una ujumbe 1 mpya"
else -> "Una ujumbe $count mpya"
}
}
)Finally, use it in your composable:
@Composable
fun NotificationBadge(count: Int) {
val strings by notificationStrings
Text(strings.unreadMessages(count))
}// Switch to a specific language
Strings.setLocale(LocaleCode.Standard.SW)
// Or use string code
Strings.setLocale("sw")
// Reset to system default
Strings.resetToSystemLocale()
// Get current locale
val current = Strings.getCurrentLocale()@Composable
fun LanguageSelector() {
Row {
Button(onClick = { Strings.setLocale(LocaleCode.Standard.EN) }) {
Text("English")
}
Button(onClick = { Strings.setLocale(LocaleCode.Standard.SW) }) {
Text("Swahili")
}
Button(onClick = { Strings.setLocale(LocaleCode.Standard.FR) }) {
Text("Français")
}
}
}The library automatically validates:
All required locales have translations
// This FAILS - missing French translation
val strings = stringResource(
key = "auth_strings",
translations = mapOf(
LocaleCode.Standard.EN to authEnglish,
LocaleCode.Standard.SW to authSwahili
// Missing FR - error at registration!
)
)No null translations
// This FAILS - null value
val strings = stringResource(
key = "auth_strings",
translations = mapOf(
LocaleCode.Standard.EN to authEnglish,
LocaleCode.Standard.SW to null // Null!
)
)// Define custom locale for Kenyan English/Sheng
Strings.initialize(
I18nConfig(
supportedLocales = setOf(
LocaleCode.Standard.EN,
LocaleCode.Custom("en-ke") // Custom locale!
),
// ...
)
)
val authKenyan = AuthStrings(
welcomeTitle = "Karibu Nyumbani",
loginButton = "Ingia",
signupButton = "Jisajili"
)
val authStrings = stringResource(
key = "auth_strings_kenyan",
translations = mapOf(
LocaleCode.Standard.EN to authEnglish,
LocaleCode.Custom("en-ke") to authKenyan
)
)Common languages available in LocaleCode.Standard:
EN- EnglishSW- SwahiliFR- French
For languages not in the standard set, use custom locales:
LocaleCode.Custom("my-language-code")