单独 .pas 文件中的字符串
这可能不是这个问题的正确位置,如果不合适请随意移动它。我标记为 Delphi/Pascal 因为这是我在 atm 中工作的,但这可能适用于我猜的所有编程。
不管怎样,我正在做一些代码清理,并考虑将程序中的所有字符串移动到一个单独的 .pas 文件中。这样做有什么优点和缺点吗?这还值得做吗?
澄清一下:我的意思是我将创建一个单独的文件 Strings.pas,在其中我将创建所有文本字符串变量。
当前
代码
Messages.Add('The voucher was NOT sent to ' + sName+
' because the application is in TEST MODE.');
Messages.Add('Voucher Saved to ' + sFullPath);
Messages.Add('----------------------------------------------------------');
新代码类似于:
Messages.Add(sMsgText1 + '' + sName + '' + sMsgText2 + '' + sFullPath)
Strings.pas 文件将保存所有字符串数据。希望这更有意义
This may not be the correct place for this question, if not feel free to move it. I tagged as Delphi/Pascal because it's what I am working in atm, but this could apply to all programming I guess.
Anyway I am doing some code cleanup and thinking of moving all the strings in my program to a separate single .pas file. Are there any pros and cons to doing this? Is it even worth doing?
To clarify: I mean that I will be creating a separate file, Strings.pas in it I will make all my text string variables.
Ex
Current Code
Messages.Add('The voucher was NOT sent to ' + sName+
' because the application is in TEST MODE.');
Messages.Add('Voucher Saved to ' + sFullPath);
Messages.Add('----------------------------------------------------------');
New Code would be something like:
Messages.Add(sMsgText1 + '' + sName + '' + sMsgText2 + '' + sFullPath)
The Strings.pas file would hold all the string data. Hope that makes better sense
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将字符串移动到单独的文件中是个好主意!它将它们保持在一起,并让您在需要时轻松更改它们。你的问题并不是说你希望能够翻译它们,但集中化将有助于实现这一点。
但是,像这样的代码
并不比这样的代码更好:
您已经将一个混乱但可读的函数调用变成了一个混乱且不可读的函数调用。使用旧代码(上面的第二个片段),您可以阅读代码并大致了解消息将要说什么,因为其中很多内容都是文本形式的。使用新代码,您就不能这样做。
其次,移动字符串的原因是为了将相关项目放在一起并更容易更改它们。如果您想更改上述消息,而不是说“路径‘bar’中的文件‘foo’...”,而是改为“文件 bar\foo 是...”,该怎么办?你不能:消息的构建方式仍然是固定的并且分散在整个代码中。如果您想以相同的方式更改多条消息的格式,则需要更改许多单独的位置。
如果您的目标是翻译消息,这将是一个更大的问题,因为翻译通常需要重新措辞消息而不仅仅是翻译组件。 (例如,您需要更改消息中包含的子项目的顺序 - 您不能只是假设每种语言都是顺序替换中的短语对短语。)
进一步重构
我建议进行更积极的重构您的消息代码。当您建议将邮件移至单独的文件时,您绝对是在正确的轨道上。但不要只移动字符串:还要移动函数。不要在代码中分散大量
Messages.Add('...')
,而是找到您创建的消息的公共子集。很多都会非常相似。创建一系列可以调用的函数,以便使用单个函数实现所有类似的消息,并且如果您需要更改它们的措辞,可以在单个位置完成。例如,而不是:
有一个函数:
您将得到:
ItemNotFount(item, 它会导致
更清晰的代码,这对我来说听起来不错:)
Moving your strings to a separate file is a good idea! It keeps them together and will let you easily change them if required. Your question doesn't say you want to be able to translate them, but centralizing will help that to.
But, code like:
is not better than code like:
You've turned a messy but readable function call into a messy and un-readable function call. With the old code (the second snippet just above), you can read the code and see roughly what the message is going to say, because a lot of it is there in text. With the new code, you can't.
Second, the reason for moving the strings to to keep related items together and make it easier to change them. What if you want to change the above message so that instead of saying "The file 'foo' in path 'bar'..." it is phrased "The file bar\foo is..."? You can't: the way the messages are built is still fixed and scattered throughout your code. If you want to change several messages to be formatted the same way, you will need to change lots of individual places.
This will be even more of a problem if your goal is to translate your messages, since often translation requires rephrasing a message not just translating the components. (You need to change the order of subitems included in your messages, for example - you can't just assume each language is a phrase-for-phrase in order substitution.)
Refactor one step further
I'd suggest instead a more aggressive refactoring of your message code. You're definitely on the right track when you suggest moving your messages to a separate file. But don't just move the strings: move the functions as well. Instead of a large number of
Messages.Add('...')
scattered through your code, find the common subset of messages you create. Many will be very similar. Create a family of functions you can call, so that all similar messages are implemented with a single function, and if you need to change the phrasing for them, you can do it in a single spot.For example, instead of:
have a single function:
You get:
ItemNotFount(item, path)
, which leads toSounds good to me :)
我认为将所有字符串常量移动到一个单元很有意义。它使更改文本变得更加容易,特别是如果您想翻译成其他(人类)语言。
但是你为什么不做我通常做的事情而不是字符串,即使用resourcestring。这样,其他人就可以使用资源编辑器更改您的字符串,而无需重新编译。
但这样的字符串可能更好地完成为:
这样做的优点是在某些语言中,此类替换的位置和顺序是不同的。这样,译者就可以将论点放在任何需要的地方。当然,应用程序必须使用 Format() 来处理字符串:
I think it makes a lot of sense to move all string constants to a single unit. It makes changing the texts a lot easier, especially if you want to translate to other (human) languages.
But instead of strings, why don't you do what I usually do, i.e. use resourcestring. That way, your strings can be changed by someone else with a resource editor, without recompilation.
But such a string is probably better done as:
The advantage of that is that in some languages, the placement and order of such substitutions is different. That way, a translator can place the arguments wherever it is necessary. Of course the application must then use Format() to handle the string:
如果您想将 UI 翻译成不同的语言,那么将所有文本放在一个文件中,或者可能是多个专门用于声明字符串常量的文件中,您可能会受益匪浅。
但是,如果您在没有如此强烈的动机的情况下进行此更改,那么您的代码可能会变得难以阅读。
一般来说,你必须问这样一个重大重构的好处是什么,如果它们不是不言而喻的,那么你很可能只是为了改变而改变。
If you are wanting to translate the UI into different languages then you may benefit from having all your text in a single file, or perhaps a number of files dedicated to declaring string constants.
However, if you do this change without such a strong motivation, then you may just make your code hard to read.
Generally you have to ask what the benefits of such a major refactoring are, and if they are not self-evident, then you may well be changing things just for the sake of change.
如果您想翻译您的应用程序,请考虑使用 Gnu GetText for delphi,也称为 dxGetText。这比将字符串放入单独的 .pas 文件更好,因为它允许您无需任何特殊工具、任何重新编译即可启用翻译,甚至无需最终用户进行。
If you want to translate your app, consider using Gnu GetText for delphi also known as dxGetText. This is better than putting your strings into a separate .pas file because it allows you to enable translation without any special tools, any recompilation, by even end users.
嗯,这里的共识似乎倾向于赞成的一方,我完全同意这一点。错误地过度使用字符串文字可能会导致您的源代码变成字符串类型。
我仍然缺少的一项好处是字符串的可重用性。尽管这不是运送到另一个单元的直接好处,但它是通过将字符串文字移动到常量而获得的。
但一个相当重要的缺点是创建这个单独的源文件需要时间。您可能会考虑在下一个项目中实施这个良好的编程实践。这完全取决于您是否有足够的时间(例如爱好项目)或截止日期,是否有下一个项目(例如学生),或者只是想做一些练习。就像 David H 的答案和评论一样,就像您所做的所有决定一样做,你必须权衡好处。
除了可以提供一些自动帮助的各种奇特的重构工具之外,要认识到仅将字符串文字移动到另一个单元并不能完成工作。就像 Rudy 和 David M 已经回答了,您必须部分重写源代码。此外,找到可读、简短且适用的常量名称确实需要时间。正如许多评论已经指出的那样,控制拼写一致性很重要,我认为同样的论点也适用于替换常量本身。
关于翻译答案,无论是否适用于 OP,将所有源字符串移动到单独的单元只是翻译解决方案的一部分。您还必须注意设计器字符串(即标题)和 GUI 兼容性:较长的翻译仍然必须适合您的标签。
如果您有奢侈或需要:那就去吧。但我会把它带到下一个项目中。
Well, the consensus here seems to tend towards the pro side, and I agree with that completely. Wrong overuse of string literals could lead to your source getting stringly typed.
One benefit I am still missing is reusability of strings. Although that is not a direct benefit from the shipping to another unit, it ís from moving the string literals to constants.
But a rather significant drawback is the required time to create this separate source file. You might consider to implement this good programming practice in the next project. It entirely depends on whether you have sufficient time (e.g. hobby project) or a deadline, on having a next project or not (e.g. student), or on just wanting to do some practice. Like David H answers and comments, as with all decisions you make, you have to weight the benefits.
Seen apart from all kinds of fancy refactoring tools that could provide in some automatic assistance, realize that moving the string literals to another unit alone does not get the job done. Like Rudy and David M already answered, you partly have to rewrite your source. Also, finding readable, short and applicable constant names dóes take time. As many comments already stated that having control over spelling an consistency is important, I like to think that same argument goes for the replacing constants itself as well.
And about the translation answers, whether being applicable to OP or not, moving all your source strings to a separate unit is only part of a translation solution. You also have to take care for designer strings (i.e. captions), and GUI compatibility: a longer translation still has to fit in your label.
If you have the luxury or the need to: go for it. But I would take this to the next project.
我确实在旧版本的框架中将所有文字与资源字符串分组。我从中回来,因为在框架中,您可能不会使用框架中的所有字符串(例如,因为某些单元或单元组未使用,但扫描通用目录将显示翻译工具中的所有字符串)。
现在我再次将它们分配给各个单位。 较小问题(*)。
我最初开始将它们分组以避免重复,但回想起来,这是在“公共”目录上使用 dxgettext 的
I did group all literals with resourcestrings in an older version of our framework. I came back from that since in frameworks you might not use all strings in a framework (e.g. because some units or unit groups are not used, but scanning the common dirs will show up all strings in your translation tool).
Now I distribute them again over units. I originally started grouping them to avoid duplicates, but in retrospect that was the lesser problem
(*) using dxgettext on the "common" dirs.