georges
georges

Reputation: 282

Rails globalize get original untranslated record

I have a model

class Product < ApplicationRecord
  translates :title
end

When I create a record I then use globalize3 to create translations. My question is: how do I get the original untranslated record value of :title ?

Because when I do Product.last.title.translations I get all translation values, and when I do Product.last.title I only get the translation of the current I18n value.

Upvotes: 2

Views: 219

Answers (1)

max
max

Reputation: 102248

I'm assuming you're using globalize and not globalize3 which has not been updated in almost 10 years. If not you should switch.

Also the whole idea of "original untranslated value" is kind of nonsense when dealing with Globalize. All the values are stored on the translations table.

You can switch locales by using Globalize.with_locale:

class Product < ApplicationRecord
  translates :title
  def original_title
    Globalize.with_locale(I18n.default_locale) do
      title
    end
  end
end

This assumes that records are originally created in the default locale. If that is not the case you could query the translations table:

class Product < ApplicationRecord
  translates :title
  def original_title
    translations.first.title
  end
end

Upvotes: 2

Related Questions