English 中文(简体)
专制时间检查的18框架
原标题:l18n framework with compiletime checking

目前,Im公司正在从事较大的 Java台式应用工作,现在正在准备增加翻译。 我对18岁制度的看法是,它没有提供任何形式的编辑时间检查。

在java s系统中,你拥有像HashMap这样的东西,每个地方化的座标都有“Key”,而翻译的座标当时是“Value”。 参见:http://docs.oracle.com/javase/tutorial/i18n/intro/ subsequently.html。

Locale currentLocale;
ResourceBundle messages;

currentLocale = new Locale(language, country);

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

如果您申请简单/小小,这便行不通。 但是,在大量应用中,有数千处翻译,因此,你可能会在“灰色”中打上打字,从而得到空洞或错误的扼杀。

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

为防止这种情况发生,采用一种系统,对旧的“灰色”进行实时检查,将是一个更好的想法。 例如,在安伯,你在一份XML-书上具体说明了资源,然后将其编入索引,并绘制成一个类别(包括“关键”使用)。 这样,你们就获得这样的东西(从http://developer.android.com/guide/topics/resources/accessing-resources.html>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);

如果你在“钥匙”中有一个字体,那么你在汇编时就会出现错误(你也从民主选举学会那里获得了“独一无二的”。

现在,为了像这项工作一样做一些事情,你需要很少的工具/文字,而这种工具/文字却能够把一部分指数化并产生资源类别(R.java)。 在安伯,Eclipse-plugin(或您的一般发展研究所)对你来说就是这样。

我现在的问题是:是否有我能够用于 Java正常桌面应用的系统? 或者,我说什么是令人可怕的错吗?

最佳回答

解决这一问题的办法相当简单。 首先,不把魔法用作法典,界定恒定和参照。 因此,你们改变了。

messages.getString("greetings");

页: 1

messages.getString(I18.GREETINGS_CODE);

并且有相应的班级;

public class I18 {

  public static final String GREETINGS_CODE = "greetings";

}

随后在每种语文资源档案中查阅<代码>I18中的每一代码。 如果任何资源档案都缺失,那么就会失败。 如果你有任何问题,该项目将失败。

问题回答

我看到,使用标准资源Bundles和信任我的企业应用很少,这不是一个问题。

成功因素很少:

  1. Resource Organization
  2. Resource Helpers
  3. Resource Factories
  4. Key naming strategy

Ad 1. Your issues will be especially visible if you decide on using single Resource Bundle. Do not go this way. There is no way to maintain large, single file. You will face problems with key duplication, key naming and so. Besides, with large applications it is common to have few people working on the same set of files at the same time. In case of Resource Bundles it means a lot of merging and getting each other in the way. This is the problem in the regular programming team, but trust me with Localizers it is even worse.
What you need is multiple, small resource files. Depending on the application scale, it might be one file per module, or even one file per dialog. How to split the files depends on the actual application, but what I recommend is not having resource file longer than two screens of text.
With large number of files, the problem is how to organize them - put to valid directories. What I d suggest is to separate resource files from the source code (place them in the separate directory called - for example - L10n). Under the resource files root it is good to have language folders (so that is easy to separate the translations for each target language), although it would require messing with ResourceBundle.Control class. Then under the language root you would create subdirectories for each module. Under the module root you would have either separate subdirectories (with very large application), or you will place your Resource Bundles directly.

Ad 2. There is no way to use ResourceBundle class directly. One of the problems is MissingResourceException thrown if there is no such key in the file(s), or there is no file you are looking for. Instead, you would probably like to see (on the User Interface) that something went wrong, for example the name of the key between exclamation marks (!the.key.does.not.exist!) - this is what Eclipse s "Externalize Strings" does by default.
You might decide on creating Resource Helper which might be either wrapper on ResourceBundle or may derive from it. The choice is yours (although with serious Resource Organization like above the reasonable choice is to derive, as you need to create your own ResourceBundle.Control class). Beside simple translation handling you can add additional functionality to Resource Helper, like handling Message Formatting, Plural Forms, etc.
For now it won t handle your problem of wrong resource key names. You would need actual UI testing - the typos would be visible. In case of using wrong keys (especially for someone who use Copy-Paste Design Pattern ;)) I don t think that there is any technology that would ever resolve the issue. Machines doesn t think. Anyway, you can handle the problems with keys by placing (for example) nested classes with key constants or nested enums. The problem with such solution is, you would need separate Resource Helper for each module, which is not necessary the good idea (especially in large application). It is feasible, but cumbersome. And of course IDE s built-in mechanism for externalizing strings won t work with this solution.

阿 曼 3. 为了处理大量资源档案,你需要建造固定资源工厂(工厂),为你们创造资源帮助者。 我的战略是,在工厂内打上一个公开可见的宝石,并拥有资源档案,作为其固定。 然后,我请求打上有效的dle,例如:

ResourceHelper helper = ResourceFactory(
                          ResourceFactory.Bundles.ERRORS, Locale locale);

阿 曼 4. 为了避免出现麻烦,小组必须就单一资源关键命名战略作出决定。 我建议的关键名称是:

  • dialog or unit
  • descriptive name
  • the context

例如:

  • validation.invalid.username.error
  • validation.enter.pass.label
  • validation.dialog.title
  • menu.main.file.open.item
  • login.dialog.ok.button

笔译员的情况(标签、方言、物品、错误、电文、型号等等)为<><>十分重要<>。 译文通常视具体情况而有所不同。 这也是你为什么不重新使用译文的原因(除了诸如通用国名“K”、“Cancel”、“Abort”、“Help”或通用国名和项目等非常常见的项目之外)。

我永远不会对这一工具感到振奋,因为这个工具能够把你的资源指数化。 Imho, 相当不寻常的要求。 其中一种做法——不是在你的法典中使用直面字面,而是将其归入单一类别,用静态的改动宣布。 利用这一常态作为你们资源图的关键。 非常容易的是,用反思来检验在资源档案中单列类别申报的所有资源。 它更加容易,然后撰写法典分析员

我想c10n 是你所期待的(例如:www.eee.org)。

The whole idea is to relieve you from writing error-prone keys, and replace them with compile-time checked plain Java interface methods, annotated with the translation values.

例如:

public interface Bundle{
  @En("Hello, World!")
  @De("Hallo Welt!")
  String greeting();
}

// ... somewhere in your code
public class MyClass{
  private static final Bundle bundle = C10N.get(Bundle.class);

  public void myMethod(){
    System.out.println(bundle.greeting());
  }
}

您可在<代码>Bundle接口上做任何调整,重新命名,并在周围进行移动,增加更多的接口,扩大接口,组成。 只要用javac汇编成册,你就可以确保翻译正确。

See the link above more details.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签