简体   繁体   English

Rails常用字符串(用于通知和错误消息等)

[英]Rails commonly used strings (for notices and error messages and the like)

About a year ago I decided to make sure that every flash notice that had text that wasn't unique would get the text from a method in a module. 大约一年前,我决定确保每个具有非唯一文本的flash通知都会从模块中的方法获取文本。 My initial reason for doing this was to avoid typing out the same string over and over. 我这样做的最初原因是为了避免反复输入相同的字符串。 If I wanted to change the wording I could do so easily in one spot and the likelihood of typos from repeating the same thing over and over would be decreased. 如果我想改变措辞,我可以在一个地方轻松地做到这一点,并且错综复杂地反复重复相同事情的可能性会降低。

What I ended up with was this: 我最终得到的是:

module Messages
    def format_error_messages(errors)
        errors.map {|attribute, message| "Error: #{attribute.to_s.titleize} #{message}.<br />"}
    end

    def error_message_could_not_find(object_name)
        "Error: Unable to find the specified " + object_name + "!"
    end

    def error_message_could_not_create(object_name)
        "Error: Unable to create the " + object_name + "!"
    end

    def error_message_could_not_save(object_name)
        "Error: Unable to save " + object_name + " to database!"
    end

    def error_message_could_not_update(object_name)
        "Error: Unable to update " + object_name + "!"
    end

    def error_message_could_not_destory(object_name)
        "Error: Unable to destroy " + object_name + "!"
    end

    def notice_message_created(object_name)
        object_name.capitalize + " has been created!"
    end

    def notice_message_updated(object_name)
        object_name.capitalize + " has been updated!"
    end

    def notice_message_destroyed(object_name)
        object_name.capitalize + " has been deleted!"
    end
end

In my controllers when I'm setting up the flash I can do something like: 在我的控制器中,当我设置闪光灯时,我可以执行以下操作:

flash[:notice] = notice_message_created("post")

That way all successfully created objects generate similar messages. 这样所有成功创建的对象都会生成类似的消息

I'm redoing one of my projects now and am going over all of this and I have this nagging feeling that this is just not the best way to go about doing this. 我现在正在重做我的一个项目并且正在完成所有这些,我有这种唠叨的感觉,这不是最好的方法。 I mean, it works, and it's served me well so far, but I've never seen anyone else do this and I'm starting to think there's a reason. 我的意思是,它有效,到目前为止它对我有好处,但我从未见过其他人这样做,我开始认为这是有原因的。 Having to add a new method every time I need a new string seems almost silly. 每次我需要一个新字符串时都必须添加一个新方法,这看起来几乎是愚蠢的。 But then how else would I go about being able to insert contextual text into the string (like the name of the type of object we're dealing with)? 但那么我怎样才能将上下文文本插入到字符串中(就像我们正在处理的对象类型的名称一样)?

Is there a Rails community standard when it comes to dealing with common strings of text? 在处理常见的文本字符串时,是否存在Rails社区标准? Especially ones that need to have additional text inserted into them at runtime? 特别是那些需要在运行时插入其他文本的人?

I've been reading up on Rails' internationalization since it would be nice to have my projects localized into more than one language and I like the idea of having all my strings of text in nice YAML files which would kill two birds with one stone. 我一直在阅读Rails的国际化,因为将我的项目本地化为多种语言会很好,我喜欢将所有文本字符串放在漂亮的YAML文件中,这样可以一举两得。 But I don't see how I could do that and keep the same sort of functionality I've already got and need (unless I'm horribly mistaken and I don't need it because there's a better way). 但我不知道我怎么能做到这一点,并保持我已经拥有和需要的相同功能(除非我非常错误,我不需要它,因为有更好的方法)。

I'm pretty much open to any ideas, suggestions, or relevant reading on this issue. 我对这个问题的任何想法,建议或相关阅读都很开放。

You could use as an alternative approach the Rails Internationalization API . 您可以使用Rails Internationalization API作为替代方法。 Then you could fill your locale file with the strings you want to use. 然后,您可以使用要使用的字符串填充您的语言环境文件。 Any parameter you want to give can be included like in the example ins section 4.2 in the tutorial. 您可以包含您想要提供的任何参数,就像教程中的示例ins部分4.2一样。

I18n.backend.store_translations :en, :thanks => 'Thanks %{name}!'
I18n.translate :thanks, :name => 'Jeremy'
# => 'Thanks Jeremy!'

So do the following steps: 请执行以下步骤:

  1. Visit the file config/locales/en.yml 访问文件config/locales/en.yml

  2. Insert there your localization: 插入您的本地化:

    en: created: "#{name} has been created!" EN:已创建:“#{name}已创建!”

  3. Call instead of notice_message_created(object_name) the following: t(:created, object_name.capitalize) 调用而不是notice_message_created(object_name)t(:created, object_name.capitalize)

At the end, everything should work as before, you have get rid of a lot of simple methods, and you are able to internationalize your application by including the locale, and adding locale files to config/locales . 最后,一切都应该像以前一样工作,你已经摆脱了很多简单的方法,并且你可以通过包含语言环境和将语言环境文件添加到config/locales来使你的应用程序国际化。

I'd definitely go for I18N. 我肯定会选择I18N。 However, it requires some effort like finding all strings that needs to be translated, and mostly, coming up with a good structure for your translations file. 但是,它需要一些努力,比如查找需要翻译的所有字符串,并且大多数情况下,为您的翻译文件提供了一个良好的结构。

Rails I18N supports interpolation , so if you need to insert a number or a name in a string that won't be a problem. Rails I18N支持插值 ,因此如果您需要在字符串中插入一个不会有问题的数字或名称。

Or did i misunderstand what you mean by "same sort of functionality"? 或者我是否误解了“相同功能”的含义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM