Skip to content

Instantly share code, notes, and snippets.

@stevdza-san
Last active November 15, 2025 13:56
Show Gist options
  • Select an option

  • Save stevdza-san/40539b177a052074691f9de65314ff27 to your computer and use it in GitHub Desktop.

Select an option

Save stevdza-san/40539b177a052074691f9de65314ff27 to your computer and use it in GitHub Desktop.
Kobweb Localization
object Localization {
private const val LANGUAGE_KEY = "language"
var currentLanguage by mutableStateOf(
value = Language.valueOf(
value = window.localStorage[LANGUAGE_KEY] ?: Language.Serbian.name
)
)
fun updateLanguage(value: Language, company: Company? = null) {
currentLanguage = value
window.localStorage[LANGUAGE_KEY] = currentLanguage.name
company?.let { generateCompanyMetadata(company = it) } ?: generateDefaultMetadata()
}
object Key {
val not_selected = "not_selected"
val choose_appointment = "choose_appointment"
val schedule_appointment = "schedule_appointment"
val error_while_resolving_short_code = "error_while_resolving_short_code"
}
// You can store translations in a nested map
private val translations = mapOf(
Language.Serbian to mapOf(
Key.not_selected to "Nije odabrano",
Key.choose_appointment to "Odaberi termin",
Key.schedule_appointment to "Rezerviši termin",
Key.error_while_resolving_short_code to "Greška prilikom čitanja: {0}"
),
Language.Russian to mapOf(
Key.not_selected to "Не выбрано",
Key.choose_appointment to "Выберите запись",
Key.schedule_appointment to "Запланировать запись",
Key.error_while_resolving_short_code to "Ошибка при разрешении короткого кода: {0}",
)
)
// Retrieve translation by key
fun text(key: String): String {
return translations[currentLanguage]?.get(key)
?: "[$key]" // show missing key if not found
}
fun text(key: String, vararg args: Any): String {
val template = translations[currentLanguage]?.get(key) ?: "[$key]"
return args.foldIndexed(template) { index, acc, arg ->
acc.replace("{$index}", arg.toString())
}
}
}
enum class Language(
val flag: String,
val title: String,
) {
Serbian(
flag = "country/RS.svg",
title = "Srpski"
),
Russian(
flag = "country/RU.svg",
title = "Русский",
),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment