如何打造结构化、本地化的资源?

发布于 2024-12-25 14:02:41 字数 2276 浏览 2 评论 0原文

我想在资源中的代码中定义可能的错误消息。将它们本地化是没有问题的。 但我还需要为每个人定义显示/记录它们的位置。例如:屏幕、本地日志、服务器日志。 如果没有本地化,我可以这样做:

<!-- 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 资源不会有帮助,因为我需要在每种语言变体中再次将其作为一个整体重复。所以,情况更糟。

请问您能否建议一些更智能的阅读资源结构?和/或定义结构化和本地化资源的其他方式?

PS 这个问题不包含有关反思的词,但我认为解决方案很可能就在该领域。所以我也用反射标记它。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2025-01-01 14:02:41

不知怎的,没有人回答......而我自己找到了解决方案。这不是一个新的、干净的解决方案。它仅详细阐述了前面描述的一项。但它有效。

我只是将错误消息分为两类 - 仍然是英语的和可本地化的。在代码中,我将它们倒入一个列表中。可本地化的消息将被本地化,英语始终保持英语。坏消息是,如果所有三个划分的(最多六个)数组都相互正确,则很难控制。

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.

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.

溺孤伤于心 2025-01-01 14:02:41

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

您可以将不想本地化的资源放入未本地化的文件夹(如“值”)中,一次即可。 。

您想要本地化的资源必须放入本地化匹配文件夹(如“values-ko_KR”)的文件夹中

此外,本地化资源和非本地化资源的 id 必须不同。

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

我是不是误会了?

I'm not sure that I understood your question but writing something because that perhaps help you.

You can put resources that never want to be localized to folder that not localized like 'values' all together only once. and

Resources that you want to be localized must put to folders that localized-matching folders like 'values-ko_KR'.

Also the id of resources must different between localized one and not one.

Then you can load all resources together into one map.

Am I misunderstood?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文