norman / friendly_id-globalize

Globalize support for FriendlyId
MIT License
66 stars 32 forks source link

Redirect same page to different language (link_to) #7

Open vincentcordel opened 9 years ago

vincentcordel commented 9 years ago

Hi there,

I've been scratching my head for the last few hours, looking for an answer but I can't find it anywhere.

My gem file:

# Use globalize for translating models
gem "globalize", github: "ncri/globalize" # for Rails 4.2
gem 'globalize-accessors', '~> 0.1.5'

# Use friendly_id for slugs
gem 'friendly_id', '~> 5.1.0'
gem 'friendly_id-globalize', '~> 1.0.0.alpha1'

Here's the situation:

So I assume friendly_id and globalize are properly configured.

However my problem is that I can't make a language switcher work using:

  <% if I18n.locale != :en %>
    <li>
    <%= link_to t('menu.languages.short_en'), url_for(locale: 'en') %>
    </li>
<% end %>

The route becomes en/pages/slug-fr (i.e. the language changes but not the slug).

I have activated config.use :finders in the initializer.

My page model:

translates :title, :slug, :blurb, :content, :seo_title, :seo_description, :seo_keywords
  globalize_accessors :locales => [:en, :fr], :attributes => [:title, :slug, :blurb, :content, :seo_title, :seo_description, :seo_keywords]

    extend FriendlyId
      friendly_id :slug, :use => :globalize
      validates :slug, presence: true, uniqueness: { case_sensitive: false }

So what do I need to do to have the proper path on my language switcher? Ideally, I'd like this to work with any models, not just the Page model.

Thanks!

thisiscab commented 9 years ago

Hey,

I am having the same issue. Have you found a work around?

vincentcordel commented 9 years ago

I found a workaround. Not sure if it's the most efficient but it works.

First create a partial on your layout:

<%= yield :change_lang %>

Then include the partial for each view page (model is story)

<% content_for :change_lang do %>
  <ul class="languages">
    <li><%= link_to t('menu.languages.text_fr'), url_for(id: @story.slug_fr, locale: 'fr') %></li>
    <li><%= link_to t('menu.languages.text_en'), url_for(id: @story.slug_en, locale: 'en') %></li>
  </ul>
<% end %>

Hope this helps,

benbonnet commented 9 years ago

As explained in the readme, if a slug in a specific locale is empty, shouldn't it fallback to the default locale and still find the record ?

If I have a similar use of @story.slug_en, and that the record have not been saved yet in its english locale version, it does returns nil so far and breaks with an error (@story.slug_en would be nil)

Is there is something to add in the conf ? With plain id's (not using the slugs), it all works like charm; able to create a record with the default locale, then navigate to the others one to fill it all

thanks if any tips

descovi commented 8 years ago

Hi, i had same problem and i resolved in this way

- link = I18n.with_locale(:it){page_path(@page, locale: 'it')}
= link_to 'Italiano', link
- link = I18n.with_locale(:en){page_path(@page, locale: 'en')}
= link_to 'English', link
afmp94 commented 7 years ago

I've a generic answer. please let me know if it works for you.

in your application_helper.rb

if you have a namespace you can set up in :shop, if not just url_for(obj)

def languages(obj=nil)
    content_for(:switch_locale) do
      I18n.available_locales.each do |locale|
        I18n.with_locale(locale) do
          concat(
              if obj
                content_tag(:li, (link_to locale, url_for([:shop, obj]) ))
              else
                content_tag(:li, (link_to locale, url_for(locale: locale.to_s) ))
              end          
             )
        end
      end
  end
end

in your views - languages @category or simply -languages

in application.html.(erb/haml.slim) or wherever you want to render the translations. =yield(:switch_locale)