Skip to main content

[I18n] How do I translate database content ?

Databases • Asked by Nino
I have translated my static content with I18n, but I need to translate some strings/texts stored in my database.

For a more practical approach I have this scheme and I want to serve a localized `description`
create_table "locations", force: :cascade do |t|
    t.string "name"
    t.string "address"
    t.text "description"
  end

How do I do this ? I don't find (or understand) solutions with I18n's documentation.


You could go and try to JSONB for this. `t.text :description \ t.jsonb :description_translations, null: false, default: '{}'` 

On top of my head not sure about much data it can hold, but this is the approach I took the localise names for locations once.

Not sure, let's say the description attr is "blah blah blah" stored in a DB and you could do in your views:
<%= t @location.description.split.first %>
config/locales/en.yml
en:
blah: "anything"

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.