Android Studio 在Android中翻译变量

vsnjm48y  于 8个月前  发布在  Android
关注(0)|答案(2)|浏览(61)

我在我的项目中遇到了翻译困难:
所以我有这个enum类:

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
    ),
}

我的问题是,我希望“humanName”可以被翻译成其他语言,为了做到这一点,我创建了另一个strings.xml,并定义了我在android studio中想要的位置,但我不能在这里定义我应该在“humaname =.”中放置什么来实现这一点。
我的原始strings.xml

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

还有一本要翻译成葡萄牙语

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

w1jd8yoj1#

要正确地将字符串翻译成其他语言,您需要在RES中为您希望翻译的每种语言创建一个文件夹。例如,将文件夹命名为“values-pt”。在此文件夹中,创建一个名为“strings”的XML文件,并将您要翻译的字符串放入其中。

lfapxunr

lfapxunr2#

你可以使用String id来代替String:

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
    ),
}

然后在可组合函数中使用String id:

@Composable
fun ThemeHuman(theme: Theme) {

  val humanName = stringResource(id = theme.humanName)

}

相关问题