存储应用程序文本:内联与单独文件

时间:2022-03-06 16:58:52

I'm working on a Merb application using Haml as the templating language. As haml encourages moving logic out of the view and into helpers, I soon started thinking about removing the copy / text from the templates themselves. In the past I have either just left the text inline within the templates, or moved it into separate yaml files separated by controller. I've found that leaving text inline is more convenient, but uglier and involves using Ack a lot to find out where a particular piece of text is stored. Storing it in a yaml file is easier to search, but harder to come up with a sane naming schema for finding a particular piece of text in a given area.

我正在使用Haml作为模板语言的Merb应用程序。由于haml鼓励将逻辑移出视图并进入帮助者,我很快就开始考虑从模板本身中删除副本/文本。在过去,我要么只是将文本内联到模板中,要么将其移动到由控制器分隔的单独的yaml文件中。我发现将文本内联更方便,但更丑陋,并且需要大量使用Ack来找出存储特定文本的位置。将它存储在yaml文件中更容易搜索,但更难以找到一个合理的命名模式来查找给定区域中的特定文本。

I'm also curious about other approaches to this issue.

我也对这个问题的其他方法感到好奇。

1 个解决方案

#1


Storing texts separately is good if you want to globalize/internationalize your application. For rails there are a lot of fine plugins for doing this (Rails 2.3 even has an api for this), but these are usually working for merb too. There are various approaches to this, the two major approach is how you store the key:

如果要全局化/国际化应用程序,单独存储文本是很好的。对于rails来说,有很多很好的插件可以做到这一点(Rails 2.3甚至有一个api为此),但这些通常也适用于merb。有各种方法,两种主要方法是如何存储密钥:

  • The YAML-like interface you are using stores symbols I think, and it assigns the text to that symbol.
  • 您正在使用的类YAML接口存储我认为的符号,并将文本分配给该符号。

  • The gettext-style approach is that the text IS the symbol, and you can redefine it in a separate file if you want (but if you don't then you'll get back the same string as the key was).
  • gettext风格的方法是文本是符号,如果你愿意,你可以在一个单独的文件中重新定义它(但如果你不这样做,那么你将获得与密钥相同的字符串)。

Both approaches have their advantages and disadvantages. Apart from the key there are numerous storage facilities you might use, like YAML, or you can actually build a complete ActiveRecord/DataMapper/etc. based storage mechanism.

两种方法都有其优点和缺点。除了密钥之外,您可以使用许多存储设施,例如YAML,或者您实际上可以构建完整的ActiveRecord / DataMapper /等。基于存储机制。

You might check how rails 2.3 does with globalization, because their naming schema might be a good starting point for you. Or use namespaces to split the YAML files, so they will be easier to search. You might try the gettext-approach too, but it's actually not considered neither DRY nor safe (but it has a lot of tools that will create mapping files that contains all of the text your application has).

您可以查看rails 2.3如何处理全球化,因为它们的命名模式对您来说可能是一个很好的起点。或者使用命名空间来分割YAML文件,这样它们就更容易搜索。您也可以尝试使用gettext方法,但实际上并不认为它既不干也不安全(但它有很多工具可以创建包含应用程序所有文本的映射文件)。

#1


Storing texts separately is good if you want to globalize/internationalize your application. For rails there are a lot of fine plugins for doing this (Rails 2.3 even has an api for this), but these are usually working for merb too. There are various approaches to this, the two major approach is how you store the key:

如果要全局化/国际化应用程序,单独存储文本是很好的。对于rails来说,有很多很好的插件可以做到这一点(Rails 2.3甚至有一个api为此),但这些通常也适用于merb。有各种方法,两种主要方法是如何存储密钥:

  • The YAML-like interface you are using stores symbols I think, and it assigns the text to that symbol.
  • 您正在使用的类YAML接口存储我认为的符号,并将文本分配给该符号。

  • The gettext-style approach is that the text IS the symbol, and you can redefine it in a separate file if you want (but if you don't then you'll get back the same string as the key was).
  • gettext风格的方法是文本是符号,如果你愿意,你可以在一个单独的文件中重新定义它(但如果你不这样做,那么你将获得与密钥相同的字符串)。

Both approaches have their advantages and disadvantages. Apart from the key there are numerous storage facilities you might use, like YAML, or you can actually build a complete ActiveRecord/DataMapper/etc. based storage mechanism.

两种方法都有其优点和缺点。除了密钥之外,您可以使用许多存储设施,例如YAML,或者您实际上可以构建完整的ActiveRecord / DataMapper /等。基于存储机制。

You might check how rails 2.3 does with globalization, because their naming schema might be a good starting point for you. Or use namespaces to split the YAML files, so they will be easier to search. You might try the gettext-approach too, but it's actually not considered neither DRY nor safe (but it has a lot of tools that will create mapping files that contains all of the text your application has).

您可以查看rails 2.3如何处理全球化,因为它们的命名模式对您来说可能是一个很好的起点。或者使用命名空间来分割YAML文件,这样它们就更容易搜索。您也可以尝试使用gettext方法,但实际上并不认为它既不干也不安全(但它有很多工具可以创建包含应用程序所有文本的映射文件)。