2016-01-11 41 views
2

有什麼辦法可以翻譯Android中的複數?可以在Android中翻譯複數嗎?

我的應用程序推送像「任務將在X分鐘內過期」的通知,但我需要用幾種語言來完成此操作。

+0

我不不知道有什麼方法讓android框架做到這一點。我總是在每個語言文件夾中使用一個字符串資源作爲單數(例如'notification_string_singular')和另一個複數(例如'notification_string_plural'),然後通過編程選擇一個取決於是否x == 1' – Michiyo

回答

6

應該沒問題。只需在語言特定的資源文件夾中定義複數(例如,res/values-es/plurals.xml)。該Android documentation on plurals精確地顯示出這樣的事情:

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <plurals name="numberOfSongsAvailable"> 
     <!-- 
      As a developer, you should always supply "one" and "other" 
      strings. Your translators will know which strings are actually 
      needed for their language. Always include %d in "one" because 
      translators will need to use %d for languages where "one" 
      doesn't mean 1 (as explained above). 
      --> 
     <item quantity="one">%d song found.</item> 
     <item quantity="other">%d songs found.</item> 
    </plurals> 
</resources> 

res/values-pl/strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <plurals name="numberOfSongsAvailable"> 
     <item quantity="one">Znaleziono %d piosenkę.</item> 
     <item quantity="few">Znaleziono %d piosenki.</item> 
     <item quantity="other">Znaleziono %d piosenek.</item> 
    </plurals> 
</resources> 

在代碼中,你可以使用這樣的:

int count = getNumberOfsongsAvailable(); 
Resources res = getResources(); 
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count); 
+0

完美解釋,謝謝: ) –

相關問題