将短语翻译与 String.Format 集成

发布于 08-17 14:57 字数 507 浏览 3 评论 0原文

我有一个英文短语,如下所示:“我有 {0} 只狗和 {1} 只猫”。在代码中,我用 String.Format 提供数据:

String.Format("I have {0} dogs and {1} cats", 1, 2)

所以输出是这样的:“我有 1 只狗和 2 只猫”。

我要解决的问题是“我有 {0} 只狗和 {1} 只猫”这句话需要翻译成其他语言。

在此西班牙语翻译示例中,英语短语“I have {0} dogs and {1} cats”和翻译短语“Tengo {0} perros y gatos {1}”存储在数据库中。

如果用户要将“Tengo {0} perros y gatos {1}”更改为“Tengo {0} perros y gatos {3}”,则当我调用 String.Format("Tengo {0 } perros y gatos {3}", 1, 2)。

现在我正在捕获格式异常,感觉不对。我正在寻找更好的解决方案的想法。

I have a phrase in English that looks like this: "I have {0} dogs and {1} cats". In code, I provide the data with a String.Format:

String.Format("I have {0} dogs and {1} cats", 1, 2)

So the output is this: "I have 1 dogs and 2 cats".

The problem that I'm trying to solve is the phrase "I have {0} dogs and {1} cats" needs to be translated in other languages.

In this Spanish translation example, the English phrase "I have {0} dogs and {1} cats" and the translated phrase "Tengo {0} perros y gatos {1}" are stored in a database.

If the user were to change "Tengo {0} perros y gatos {1}" to "Tengo {0} perros y gatos {3}", a System.FormatException will be thrown when I call String.Format("Tengo {0} perros y gatos {3}", 1, 2).

For now I'm trapping the format exception and it feels wrong. I'm looking for ideas on a better solution.

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

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

发布评论

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

评论(6

壹場煙雨2024-08-24 14:57:39

在保存到数据库之前,为什么不看看 String.Format 是否抛出异常?如果是这样——不要让用户保存。

只需一个简单的想法就可以解决问题......

Before saving to the database, why not see if you String.Format throws? If so -- do not let the user save.

Just a simple idea that may solve the problem...

2024-08-24 14:57:39

我会将翻译后的短语与存储的原始短语进行比较,以检查占位符的数量和类型是否相同。

解析原始英语短语,并检查占位符,例如使用正则表达式:/\{\d+\}/(如果您仅使用占位符且不使用格式)。检查匹配项,并在编辑时将它们与翻译后的字符串进行比较。这确保了应用程序在出现故障时用户仍然可以纠正它。

I would compare the translated phrase with the stored original phrase, to check if the number and types of placeholders are equal.

Parse the original English phrase, and check for placeholders, e.g. using a regex: /\{\d+\}/ (if you only use placeholders and no formatting). Check the matches, and compare them at edit-time to the translated string. This makes sure the application fails at a time the user can still correct it.

一枫情书2024-08-24 14:57:39

好吧,我认为您的用户不应该能够更改您的翻译字符串,除非他们知道自己在做什么。通常,应告知翻译人员,{0}、{1} 代表可替换参数,不应更改。恕我直言,除了翻译人员之外的所有其他人都不应访问这些字符串。

Well, I think your users shouldn't be able to change your translation string unless they know what they're doing. Usually, translators should be told that the {0}, {1} represents replacable parameters and should not be altered. All other people besides translators should have no access to these strings, IMHO.

七分※倦醒2024-08-24 14:57:39

您可以做两件事:

  1. 检查翻译者保存新翻译时是否不会发生这种情况,即您可以搜索{XX}并查看最大数量是否与原始翻译不同。如果是这样,请不要保存新的。
  2. 显示它时,如果出现类似错误,请捕获异常并回退到您知道正确的默认语言。

There are two things you could do :

  1. Check that this does not happen when the translator saves the new translation, ie you could search for {XX} and see if the maximum number is different from the original translation. If so, don't save the new one.
  2. When displaying it, if there is an error like that, catch the exception and fall back to the default language that you know is correct.
萌逼全场2024-08-24 14:57:39

您肯定需要一个验证函数来以某种方式检查编辑后的字符串。即使在不使用数字位置指示器的不同方案中,仍然有可能出现拼写错误。

就 UI 而言,在他们完成编辑后,您可以向他们展示实际填充参数化值的示例翻译。这将达到双重目的:验证其格式字符串并让他们对其在上下文中的外观进行视觉双重检查。

You definitely need a validation function that can check the edited string in some way. Even in a different scheme that didn't use number position indicators there would still be opportunity for typos.

In terms of UI, after they finish their edit, you could show them a sample translation that actually fills in the parameterized values. This would serve the double purpose of validating their formatting string and giving them a visual double-check of how it will look in context.

再浓的妆也掩不了殇2024-08-24 14:57:39

每晚的单元测试可以发现这个问题。

另一个想法:

为该用户提供一个检查工具,用户可以在进行更改后运行该工具。

Nightly unit tests can catch this problem.

Another idea:

Give this user a checker tool which the user can run after making changes.

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