[英]How to a xml string to an string output
使用 Android Studio,我想知道如何讓“月”從 xml 字符串中獲取一個字符串。 (目前正在學習android)。 需要從 strings.xml 中提取,因為我需要將其翻譯成另一種語言。
else if (human_year == 0) {
return Integer.toString(Math.round(human_month)) + " months";
輸出:years - 可以翻譯成西班牙語(這是工作已經設置了一個按鈕來翻譯)目前所有鏈接回 string.xml 的單詞都被翻譯。 由於此“月”未附加到字符串中,因此不會被翻譯。
首先聲明“月”
英文字符串(默認語言環境),/values/strings.xml
<resources>
<string name="myStringMonths">months</string>
</resources>
然后是西班牙語
西班牙語字符串(es locale),/values-es/strings.xml:
<resources>
<string name="myStringMonths">meses</string>
</resources>
然后在您的代碼中將其如下所示:
else if (human_year == 0) {
return String.format("%d", Math.round(human_month)) + getString(R.id.myStringMonths);
為此使用“數量字符串” 。
在您的資源目錄中創建一個plurals.xml
文件。 然后用以下內容填充它:
<resources>
<plurals name="months">
<item quantity="one">%1$d month</item>
<item quantity="other">%1$d months</item>
</plurals>
</resources>
您可以為不同的語言環境創建不同的文件。
然后,您可以在代碼中訪問它:
final int months = Math.round(human_month);
return resources.getQuantityString(R.plurals.months, months, months)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.