Module: LocalizerRails

Extended by:
LocalizerRails
Included in:
LocalizerRails
Defined in:
lib/localizer_rails.rb,
lib/localizer_rails/engine.rb,
lib/localizer_rails/version.rb,
lib/localizer_rails/set_locale.rb,
app/helpers/localizer_rails/localizer_rails_helper.rb

Defined Under Namespace

Modules: LocalizerRailsHelper, SetLocale Classes: Engine, LocalizerRailsGenerator

Constant Summary collapse

Conf =
CONFIGURATION DEFAULT SETTINGS & USER PREFS
OpenStruct.new :merge_lang_countries_tables => true,
:all_available_locales       => true,
:active_locales              => [ I18n.default_locale ],
:country_downcase            => false,
:display_local_language      => false,
:use_bootstrap               => false,
:cookie_expires              => nil,
:store_in_session            => false
VERSION =
"0.1.2"
PATH_TO_LANG =
%w[ config localizer_rails lang_countries.yml ]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Allows LocalizerRails.method instead of LocalizerRails::Conf.method



23
24
25
# File 'lib/localizer_rails.rb', line 23

def method_missing(meth, *args, &block)
  Conf.respond_to?(meth) ? Conf.send(meth, *args, &block) : super
end

Instance Method Details

#active_localesObject

DEFINE WHICH LOCALES SHOULD BE AVAILABLE TO THE APPLICATION:



30
31
32
33
34
35
36
37
38
# File 'lib/localizer_rails.rb', line 30

def active_locales
  @active_locales_temp ||= begin
    if Conf.all_available_locales.blank?
      I18n.available_locales & Conf.active_locales
    else
      I18n.available_locales
    end
  end
end

#active_locales_mapObject

BUILD LOCALE MAP:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/localizer_rails.rb', line 66

def active_locales_map
  @my_locales_ok ||= begin
                        custom_file_path = Rails.root.join(*PATH_TO_LANG)
                        loc_map          = if File.exist?(custom_file_path)
                                             if Conf.merge_lang_countries_tables
                                               lang_file(Engine).merge(lang_file(Rails))
                                             else
                                               lang_file(Rails)
                                             end
                                           else
                                             lang_file(Engine)
                                           end

                        my_locales_temp  = {}

                        if Conf.all_available_locales.blank?
                          active_locales.each do |k|
                            next unless I18n.available_locales.include?(k) && loc_map.key?(k)
                            my_locales_temp[k] = loc_map[k]
                          end

                        else
                          I18n.available_locales.each do |k|
                            next unless loc_map.key?(k)
                            my_locales_temp[k] = loc_map[k]
                          end
                        end
                        my_locales_temp
                      end
end

#set_locale(req) ⇒ Object

FIND BEST-MATCH LOCALE:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/localizer_rails.rb', line 41

def set_locale(req)
  I18n.locale = req.params[:locale] ||
                req.send(:cookies)['locale'] ||
                (req.current_user.locale if (!!(defined? req.current_user.locale))) ||
                req.session[:locale] ||
                accepted_locales(req.env['HTTP_ACCEPT_LANGUAGE']) ||
                I18n.default_locale
  ## FALLBACK IF INVALID DATA IN COOKIES, ...
  I18n.locale = I18n.default_locale unless active_locales.include?(I18n.locale)
  ## SET COOKIE:
  req.send(:cookies)[:locale]                           = { :value   => I18n.locale,
                                                            :expires => Conf.cookie_expires
                                                          }
  ## SET SESSION:
  req.send(:session)[:locale]                           = I18n.locale unless Conf.store_in_session.blank?
  ## LOCALIZE LINKS
  Rails.application.routes.default_url_options[:locale] = I18n.locale

  ## RETURN LOCALE:
  I18n.locale
end