具有编译时检查的l18n框架 [英] l18n framework with compiletime checking

查看:75
本文介绍了具有编译时检查的l18n框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个更大的Java桌面应用程序,现在正要添加翻译.我对l18n系统感到困扰的是,它不提供任何形式的编译时检查.

I'm currently working on a bigger Java desktop-application and now are at the point where I want to add translations. What bothers me about the l18n-system is, that it doesn't provide any kind of compiletime checking.

在java的系统中,您有一个类似于HashMap的东西,其中每个本地化的字符串都有一个键",而翻译后的字符串则是值".看起来像这样(摘自教程示例):

In java's system, you have something like a HashMap, where every localized string has a "Key" and the translated string then is the "Value". This looks something like this (taken from the tutorials example):

Locale currentLocale;
ResourceBundle messages;

currentLocale = new Locale(language, country);

messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
System.out.println(messages.getString("greetings"));

如果您有简单/小型应用程序,则此方法很好用.但是,在具有成千上万个已翻译字符串的大型应用程序中,可能会发生这样的情况,即您在键"中输入了错字,因此得到了一个空的或错误的字符串.

This works nice if you have a simple/small application. But in a big application with thousands of translated strings, it might so happen that you have a typo in the "Key" and therefore get an empty or wrong string.

幸运的是,应用程序抛出了RuntimeException来告诉您有关信息,但是即使那样,您甚至可能还没有到这一点,因为在错误的对话框中使用了错误的键"在某些情况下不会显示(例如,这是一个错误对话框).

With a little luck, the application throws a RuntimeException to tell you about it, but even then it is possible that you don't even come to this point because the wrong "Key" is used in a dialog that might not show up under certain circumstances (say it's an error dialog).

为防止这种情况发生,最好使用提供编译时检查已用过的键"的系统.例如,这在Android中使用,您可以在XML文件中指定资源,然后将其编入索引并映射到一个类(包括要使用的键").这样,您会得到以下内容(来自 Android文档):

To prevent this from happening, using a system which offers compiletime checking of the used "Keys" would be the better idea. This is for example used in Android, where you specify the Resources in an XML-file which is then indexed and mapped to a class (including the "Keys" to use). This way, you get something like this (from the Android Docs):

// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);

如果您在该键"中有错别字,则在编译时会收到错误消息(IDE中也有自动完成"字样).

If you have a typo in that "key", you'll get an error at compile-time (also you have "auto-completion" from your IDE).

现在,要进行类似的工作,您将需要一个小的工具/脚本来做索引部分并生成Resource-class(R.java).在Android中,Eclipse插件(或通常是您的IDE)可以为您完成此任务.

Now, to make something like this work, you'll need a little tool/script that does the indexing part and generates the Resource-class (R.java). In Android, the Eclipse-plugin (or your IDE in general) does that for you.

我现在的问题是:是否已经有一个可用于Java中普通桌面应用程序的系统?还是我所说的话太可怕了?

My question now is: Is there already a system that that I can use for normal desktop-applications in Java? Or am I horribly wrong with what I'm saying?

推荐答案

有一个相当简单的解决方案.首先,不要使用魔术字符串作为代码,不要定义常量并引用它们.所以你改变了.

There is a fairly simple solution to this problem. First, don't use magic strings as codes, define constants and refer to them. So you change..

messages.getString("greetings");

messages.getString(I18.GREETINGS_CODE);

并具有相应的类;

public class I18 {

  public static final String GREETINGS_CODE = "greetings";

}

接下来编写一个测试用例,其中在每个语言资源文件中查找I18类中的每个代码.如果任何资源文件缺少代码,则测试失败.它不是编译时间,但是如果您有任何问题,您的项目将无法通过测试.

Next write a test case where each code in your I18 class is looked up in each language resource file. If any resource file is missing the code, then fail the test. Its not compile time, but your project will fail its test if you have any problems.

这篇关于具有编译时检查的l18n框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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