Question

Ok, I am slightly confused here - and there might be a simple solutions to this problem - but I haven't figured it out over the past two hours, so I thought I might ask.

I have a blog entires in the DB which are written in two different languages (AT & EN) - the locales defined in I18n are at (german) & en (english).

Each time I create a blog entry I save the locale together with the entry. Simplified example:

id  |   title      |  content  |  locale
-----------------------------------------
1   |  First Blog  |  Hey, ..  |  en
2   |  Erster Blog |  Hallo, . |  at

etc.

So, in general I keep those two languages separated on the site - as if those were two different sites, but accessing a bloggers profile a list of most recent blogs are shown. Some of the bloggers post in German as well as English.

If the visitor is now at www.mysite.com/en - and clicks on a reference on a german blog it will error out.

In the controller I defined the following to make sure the same content cannot be accessed via two URL's:

@blog = Blog.friendly.find(params[:id], :conditions => ["language_id = ?", I18n.locale] )

So this is to make sure SEO wise I don't get penalised. (www.mysite.com/en/first-blog == www.mysite.com/at/first-blog). Currently I catch the exception in the model:

around_filter :catch_not_found

.... 

 def catch_not_found
  yield
  rescue ActiveRecord::RecordNotFound
  redirect_to blogs_path, :flash => { :error => "Sorry, the Blog isn't available in your language." }
end

The question is - How can I pass the locale in link_to, so that the user gets redirected to blog in the correct locale?

Thanks

Was it helpful?

Solution

There are some another approaches to store translated fields for your models.

First is how Globalize gem works, when you have another polymorphic model for storing translated fields

Another are using postgresql's hstore for storing translations as dictionary en : 'First Blog', at : 'Erster Blog'. There are also some gems that help to integrate this approach.

In both approaches you have same post_id for all locales, so it solves problem with links/rederects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top