我想从代码中定义可能的错误消息 - 在资源中。使它们本地化是没有问题的。 但我还需要为每个人定义他们将在哪里显示/记录的地方。例如: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.这个问题没有包含任何反思,但我认为解决方案很可能在于这个领域。所以我也用反射标记了它。