Translating current url into another language #1

Closed
dgilperez opened this Issue Jan 5, 2012 · 4 comments

Comments

Projects
None yet
3 participants
@dgilperez

Hi Francesc,

I'm looking into your plugin as replacement from Raul's translate_routes in my Rails3.1 app. I'm struggling with the following use case, maybe this can be done easily. If not, this might me an interesting feature to add :)

I want to have a language switcher in the header, used across the site, with the different available languages. I'd like the switcher to link or redirect to the page the user is at, localized with the selected language. I was looking for some helper or method to translate current_url. I ended up using some ugly javascript, but it good be great if I could use translate-routes.

Any ideas?

Thanks!

@francesc

This comment has been minimized.

Show comment Hide comment
@francesc

francesc Jan 5, 2012

Owner

I had this on my mind too but it's difficult to provide a good solution that covers real-world(tm) examples not just simple ones. I think the main problem would be the following case:

Let's image we have an app with products and 2 languages (en / es), with rails-translate-routes we set this url structure

/en/products
/en/products/1
/es/productos/1

In that case it should be simple to redirect the user from the english url to exactly the same place in the spanish one. But now imagine we also use a semantic id for each product in each language (a more real example for better SEO):

/en/products/red-trousers
/es/productos/pantalones-rojos

In that case it would be almost impossible for the gem to help you there, and it should be done by yourself in the app depending on your logic.

There's also another problem for real usage, based on the same example maybe we only display products that have an available translation and maybe we have red-trousers but in the spanish version the pantalones-rojos still doesn't exist because it hasn't been translated so we can't do the switch.

Owner

francesc commented Jan 5, 2012

I had this on my mind too but it's difficult to provide a good solution that covers real-world(tm) examples not just simple ones. I think the main problem would be the following case:

Let's image we have an app with products and 2 languages (en / es), with rails-translate-routes we set this url structure

/en/products
/en/products/1
/es/productos/1

In that case it should be simple to redirect the user from the english url to exactly the same place in the spanish one. But now imagine we also use a semantic id for each product in each language (a more real example for better SEO):

/en/products/red-trousers
/es/productos/pantalones-rojos

In that case it would be almost impossible for the gem to help you there, and it should be done by yourself in the app depending on your logic.

There's also another problem for real usage, based on the same example maybe we only display products that have an available translation and maybe we have red-trousers but in the spanish version the pantalones-rojos still doesn't exist because it hasn't been translated so we can't do the switch.

@dgilperez

This comment has been minimized.

Show comment Hide comment
@dgilperez

dgilperez Jan 5, 2012

Thanks Francesc!

Interesting problem in deed. In the meanwhile I found that link_to was translating routes this way:

link_to 'es', :locale => 'es'

So I used url_for to obtain my translated route:

url_for(:locale => 'es')

This problably won't work with the use case you describe, I've not tried ... but I will and let you know.

Thanks Francesc!

Interesting problem in deed. In the meanwhile I found that link_to was translating routes this way:

link_to 'es', :locale => 'es'

So I used url_for to obtain my translated route:

url_for(:locale => 'es')

This problably won't work with the use case you describe, I've not tried ... but I will and let you know.

@cawel

This comment has been minimized.

Show comment Hide comment
@cawel

cawel Jan 18, 2012

I also stumbled upon this issue of having a "language switcher" which links to the same current page, except translated in another language (and thus having its proper url). Indeed, if your current page is in English, using

url_for(:locale => 'es')

will generate a link with the Spanish locale, but as mentioned by @francesc, it will not correctly handle the slug (i.e. what is returned by the to_param method in a model).

However, I managed to have it working by coding a helper which does something like this:

def language_switcher_link
  other_locale = toggle_locale.to_s
  url = if params.keys.include? 'id'
          # TODO: introspection into params to figure out 
          # the object model from which to call to_param on, e.g.
          # new_id = @product.to_param
          url_for(:locale => other_locale, :id => new_id)
        else
          url_for(:locale => other_locale)
        end 
  toggle_locale
  url 
end

def toggle_locale
  I18n.locale = [:en, :es].reject{|l| l == I18n.locale}.pop
end 

cawel commented Jan 18, 2012

I also stumbled upon this issue of having a "language switcher" which links to the same current page, except translated in another language (and thus having its proper url). Indeed, if your current page is in English, using

url_for(:locale => 'es')

will generate a link with the Spanish locale, but as mentioned by @francesc, it will not correctly handle the slug (i.e. what is returned by the to_param method in a model).

However, I managed to have it working by coding a helper which does something like this:

def language_switcher_link
  other_locale = toggle_locale.to_s
  url = if params.keys.include? 'id'
          # TODO: introspection into params to figure out 
          # the object model from which to call to_param on, e.g.
          # new_id = @product.to_param
          url_for(:locale => other_locale, :id => new_id)
        else
          url_for(:locale => other_locale)
        end 
  toggle_locale
  url 
end

def toggle_locale
  I18n.locale = [:en, :es].reject{|l| l == I18n.locale}.pop
end 
@francesc

This comment has been minimized.

Show comment Hide comment
@francesc

francesc Jan 18, 2012

Owner

I'll add some info about this in the Readme for simple uses to help developers. Thanks!

Owner

francesc commented Jan 18, 2012

I'll add some info about this in the Readme for simple uses to help developers. Thanks!

@francesc francesc closed this Feb 24, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment