如何使结构化和本地化的资源? [英] How to make a structured and localized resource?

查看:117
本文介绍了如何使结构化和本地化的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义可能的错误消息了$ C $的c - 资源。是没有问题的,以使它们的局部。
但我也需要定义每个人的地方,在那里他们将被显示/记录。例如:屏幕,localLog,SERVERLOG。
如果没有本地化我能做到这样:

I would like to define the possible error messages out of the code - in resources. There is no problem to make them localized. But I need also to define for every of them the places, where they will be shown/logged. For example: screen, localLog, serverLog. Without localization I could do it so :

<!-- 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> 

和我会做与装载错误信息个个一类为静态图

And I would make a class for error messages with loading all of them into a static map

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 );
    }
}

这不是一个干净的解决方案,只能尽我想到的。但是,它甚至不是不够好!因为如果我要本地化的信息(它应该做的,太),我要重复/翻译的消息在每一个语言变体整个字符串数组。只有那些意味着一些消息进行本地化。技术的人仍英语。因此,重复一切都是坏的。

It is not a clean solution, only the best I thought of. But it is not even good enough! Because if I want to localize the messages (it should be done, too), I have to repeat/translate the whole string-array of messages in every language variant. Only some messages that are meant to be localized. Technical ones remain English. So, repeating everything is bad.

XML资源不会帮助,因为我需要再次重复它作为一个整体在每一种语言变体。因此,它更是雪上加霜。

XML resource won't help, because I need again repeat it as a whole in the every language variant. So, it is even worse.

请,你能劝阅读资源更聪明一些结构?和/或用于定义结构和本地化资源一些其他的方式?

Please, could you advise some smarter structure for reading Resources? And/or some other way for defining structured and localized resources?

P.S。这个问题不包含反射一个字,但我认为,解决办法很可能出在这一领域。于是,我与反思标记这一点。

P.S. The question doesn't contain a word on reflection, but I think that the solution very probably lies in that field. So I tagged it with reflection, too.

推荐答案

不知怎的,没人接......我已经找到了自己的解决方案。这不是一个新的和清洁溶液。它只是阐述了previously描述之一。但它的作品。

Somehow nobody answered... And I have found a solution myself. It is not a new and clean solution. It only elaborates the previously described one. But it works.

我已经简单地划分错误信息两大类 - 即保持英语和本地化的。在code我倒他们到一个列表。本地化的消息将被本地化,不断英语仍将是英语。坏消息是,很难控制,如果将所有的三(所以,多达六)阵列将是互利正确的。

I have simply divided error messages to two categories - that remain English and localizable ones. In code I pour them into one list. Localizable messages will be localized, constantly English will remain English. The bad news is that very hard controlling if all three divided (so, up to six) arrays will be mutually correct.

这篇关于如何使结构化和本地化的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆