skip to Main Content

Im having trouble doing a translation in my project:

So I have this enum class:

enum class Theme(
    val theme: ThemeL,
    val name: String,
    @DrawableRes val image: Int? = null,
){
THEME_1(
        theme = ThemeL.Theme1,
        humanName = "theme1 description",
        image = R.drawable.theme1
    ),
    THEME_2(
        theme = ThemeL.Theme2,
        humanName = "theme2 des",
        image = R.drawable.theme1
    ),
}

My question is, I want that "humanName" can be translated to another languages,
in order to do that I created another strings.xml and defined the location I wanted in android studio, however I cant define here what should I put in "humaname = …" to achieve so.

my original strings.xml

  <string name="theme1">theme one</string>
    <string name="theme2">theme two</string>

and the one to be translated in portuguese

<string name="theme1">tema um</string>
    <string name="theme2">tema dois</string>

2

Answers


  1. To correctly translate strings into other languages, you need to create a folder for each language you wish to translate to in RES. For example, name the folder "values-pt". Within this folder, create an XML file called "strings" and place the strings you want to translate inside.

    Login or Signup to reply.
  2. Instead of using String, you can use String id:

    enum class Theme(
        val theme: ThemeL,
        @StringRes val humanName: Int,
        @DrawableRes val image: Int? = null,
    ){
    THEME_1(
            theme = ThemeL.Theme1,
            humanName =  R.string.theme1,
            image = R.drawable.theme1
        ),
        THEME_2(
            theme = ThemeL.Theme2,
            humanName =  R.string.theme1,
            image = R.drawable.theme1
        ),
    }
    

    Then in composable function use String id:

    @Composable
    fun ThemeHuman(theme: Theme) {
    
      val humanName = stringResource(id = theme.humanName)
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search