如何验证翻译的数据?

时间:2012-04-13 17:38:59

标签: ruby-on-rails ruby ruby-on-rails-3 globalization globalize3

我正在使用Ruby on Rails(3.2.2),globalize3(0.2.0)和batch_translations(0.1.2)ruby-gems。我想验证globalize3处理的翻译数据以及它应该是。也就是说,例如,如果......

...在我的ROOT_RAILS/app/models/article.rb文件中:

class Article < ActiveRecord::Base
  translates :title, :content

  # This is needed to make the batch_translations to work.
  attr_accessible :translations_attributes
  accepts_nested_attributes_for :translations

  validates :title,
    :presence => true,
    :length   => ...
  validates :content,
    :presence => true,
    :length   => ...

  ...
end

...在我的ROOT_RAILS/app/views/articles/_form.html.erb文件中:

<%= form_for(@article) do |f| %>
    English translation:
    <%= f.text_field :title %>
    <%= f.text_field :content %>

    Italiano translation:
    <%= f.globalize_fields_for :it do |g| %>
      <%= g.text_field :title %>
      <%= f.text_field :content %>
    <% end %>
<% end %>

我想在提交表单时验证titlecontent值的数据,而当前I18n.locale不是I18n.default_locale。换句话说,为了在数据库中存储正确的翻译信息,我想在用户提交非默认英语语言环境语言的翻译数据时验证titlecontent属性。语言。

有可能吗?如果是这样,怎么样?

注意:我从this question获取了示例。

1 个答案:

答案 0 :(得分:0)

如果我正确理解你,你可以通过几种方式解决这个问题。

1)控制器级别测试,您可以在其中测试使用区域设置提交的表单是否仍然存在于正确的本地化内容表中。一种方法是这样的:

    it "persists localized content for the Italian locale" do
       Article.should_receive(:create).with(:locale => 'it', :title => 'title goes here', :content => 'this is content')
       post :create, :locale => 'it', :title => 'title goes here', :content => 'this is content'
    end

这基本上表明Article模型应该接收带有这些参数的create消息。我并不完全清楚batch_translations API的工作原理,因此您需要验证参数是否来自该样式表单。

此时,我认为您已经充分测试数据是否已正确保存到数据库中。还有什么可以测试Globalize gem的工作原理,你要避免它。尝试并测试边界。

相关问题