Skip to content
effkay edited this page Oct 24, 2014 · 1 revision

Adding additional content languages

If you want to translate your pages into another language, generate a new translation migration:

# translate your pages into french
rails g qbrick:translations:add fr

Or

# translate your pages into swiss german
rails g qbrick:translations:add de-CH

This creates a new migration file inside db/migrate of your app. Run the migration as you normally do:

rake db:migrate

Finally, add the new translation locale to your available_locales inside your apps application.rb:

config.available_locales = [:en, :fr]

Or

config.available_locales = [:en, 'de-CH']

Adding a language switch

Add scope around routes:

scope "(:locale)", locale: /de|en|fr/ do
  root 'qbrick/pages#show'
end

Set the locale in the ApplicationController in a before_action and set default url options:

before_action :set_locale

def set_locale
  if I18n.locale_available? params[:locale]
    I18n.locale = params[:locale]
  else
    I18n.locale = I18n.default_locale
  end
end

def default_url_options(options = {})
  { locale: I18n.locale }
end

Add method to ApplicationHelper which redirects to homepage when current page is not translated. Make sure to have the homepage translated in every available language.

def localized_url(url, target_locale)
  page = Qbrick::Page.find_by_url("#{I18n.locale}/#{url}")
  I18n.with_locale target_locale do
    translated_url = page.try :url
    if translated_url.present?
      "/#{translated_url}"
    else
      root_path(locale: target_locale)
    end
  end
end

def language_link(url, locale)
  localized_url(params[:url], locale)
end

Add language switch to navigation:

SimpleNavigation::Configuration.run do |navigation|
  I18n.available_locales.each do |locale|
    primary.item locale, locale.to_s.upcase, language_link(params[:url], locale), highlights_on: Proc.new { I18n.locale == locale }
  end
end

Make sure to render only pages which are translated and published by using published and translated scope, so pages without translation and which are not published will not be displayed in the navigation. Here is an example of a possible navigation:

SimpleNavigation::Configuration.run do |navigation|
  navigation.items do |primary|
    primary.dom_class = 'right'
    primary.selected_class = 'active'
    Qbrick::Page.find_by(slug_de: 'meta-navigation').children.published.translated.each do |page|
      primary.item page.id, page.title, page.link, class: 'contact icon'
    end

    primary.item '', 'Sprache', '#', class: 'language icon has-dropdown'do |language|
      I18n.available_locales.each do |locale|
        language.dom_class = 'dropdown'
        language.item locale, language_text(locale), language_link(params[:url], locale), highlights_on: Proc.new { I18n.locale == locale }, class: "icon lang-#{locale}"
      end
    end
  end
end