Module: LocaleSelector

Defined in:
lib/locale_selector.rb

Overview

Usage

Controller

In your controllers or in the application controller

require 'locale_selector'

and use ControllerClassMethods#offer_locales:

offer_locales :en_UK, :en_ZA, :de, :ru

You can use different text domains in different parts of your application, if your application is large enough for multiple translation files. Please provide the differing domain in the single controllers e.g.

class JobsController < ApplicationController
  offer_locales :de, :ru, :domain => 'HR_terminology'
end

Persisting preferences

locale_selector provides helpers which you can use in connection with Rails hooks like this:

class ApplicationController
  before_filter :persist_locale_choice_cookie
  after_filter  :persist_locale_choice_in_db

  def persist_locale_choice_cookie
    compute_effective_locale do |requested_locale|
      save_cookie requested_locale
    end
  end

  def persist_locale_choice_in_db
    compute_effective_locale do |requested_locale|
      if current_user
        current_user.update_attribute 'preferred_locale', requested_locale
        current_user.reload
      end
    end
  end
  ...

Initializer

The default text domain can be set in a initializer file config/initializers/gettext.rb

LocaleSelector::default_domain = 'myapp'

View

In app/views/layouts/application.html.erb or in single views you can use

<%= language_selector %>

Or write your own language selector helper. For the example see the documentation for language_selector method for an implementation example.

Defined Under Namespace

Modules: ControllerClassMethods, ControllerInstanceMethods, ViewInstanceMethods

Class Method Summary collapse

Class Method Details

.default_domainObject



78
79
80
# File 'lib/locale_selector.rb', line 78

def self.default_domain
  defined?(@@default_domain) ? @@default_domain : 'default'
end

.default_domain=(d) ⇒ Object

Please set the gettext domain in a initializer, the file config/initializers/gettext.rb is recommended

LocaleSelector::default_domain = 'myapp'


74
75
76
# File 'lib/locale_selector.rb', line 74

def self.default_domain=(d)
  @@default_domain = d
end