francesc / rails-translate-routes

MIT License
147 stars 50 forks source link

Wrong generated links when models are translated #14

Closed K-Yo closed 12 years ago

K-Yo commented 12 years ago

links are not properly translated if we use both rails-translate-models and rails-translate-routes.

If I use a route including a custom parameter (not the id), then it is not taken into account when linking to another locale than the current one.

let's say I have a model Post, the associated controller PostController and the action show.

I add in my routes.rb the following line : match "post/:url" => "post#show"

I define my model with has_translations :url and the proper translations to have urls /post/english_message and /post/espanol_mensaje defined and replying to GET requests as expected.

then, if my locale is 'es', the helper <%= link_to url_for( :locale => 'es' ) %>, if I'm in an english page, will generate a link to "www.mysite.com/es/post/english_message/" instead of "www.mysite.com/es/espagnol_mensaje"

francesc commented 12 years ago

I've used a similar approach many times. I use a field called slug to use it as semantic url (your english_message/espagnol_mensaje) you have to make this field also translatable in rails-translate-models and when you do:

<%= link_to post_path(post.slug) %>

It will give the right language translated attribute. Remember it uses the current I18n.locale so you'll have to set it if you use url_for.