Factory Girl in Rails I have a FactoryGirl page that generates same data but in two different languages. The variable names are the same. How do I write it in the smartest way possible without having to write a huge bunch of code?

    FactoryGirl.define do
      factory :static_page do
        language ""

        trait :purchase_loan do
          page_name "Purchase Loan Wizard"
          titlebar_text "Home Loan Wizard -Compare and find the best home loan in Indonesia"
         language "en"
        end

        trait :refinance_loan do
          page_name "Purchase Loan Wizard"
          titlebar_text "Rumah Kita Wizard -Dah Bayar rumah Best di Indonesia"
          language "ms"
        end

      end
    end
You can use I18n directly in factory definitions

FactoryGirl.define do
  factory :static_page do
    language "en"
    trait :purchase_loan do
      page_name { I18n.t("static_page.page_name.purchase_loan", locale: language) }
      titlebar_text { I18.t("static_page.titlebar_text.purchase_loan", locale: language) }
    end

    trait :refinance_loan do
      page_name { I18n.t("static_page.page_name.refinance_loan", locale: language) }
      titlebar_text { I18.t("static_page.titlebar_text.refinance_loan", locale: language) }
    end
  end
end


This will allow you to share locales in views / application code and in your tests.

But you could go 1 step further and in your model store only locale keys, ie.

class StaticPage &lt; ActiveRecord::Base
  #attribues in db :pagename, :titlebar, :language
  def pagename_text
    I18n.t(pagename, locale: language)
  end

  def titlebar_text
    I18n.t(titlebar, locale: language)
  end
end


then you can use it this way:

purchase_loan_en = StaticPage.create(language: 'en', page_name: 'static_page.page_name.purchase_loan')


this will allow you to remove translations directly from factories.

Have a look on I18n Rails guide for how to use I18n. Maybe you don't even need to store language in database.