Thomas
Thomas

Reputation: 850

How to control RoR I18n texts stored in database?

I'm using the I18n Gem from Sven Fuchs in my Ruby on Rails 3.2 Application and while the gem works great I came across a situation, which I don't know the solution to:

I have a seed file, which contains the basic translation for my MVC's and is seeded, when I install my application on a new machine. The problem is that when one of these translations changes, I have to go to my seed file, edit it, delete in the database and reseed it. Which is problem not the best way to do this.

Furthermore, my application can create complete MVC's on the fly, which of course need translations as well. These translations get only stored in the database. But it would be nice to store them in a real file, keep them under version control and import or export them if I need to.

So, basically what I'm looking for, is an intelligent connection between the translations in my database and the ones in my files. So I can populate one from the other or vica verca and keep them in sync.

And also I looked at solutions like Globalize3 or localeapp, but they don't seem to fit.

Summarized, what I have is:

What I want:

I'm sure I can't be the only one who needs this...

Thanks in regards!

Upvotes: 0

Views: 194

Answers (1)

Claudio Shigueo Watanabe
Claudio Shigueo Watanabe

Reputation: 1003

Here is how I solved a problem closer to the question asked:

task :task_name => [:environment] do  

  file = "db/file_name.txt"

  counter = 0

  CSV.foreach(file, :headers => true, :col_sep => "^", :quote_char => "~") do |row|
    identifier = row[0].to_i
    model_name = ModelName.find_or_create_by_identifier(identifier)
    I18n.locale = row[1]
    model_name.name = row[3]
    model_name.save!
  end
end

Note that identifier needs to be a unique identifier that doesn't change and exists in the file and in the database. In this example, the columns are separated by "^" and quores are "~"

As @tigrish said in the comments, it is not a good idea to insert in the file and in the database, so it is important to restrict this.

These links may also help:

As the question is a little old, I hope it can help somebody else.

Upvotes: 0

Related Questions