2012-01-09 95 views
0

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

回答

0

不知何故没有人回答...我自己找到了解决方案。这不是一个新的和干净的解决方案。它只是阐述了前面描述的一个。但它的工作。

我简单地将错误消息分为两类 - 即英文和可本地化的错误消息。在代码中,我将它们倒入一个列表中。可本地化的消息将被本地化,不断英语将保持英语。坏消息是,如果所有三个分组(如此,最多六个)阵列相互正确,则非常难控制。

0

我不确定我是否理解了你的问题,但写了一些东西,因为这可能会帮助你。

您可以将永远不需要本地化的资源放到不像“值”本地化的文件夹中,只有一次。和

您想要本地化的资源必须放置在与“values-ko_KR”等本地化匹配文件夹相同的文件夹中。

此外资源的ID必须不同于本地化的而不是一个。

然后,您可以将所有资源一起加载到一个地图中。

我误解了吗?