GWT i18n - 复数形式根本不起作用?
我正在使用 GWT 国际化消息。 复数形式文档说这应该有效:
@DefaultMessage("{0} {1,number} hours {2}")
@PluralText({"one", "an hour"})
String hours(String prefix, @PluralCount int count, String suffix);
嗯,事实并非如此't。无论 count
的值如何,它仍然会传递 DefaultMessage
(例如“1 小时前”)。如果我使用 .properties 文件,则相同:
hours[one]=an hour
hours[few]=some hours
hours={0} {1,number} hours {2}
文档或 GWT(我正在使用 GWT 2.0.3)或我体内是否存在错误?如果是前两者中的任何一个,有人知道解决方法吗?
编辑:这个谜团的更多线索。如果我不依赖默认的区域设置处理,我可以获得复数处理。也就是说,我需要:
在我的模块的 gwt.xml 文件中:
在我的消息扩展中:
@DefaultLocale("en")
public interface MyMessages extends Messages { ...
通过附加到 URL 来显式加载“en”区域设置
&locale=en
请参阅 http://groups.google.com/group/google-web-toolkit/browse_thread/ thread/80ae300213cc6adb 我在其中交叉发布了这个问题。
编辑2:我进入这个GWT复数领域的原因是我正在创建一个“GWT HUman Readablerelative Timestamps”模块。在 GitHub 上开源:http://github.com/PEZ/GWT-Relative-Time 请检查一下。它很快就会有正确的单数形式并支持某些语言。 =)
I'm using GWT internationalization Messages. The documentation for Plural Forms says this should work:
@DefaultMessage("{0} {1,number} hours {2}")
@PluralText({"one", "an hour"})
String hours(String prefix, @PluralCount int count, String suffix);
Well, it doesn't. Whatever value of count
it still delivers DefaultMessage
(e.g. "1 hours ago"). Same if I use a .properties file:
hours[one]=an hour
hours[few]=some hours
hours={0} {1,number} hours {2}
Is there a bug in the docs or in GWT (I'm using GWT 2.0.3) or in me? If any of the two former, anyone knows of a workaround?
EDIT: More clues to this mystery. I can get plural handling to work if I don't rely on default locale handling. That is, I need:
In my module's gwt.xml file:
<extend-property name="locale" values="en"/>
In my Messages extentsion:
@DefaultLocale("en")
public interface MyMessages extends Messages { ...
Explicitly load the "en" locale by appending to the URL
&locale=en
See http://groups.google.com/group/google-web-toolkit/browse_thread/thread/80ae300213cc6adb where I have cross posted this question.
EDIT 2: The reason I entered this GWT plurals land is that I'm creating a "GWT HUman Readable Relative Timestamps" module. Open sourced at GitHub: http://github.com/PEZ/GWT-Relative-Time Please check it out. It'll very soon have correct singular forms and support for some languages. =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现的另一件事是,您可以像这样定义后备区域设置:
;在你的模块 XML 文件中
One more thing I found is that you can define a fallback locale like this:
<set-property-fallback name="locale" value="en"/> in your module XML file
此处的 gwt 问题列表中的另一个解决方案:
您必须在 MyMessages.java:
和 MyMessages.properties 中定义 @DefaultLocale:
Another solution from the gwt issue list here:
You have to define the @DefaultLocale in the MyMessages.java:
and in MyMessages.properties:
习惯于回答我自己的问题 =) 这是我在 GWT Google 群组上交叉发布的“答案”:
默认区域设置处理似乎存在错误。我是这样得出这个结论的:
我想向我的模块添加一些语言环境。我想如果我使用 @Generate 注释,我可以获得属性文件的样板。我注意到它创建了一个
_en.properties
文件和一个_default.properties
。更重要的是; _en
文件完全缺少复数形式信息!不过 _default
文件有它们。然后,我将
_defaults
文件移至与TimeMessages.java
文件相同的目录,并将其重命名为TimeMessages_default.properties
。有了这个,我可以从模块的 .gwt.xml 文件中删除
,更重要的是,& ;locale=en
来自运行我的应用程序时的 URL。尽管文档明确指出这不是必需的,但我仍然需要@DefaultLocale("en")
注释。总之,如果您遇到此问题,请尝试:
@Generate
生成属性文件,YourMessages_default.properties
与YourMessages.java
并排放置@DefaultLocale("en")
注释。关于@Generate。这对我有用。就在我扩展消息接口之前:
GWT 日志说它创建了我的属性文件,但我找不到它。我通过添加编译器标志
-extra extras
修复了这个问题,然后找到了extras
目录中生成的属性文件。在此包含此信息,因为我花了一个多小时才弄清楚。Getting used to answering my own questions =) Here's cross post of my "answer" on the GWT Google group:
There seems to be a bug with the default locale handling. Here's how I have reached that conclusion:
I wanted to add some locales to my module. Figured I could get a boiler plate for the properties file if I used the
@Generate
annotation. I noticed that it created both an_en.properties
file and a_default.properties
. What's more; the _en
file completely lacked the plural form info! The _default
file had them though.I then moved the
_defaults
file to the same directory as myTimeMessages.java
file and renamed itTimeMessages_default.properties
.With this in place I can remove
<extend-property name="locale" values="en"/>
from my module's .gwt.xml file and, more important, the&locale=en
from the URL when running my app. I still need the@DefaultLocale("en")
annotation though, even though the documentation clearly states that this is not necessary.In conclusion, if you run into this problem, try:
@Generate
YourMessages_default.properties
side by side withYourMessages.java
@DefaultLocale("en")
annotation.About that
@Generate
. This is what worked for me. Just before my extension of the Message interface:The GWT log said it created my properties file, but I couldn't find it. I fixed that by adding the compiler flag
-extra extras
and then found the properties files generated in theextras
directory. Including this info here since I spent more than an hour figuring it out.另外,我认为知道默认的英语复数规则仅支持
"other"
和"one"
会节省人们的时间。DefaultRule_en.java 使用 DefaultRule_1_0n.java
因此,如果您想要使用
“无”
、“两个”
、很少”
...您可以在包中查看可用规则 com.google.gwt.i18n.client.impl.plurals。
Also I think it will save people time to know that the default English plural rule only supports
"other"
and"one"
.DefaultRule_en.java uses DefaultRule_1_0n.java
So you have to redefine
DefaultRule_en.java
if you want to use"none"
,"two"
,few"
...You can see the available rules in the package com.google.gwt.i18n.client.impl.plurals.