Module: Localyzed::LocalyzedController

Defined in:
lib/localyzed/controllers/action_controller_extension.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
# File 'lib/localyzed/controllers/action_controller_extension.rb', line 2

def self.included base
  base.send :before_filter, :set_locale
  # redirects_to localized path if path not loaclized and request not ajax or api
  base.send :before_filter, :redirect_to_localized_path_if
end

Instance Method Details

#redirect_to_localized_path_ifObject

if !request.xhr then non ajax request then ensure the path is localized (/^(en|fr)/.*/)



25
26
27
28
29
30
31
32
# File 'lib/localyzed/controllers/action_controller_extension.rb', line 25

def redirect_to_localized_path_if
  @translatable = true # used to display or hide the languages menu
  # Rails.env != test to make controller specs pass if locale is defined but path is not well written
  if ( !request.xhr? && ( ( !request.path.match(/(.*|)\/(en|fr)(\/|$)/) && Rails.env != 'test' ) || params[:locale] != I18n.locale.to_s) && request.method == 'GET')
    flash.keep
    redirect_to(url_for(params.merge(:locale => I18n.locale.to_s, :from_l => (params[:locale] if params[:locale]))), :status => :moved_permanently)
  end
end

#set_localeObject

sets the locale by checking user data then request param then session data then retrieving accept language data



13
14
15
16
17
18
19
20
21
22
# File 'lib/localyzed/controllers/action_controller_extension.rb', line 13

def set_locale
  cl = ( user_language || 'en')
  if params[:locale].nil? and user_signed_in? and cl and I18n.available_locales.include?(cl)
    locale = I18n.locale = cl
    session[:locale] = locale
  else
    current_user.try(:user_profile).try(:update_attributes,{:language => params[:locale]}) if params[:locale] && cl.try(:!=, params[:locale]) && current_user.try(:respond_to?, :user_profile)
    define_locale
  end
end

#url_optionsObject



8
9
10
# File 'lib/localyzed/controllers/action_controller_extension.rb', line 8

def url_options
  {:locale => I18n.locale.to_s}.merge(super)
end