如何从我的属性文件中重构HTML标记?

时间:2022-09-20 10:47:08

I've recently inherited a internationalized and text-heavy Struts 1.1 web application. Many of the JSP files look like:

我最近继承了一个国际化和文本密集的Struts 1.1 Web应用程序。许多JSP文件看起来像:

<p>
    <bean:message key="alert" />
</p>

and the properties files look like:

和属性文件看起来像:

messages.properties
alert=Please update your <a href="/address.do">address</a> and <a href="/contact.do">contact information</a>.

with the appropriate translations in N other languages (messages_fr.properties, etc).

使用N种其他语言的适当翻译(messages_fr.properties等)。

Problems:

  1. DRY violation - I have N references to my Struts action URLs instead of 1, which makes refactoring action URLs error-prone.
  2. DRY违规 - 我有N个引用我的Struts操作URL而不是1,这使得重构操作URL容易出错。

  3. Mixed concerns - My application's markup is now in more than just my JSP files, making it difficult for a web specialist to tweak the markup (using CSS, etc).
  4. 混合问题 - 我的应用程序的标记现在不仅仅是我的JSP文件,这使得Web专家很难调整标记(使用CSS等)。

  5. Post-translation markup - Anytime I receive newly-translated text, I must decide what to surround with the <a>...</a> markup. Easy for English but less so for unfamiliar languages.
  6. 翻译后标记 - 每当我收到新翻译的文本时,我必须决定用 ... 标记包围什么。容易学习英语,但对于不熟悉的语言则不那么容易。

I've considered adding placeholders in the messages file, like:

我考虑在消息文件中添加占位符,例如:

alert=Please update your {0} and {1}.

but then the words "address" and "contact information" would somehow need to be localized, wrapped with markup, and passed to my message tag - and I can't see an easy way to do it.

但随后单词“地址”和“联系信息”将以某种方式进行本地化,包含标记,并传递给我的消息标记 - 我看不到一个简单的方法来做到这一点。

What can I do to improve this?

我该怎么做才能改善这个?

4 个解决方案

#1


2  

Avoid creating links within long blocks of text. Prefer shorter text that can act as a logically complete and independent link.

避免在长文本块中创建链接。更喜欢可以作为逻辑完整和独立链接的较短文本。

Generally, it will lead to fewer problems. Sometimes you have to compromise your UI design to accommodate localization; sometimes you need to compromise your localization process to accommodate the UI.

通常,它会导致更少的问题。有时您必须妥协您的UI设计以适应本地化;有时您需要妥协您的本地化过程以适应UI。

Any time a developer manually manipulates post-translation strings is a source of potentially expensive bugs. Cutting/pasting or string editing can result in character corruption, misplaced strings, etc. A translation defect needs the participation of outside parties to fix which involves cost and takes time.

任何时候开发人员手动操作翻译后字符串都是潜在的昂贵错误的来源。剪切/粘贴或字符串编辑可能导致字符损坏,错误的字符串等。翻译缺陷需要外部参与者的参与才能确定哪些涉及成本并需要时间。

Thinking on it, something like this might be less ugly:

考虑到这一点,这样的事情可能不那么难看:

<p>Please update your address and contact information.
<br />
<a href="/address.do">update address</a>
<br />
<a href="/contact.do">update contact information</a></p>

...but I'm no UI designer.

...但我不是UI设计师。

#2


1  

One approach that comes to mind is that you could store the translated replacement parameters i.e. "address" and "contact information" in a separate properties file, one per locale. Then have your Action class (or probably some helper class) look up the values from the correct ResourceBundle for the current locale and pass them to the message tag.

我们想到的一种方法是,您可以将已翻译的替换参数,即“地址”和“联系信息”存储在单独的属性文件中,每个区域设置一个。然后让你的Action类(或者可能是一些帮助类)从当前语言环境的正确ResourceBundle中查找值并将它们传递给消息标记。

#3


0  

Perhaps:

#
alert=Please update your {0}address{1} and {2}contact information{3}.

#4


0  

The message message tag API allows only 5 parametric arguments

消息消息标记API仅允许5个参数参数

Ah! I blame my complete ignorance of the Struts API.

啊!我责怪我对Struts API的完全无知。

To quote the manual:

引用手册:

Some of the features in this taglib are also available in the JavaServer Pages Standard Tag Library (JSTL). The Struts team encourages the use of the standard tags over the Struts specific tags when possible.

此taglib中的某些功能也可在JavaServer Pages标准标记库(JSTL)中找到。 Struts团队鼓励尽可能在Struts特定标签上使用标准标签。

You could probably do this with the http://java.sun.com/jsp/jstl/fmt taglib.

您可以使用http://java.sun.com/jsp/jstl/fmt taglib执行此操作。

<fmt:bundle basename="messages">
    <fmt:message key="alert">
        <fmt:param value='<a href="/">' />
        <fmt:param value="</a>" />
        <fmt:param value='<a href="/">' />
        <fmt:param value="</a>" />
    </fmt:message>
</fmt:bundle>

The downside is that this isn't valid XML and yanking the values to variables involves more indirection, lookups and verbosity. This is not a good solution.

缺点是这不是有效的XML,并且将值赋予变量涉及更多的间接,查找和详细程度。这不是一个好的解决方案。

I don't know Struts, but if it is anything like JavaServer Faces (same architect), then there is probably support for configuring a replacement control. I would either replace the existing control with a more flexible one or add a new one.

我不知道Struts,但如果它像JavaServer Faces(相同的架构师),那么可能支持配置替换控件。我会用更灵活的控件替换现有控件或添加新控件。

Anytime I receive newly-translated text, I must decide what to surround with the <a>...</a> markup.

每当我收到新翻译的文本时,我必须决定用 ... 标记包围什么。

There is no way you should be doing this and I see this as a fault in your translation process (I am an ex-localization engineer and ex-developer of localization tools). The {0} characters should be included in the files that are sent to the translators. The localization guidelines should explain the string's context and the meaning of any variables.

你不应该这样做,我认为这是你的翻译过程中的一个错误(我是一个前本地化工程师和本地化工具的前开发人员)。 {0}字符应包含在发送给转换程序的文件中。本地化指南应该解释字符串的上下文和任何变量的含义。

You can programmatically validate the property bundles on return. String-specific regex's might do the trick. It isn't outside the realms of possibility that "address" and "contact information" would swap order during translation.

您可以在返回时以编程方式验证属性包。字符串特定的正则表达式可能会成功。在翻译过程中,“地址”和“联系信息”会交换顺序,这不属于可能的范围。

The simplest solution is to redesign the messages to render:

最简单的解决方案是重新设计要呈现的消息:

<a href="/address.do">Please update your address.</a>
<a href="/contact.do">Please update your contact information.</a>

I accept that this might not be a solution in all cases and may have your UI designer spitting teeth.

我接受这可能不是所有情况下的解决方案,并且可能让您的UI设计师唾手可得。

#1


2  

Avoid creating links within long blocks of text. Prefer shorter text that can act as a logically complete and independent link.

避免在长文本块中创建链接。更喜欢可以作为逻辑完整和独立链接的较短文本。

Generally, it will lead to fewer problems. Sometimes you have to compromise your UI design to accommodate localization; sometimes you need to compromise your localization process to accommodate the UI.

通常,它会导致更少的问题。有时您必须妥协您的UI设计以适应本地化;有时您需要妥协您的本地化过程以适应UI。

Any time a developer manually manipulates post-translation strings is a source of potentially expensive bugs. Cutting/pasting or string editing can result in character corruption, misplaced strings, etc. A translation defect needs the participation of outside parties to fix which involves cost and takes time.

任何时候开发人员手动操作翻译后字符串都是潜在的昂贵错误的来源。剪切/粘贴或字符串编辑可能导致字符损坏,错误的字符串等。翻译缺陷需要外部参与者的参与才能确定哪些涉及成本并需要时间。

Thinking on it, something like this might be less ugly:

考虑到这一点,这样的事情可能不那么难看:

<p>Please update your address and contact information.
<br />
<a href="/address.do">update address</a>
<br />
<a href="/contact.do">update contact information</a></p>

...but I'm no UI designer.

...但我不是UI设计师。

#2


1  

One approach that comes to mind is that you could store the translated replacement parameters i.e. "address" and "contact information" in a separate properties file, one per locale. Then have your Action class (or probably some helper class) look up the values from the correct ResourceBundle for the current locale and pass them to the message tag.

我们想到的一种方法是,您可以将已翻译的替换参数,即“地址”和“联系信息”存储在单独的属性文件中,每个区域设置一个。然后让你的Action类(或者可能是一些帮助类)从当前语言环境的正确ResourceBundle中查找值并将它们传递给消息标记。

#3


0  

Perhaps:

#
alert=Please update your {0}address{1} and {2}contact information{3}.

#4


0  

The message message tag API allows only 5 parametric arguments

消息消息标记API仅允许5个参数参数

Ah! I blame my complete ignorance of the Struts API.

啊!我责怪我对Struts API的完全无知。

To quote the manual:

引用手册:

Some of the features in this taglib are also available in the JavaServer Pages Standard Tag Library (JSTL). The Struts team encourages the use of the standard tags over the Struts specific tags when possible.

此taglib中的某些功能也可在JavaServer Pages标准标记库(JSTL)中找到。 Struts团队鼓励尽可能在Struts特定标签上使用标准标签。

You could probably do this with the http://java.sun.com/jsp/jstl/fmt taglib.

您可以使用http://java.sun.com/jsp/jstl/fmt taglib执行此操作。

<fmt:bundle basename="messages">
    <fmt:message key="alert">
        <fmt:param value='<a href="/">' />
        <fmt:param value="</a>" />
        <fmt:param value='<a href="/">' />
        <fmt:param value="</a>" />
    </fmt:message>
</fmt:bundle>

The downside is that this isn't valid XML and yanking the values to variables involves more indirection, lookups and verbosity. This is not a good solution.

缺点是这不是有效的XML,并且将值赋予变量涉及更多的间接,查找和详细程度。这不是一个好的解决方案。

I don't know Struts, but if it is anything like JavaServer Faces (same architect), then there is probably support for configuring a replacement control. I would either replace the existing control with a more flexible one or add a new one.

我不知道Struts,但如果它像JavaServer Faces(相同的架构师),那么可能支持配置替换控件。我会用更灵活的控件替换现有控件或添加新控件。

Anytime I receive newly-translated text, I must decide what to surround with the <a>...</a> markup.

每当我收到新翻译的文本时,我必须决定用 ... 标记包围什么。

There is no way you should be doing this and I see this as a fault in your translation process (I am an ex-localization engineer and ex-developer of localization tools). The {0} characters should be included in the files that are sent to the translators. The localization guidelines should explain the string's context and the meaning of any variables.

你不应该这样做,我认为这是你的翻译过程中的一个错误(我是一个前本地化工程师和本地化工具的前开发人员)。 {0}字符应包含在发送给转换程序的文件中。本地化指南应该解释字符串的上下文和任何变量的含义。

You can programmatically validate the property bundles on return. String-specific regex's might do the trick. It isn't outside the realms of possibility that "address" and "contact information" would swap order during translation.

您可以在返回时以编程方式验证属性包。字符串特定的正则表达式可能会成功。在翻译过程中,“地址”和“联系信息”会交换顺序,这不属于可能的范围。

The simplest solution is to redesign the messages to render:

最简单的解决方案是重新设计要呈现的消息:

<a href="/address.do">Please update your address.</a>
<a href="/contact.do">Please update your contact information.</a>

I accept that this might not be a solution in all cases and may have your UI designer spitting teeth.

我接受这可能不是所有情况下的解决方案,并且可能让您的UI设计师唾手可得。