question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String resources for internationalization (i18n)

See original GitHub issue

I’d like to see an example of how to do internationalization for Compose Desktop.

I looked at the Code Viewer example, as it’s written to run under both Android and desktop. It doesn’t yet address that feature; its Text widgets and contentDescription values are hard-coded, e.g. https://github.com/JetBrains/compose-jb/blob/4de10a649910053ed634429b88c82541989bf332/examples/codeviewer/common/src/commonMain/kotlin/org/jetbrains/codeviewer/ui/editor/EditorEmptyView.kt#L28-L29

Can we use stringResource (from androidx.compose.ui.res) on desktop? How do we get the resource ID if we’re developing for desktop and not using Android Studio?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:19
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
nrobi144commented, Mar 29, 2021
5reactions
igordmncommented, Mar 5, 2021

One of the complex things is pluraziation support.

Java only provides ChoiceFormat, which properly works only with some languages.

Proper pluraziation support implemented in ICU.

We can provide a function for pluralization in skiko (because skia already uses icu library).

Or write a pluralization function in pure Kotlin/Java using patterns (example)

Another thing we need to think about - how string resources should be defined:

  1. We can write strings in pure Kotlin:
val strings = when(Locale.getDefault().language) {
    "en" -> EnStrings
    "ru" -> RuStrings
    else -> EnStrings
}

interface Strings {
    val appName: String get() = "Image viewer"
    val open: String
    fun folderFileCount(count: Int): String
}

object EnStrings : Strings {
    override val open = "Open"
    override fun folderFileCount(count: Int) = "Folder file count: ${fileCount(count)}"

    private fun fileCount(count: Int) = plural(
        count,
        zero = "$count files",
        one = "$count file",
        many = "$count files"
    )
}

object RuStrings : Strings {
    override val open = "Открыть"
    override fun folderFileCount(count: Int) = "Количество файлов в папке: ${fileCount(count)}"

    private fun fileCount(count: Int) = plural(
        count,
        zero = "$count файлов",
        one = "$count файл",
        many = "$count файлов"
    )
}

Don’t know if it is a good idea. It maybe convinient for string formatting, but not convinient for translators.

  1. Provide android-like strings.xml. There is MPP implementation Moko resources. But support for JVM target isn’t implemented yet. There is PR, but for plurals it uses ChoiceFormat.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Resource Files Best Practices for i18n & Localization - Lingoport
User-facing strings are typically found in user interfaces: menu items, labels, descriptions, and text elements that are rendered in the application.
Read more >
A Step-by-Step Guide to Go Internationalization (i18n)
A Step-by-Step Guide to Go Internationalization (i18n) ... A Catalog defines collections of translated format strings.
Read more >
Internationalizing UI Strings | Adobe Experience Manager
The I18n class provides the get method that retrieves localized strings from the AEM dictionary. The only required parameter of the get method ......
Read more >
Chapter 16. Internationalization, localization and themes
You'll also need localized strings for all the messages in your application (for example field labels on your views). First you need to...
Read more >
Internationalization Guide - Java - Oracle Help Center
All locale-sensitive classes must be able to access resources customized for the locales they support. To aid in the process of localization, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found