Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect same page to different language (link_to) #7

Open
vincentcordel opened this issue Jan 29, 2015 · 5 comments
Open

Redirect same page to different language (link_to) #7

vincentcordel opened this issue Jan 29, 2015 · 5 comments

Comments

@vincentcordel
Copy link

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:

  • I have two languages "en" and "fr"
  • 2 models : pages and pages_translations
  • pages has a slug column, pages_translations also has a slug column.
  • if I view the page -> en/pages/slug-en, it works.
  • if I view the page -> fr/pages-slug-fr, it works.

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!

  • Vincent
@thisiscab
Copy link

Hey,

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

@vincentcordel
Copy link
Author

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,

  • Vincent

@benbonnet
Copy link

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
Copy link

descovi commented Dec 5, 2015

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
Copy link

afmp94 commented Jan 22, 2017

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants