使用 String.Format 避免资源(可本地化字符串)重复

发布于 2024-09-05 07:25:13 字数 1119 浏览 4 评论 0原文

我正在开发一个应用程序(.NET,但不相关),其中资源/字符串重复的可能性很大 - 大多数字符串都很简单,例如:

  • Volume: 33
  • Volume: 33 (dB)
  • Volume
  • 33 dB
  • Volume (dB)
  • 命令 - 音量:33 (dB),

其中 X、Y 和单位相同。

我应该为每个字符串定义一个新资源,还是最好使用 String.Format 来简化其中的一些资源,例如:

String.Format("{0}: {1}", Resource.Volume, 33)
String.Format("{0}: {1} {2}", Resource.Volume, 33, Resource.dB)
Resource.Volume
String.Format("{0} ({1})", 33, Resource.dB)
String.Format("{0} ({1})", Resource.Volume, Resource.dB)
String.Format("Command - {0}: {1} {2}", Resource.Volume, 33, Resource.dB)

我还会在资源中定义“{0}: {1}”等字符串格式,以便有将有可能对单词进行重新排序...

我会选择性地使用这种方法,而不是在整个应用程序中使用这种方法。

那么:

 String.Format("{0}: {1}", Volume, Resource.Muted_Volume) 
 // = Volume: Muted
 Resource.Muted_Volume
 String.Format("{0}: {1} (by user {2})", Volume, Resource.Muted_Volume, "xy") 
 // = Volume: Muted (by user xy)

像“User”与“User:”与“User - ”这样的简单情况怎么样?您能想到在任何文化中“:”会出现在不同的位置或者可能会使用不同的字符吗?

优点是在我的应用程序中将资源数量减少了 2-3 倍。

使用这种方式会不会有什么隐患呢?有人能给我举个例子(语言/文化)吗?

I'm working on a application (.NET, but not relevant) where there is large potential for resource/string duplication - most of these strings are simple like:

  • Volume: 33
  • Volume: 33 (dB)
  • Volume
  • 33 dB
  • Volume (dB)
  • Command - Volume: 33 (dB)

where X, Y and unit are the same.

Should I define a new resource for each of the string or is it preferable to use String.Format to simplify some of these, eg.:

String.Format("{0}: {1}", Resource.Volume, 33)
String.Format("{0}: {1} {2}", Resource.Volume, 33, Resource.dB)
Resource.Volume
String.Format("{0} ({1})", 33, Resource.dB)
String.Format("{0} ({1})", Resource.Volume, Resource.dB)
String.Format("Command - {0}: {1} {2}", Resource.Volume, 33, Resource.dB)

I would also define string formats like "{0}: {1}" in the resources so there would be a possibility of reordering words...

I would use this approach selectivly and not throughout the whole application..

And how about:

 String.Format("{0}: {1}", Volume, Resource.Muted_Volume) 
 // = Volume: Muted
 Resource.Muted_Volume
 String.Format("{0}: {1} (by user {2})", Volume, Resource.Muted_Volume, "xy") 
 // = Volume: Muted (by user xy)

What about simple cases like "User" vs "User: " vs "User - "? Can you think of any culture where ':' would be in a different place or maybe a different character would be used?

The advantage is cutting the number of resource by the factor of 2-3 in my application.

Are there any hidden dangers of using this approach? Could someone give me an example (language/culture) where this would not work correctly?

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

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

发布评论

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

评论(2

金橙橙 2024-09-12 07:25:13

当您提出问题时,您自己已经确定了主要缺点:

有人能给我一个无法正常工作的示例(语言)吗?

您依赖于所有语言中单词的顺序都是相同的。无论如何,我都不是语言专家,但我曾经开发过翻译成其他语言(通常是法语和德语)的应用程序,并且构建字符串时的词序始终是一个问题。

对于简单的短语(例如您给出的短语),您可能没问题,但对于任何更复杂的短语,您都需要为每个短语拥有单独的资源。

You've identified the main drawback yourself when you ask the question:

Could someone give me an example (language) where this would not work correctly?

You are relying on the order of the words being the same in all languages. I'm not a language expert by any means, but I have worked on applications that were translated into other languages (typically French and German) and word order when building up strings was always an issue.

You'll probably be OK for simple phrases - the ones you give for example, but anything more complicated and you'll need to have separate resources for each one.

旧城空念 2024-09-12 07:25:13

除了复数/单数问题(不是特定于这个问题)之外,我认为这对于大多数欧洲语言来说是安全的,因为它不使用自由文本,而是主要使用数量和名称:例如命令:命令的名称。量:数量。
您需要处理词序,拥有由资源定义的模板,但这仍然只是“格式类型”的一种资源 - 模板中的单词仍然是参数化的。

即便如此,我会使用一种策略来实现它,在该策略中默认使用组合方法,但可以选择覆盖特定语言,以便可以在默认组合规则失效的情况下显式指定字符串。

Aside from issues with plural/singular, (which are not specific to this problem), I think this is safe for the majority of european languages since it is not using free text but mostly quantity and appelations: e.g. Command: the name of a command. Volume: quantity.
You'll need to deal with word order, having a the template defined by the resource, but this is still only one resource for "format type" - the words in the template are still parameterized.

Even so, I would implement it using a strategy where you use the composition approach by default, but have the option to override for specific languages so the strings can be specified explicitly where the default composition rules break down.

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