.NET 格式化字符串 - 评论的良好做法?
在 .NET 出现之前,我们拥有自己的短语本地化系统,并且我们构建了一种将注释嵌套在格式字符串中的方式,例如:“{0:price}”。我发现随着岁月的流逝,我越来越怀念这个。
似乎没有一种方法可以在 .NET 中像这样在原位记录格式化字符串:
string.Format("{0//numerator} / {1//denominator} = {2//ratio}"
,somevar
,anothervar
,yetanothervar);
特别是这在本地化/短语学中很有用,其中插入点可以重新排序,而无需更改代码:
string.Format("Dividing {1//denominator} into {0//numerator} gives {2//ratio}"
,somevar
,anothervar
,yetanothervar);
任何人都有他们使用的技巧记录这些以避免在维护/本地化等方面重新排列术语时出现错误?
注释很重要的原因是,对于本地化和配置,通常,字符串不在带有变量的代码中 - 我将它们放在资源文件、app.config 和数据库中。
在实际示例中,子类化控件公开 PhraseID 属性(控件映射到从表单生成的 XML 文件中的 ID,并且表单控件即时翻译),因此子类化表单执行如下操作
// Handle the phrases without insertion points - this is in the base class
foreach (Control in this.Controls) {
IXLatable ixl = (IXLatable) Control;
ixl.Text = GetPhrase(ixl.PhraseID);
}
// in the individual form classes, they override the behavior for complex displays:
lnkPublish.Text = string.Format(GetPhrase(lnkPublish.PhraseID), filename, foldername, userid);
:包含默认和本地化的字符串,例如:
phraseid, language code, phrase
1,en,"{0//filename} published to {1//foldername} by {2//userid}"
1,pl,"{2//userid} ublishedpay ethay ilefay {0//filename} otay {1//foldername}"
如果在默认短语中提供注释,则译者(从未见过源代码)将索引弄错的可能性要小得多。对于非本地化的发言者来说,更容易解决翻译资源中的问题。
Before .NET, we had our own phrase localization system and we built in a way for comments to be nested in the formatting string like: "{0:price}". I'm finding that I miss this more and more as the years go by.
It doesn't appear that there is a way to document formatting strings in situ like this in .NET:
string.Format("{0//numerator} / {1//denominator} = {2//ratio}"
,somevar
,anothervar
,yetanothervar);
In particular this is useful in localization/phraseology where the insertion points get reordered, without changing the code:
string.Format("Dividing {1//denominator} into {0//numerator} gives {2//ratio}"
,somevar
,anothervar
,yetanothervar);
Anyone have any tricks they use to document these to avoid mistakes when terms get rearranged in maintenance/localization etc?
The reason that the commenting is important is that for localization and configuration, typically, the string is not in the code with the variables - I have had them in resource files, in the app.config and in databases.
In an actual example, a subclassed control exposes a PhraseID property (controls are mapped to IDs in an XML file generated from the form, and the form controls are translated on the fly), so the subclassed form does something like this:
// Handle the phrases without insertion points - this is in the base class
foreach (Control in this.Controls) {
IXLatable ixl = (IXLatable) Control;
ixl.Text = GetPhrase(ixl.PhraseID);
}
// in the individual form classes, they override the behavior for complex displays:
lnkPublish.Text = string.Format(GetPhrase(lnkPublish.PhraseID), filename, foldername, userid);
Where the dictionary contains default and localized string like:
phraseid, language code, phrase
1,en,"{0//filename} published to {1//foldername} by {2//userid}"
1,pl,"{2//userid} ublishedpay ethay ilefay {0//filename} otay {1//foldername}"
It is a lot less likely that a translator (who never sees the source code) will get the indexes wrong if they are provided with comments in the default phrase. And easier for a non-localized speaker to troubleshoot problems in a translated resource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以查看 Phil Haack 的 NamedFormat 扩展,它允许您使用类似的格式
You could look at Phil Haack's NamedFormat extension, which allows you to use formats like
在您的示例中,将变量命名为有意义的名称,将具有与注释相同的效果。
In your example naming the variables something meaningfull, will have the same affect as the comments.