0

I have integrated friendly_id gem and globalize and it works just fine if I have translation for slug in specific locale.

But what I want to achieve is to use default translation for slug in case it is missing in translation table for some locale.

So let say I have a master locale MA where the slug is filled and I have a EN locale, where it is missing. If I will go to EN version, I want to see MA version of the slug.

Is there a way how to do this?

Thanks, Miroslav

Miroslav
  • 158
  • 2
  • 22

2 Answers2

1

I figured this out finally by writing an application helper method (other solutions did not work for me). Hope it will help someone. If the translations do not exist for a product, it will use master translation (which is created by default on product creation) otherwise use current locale version.

# application_helper.rb

def product_url(product)
    if product.translations.pluck(:locale).include?(I18n.locale.to_s)
      admin_translations_product_path(product)
    else
      I18n.with_locale(:ma) { admin_translations_product_path(product) }
    end
end

# index.html.erb
<%= link_to "#{t :button_admin_edit} #{locale.to_s.upcase}", product_url(product) %>
Miroslav
  • 158
  • 2
  • 22
0

I believe this is handled for you if you use this add-on gem for friendlyid and globalization: https://github.com/norman/friendly_id-globalize/blob/master/README.md

It handles the default locale. I'll just refer you to the docs but that will hopefully do it.

Tom Fast
  • 1,138
  • 9
  • 15
  • Thanks Tom. I went through the documentation and tried many ways, but none of them worked for me. It works fine for loading default translations for texts if they do not exist in current language, but not for the product URLs. Finally I did it using helper method and it works just fine (but maybe there is more elegant solution). – Miroslav Sep 06 '15 at 18:34