我想從代碼中定義可能的錯誤消息 - 在資源中。使它們本地化是沒有問題的。 但我還需要爲每個人定義他們將在哪裏顯示/記錄的地方。例如:screen,localLog,serverLog。 沒有本地化我能做到這樣:如何製作結構化和本地化的資源?
<!-- every error has a message and boolean keys that allows or not to write the message
to places serverLog/localLog/Screen -->
<string-array name="error_names">
<item >no_net</item>
</string-array>
<string-array name="error_messages">
<item >There is no internet connection. Check the cables. If they are OK, call your provider.</item>
</string-array>
<string-array name="output_keys">
<item >010</item>
</string-array>
而且我會做出與加載錯誤信息所有的人一類爲靜態圖
public class ErrorMessage {
private String messageText;
private String errorName;
private ArrayList<String> messageOutputs;
static List<String> messagesPossibleOutputs=Arrays.asList("Server","LocalLog","TV");
static public HashMap<String,ErrorMessage> messagesMap;
static public int downloadMessages(Context context){
String messages[]=context.getResources().getStringArray(R.array.error_messages);
String errorNames[]=context.getResources().getStringArray(R.array.error_names);
String outputKeys[]=context.getResources().getStringArray(R.array.output_keys
messagesMap=new HashMap<String,ErrorMessage>();
for(int i=0; i<messages.length; i++){
ErrorMessage curErrorMessage=new ErrorMessage();
curErrorMessage.errorName=errorNames[i];
curErrorMessage.messageText=messages[i];
curErrorMessage.messageOutputs=new ArrayList();
for(int iKey=0; iKey<outputKeys[i].length();iKey++){
String curChar=outputKeys[i].substring(iKey, iKey+1);
if(curChar.equals("1")){
curErrorMessage.messageOutputs.add(messagesPossibleOutputs.get(iKey));
}
}
messagesMap.put(curErrorMessage.errorName,curErrorMessage);
}
}
它不是一個乾淨的解決方案,只有最好的我想到了。但它還不夠好!因爲如果我想本地化消息(它也應該完成),我必須在每種語言變體中重複/翻譯整個字符串數組。只有一些意味着本地化的消息。技術的仍然是英語。所以,重複一切都很糟糕。
XML資源不會幫助,因爲我需要在每種語言變體中再次重複它作爲一個整體。所以,情況更糟。
請問,您能否提供一些更智能的閱讀資源結構?和/或以其他方式定義結構化和本地化的資源?
P.S.這個問題沒有包含任何反思,但我認爲解決方案很可能在於這個領域。所以我也用反射標記了它。