Module: MLocale

Defined in:
lib/merb_babel/m_locale.rb

Overview

The MLocale module helps you set up a locale, language, country You don’t have to use a locale, in some cases you might just want to use the language

Instance Method Summary collapse

Instance Method Details

#countryObject

The country is used when localizing currency or time



36
37
38
# File 'lib/merb_babel/m_locale.rb', line 36

def country
  request.env[:country] || params[:country] || country_from_locale || (session ? session[:country] : nil) || LocaleDetector.country_from_language(language) || default_country
end

#country_from_localeObject

Extract the country from the locale



51
52
53
# File 'lib/merb_babel/m_locale.rb', line 51

def country_from_locale
  request.env[:locale] ? request.env[:locale][3..5].upcase : nil
end

#default_countryObject



67
68
69
# File 'lib/merb_babel/m_locale.rb', line 67

def default_country
  Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_country] : nil
end

#default_languageObject



63
64
65
# File 'lib/merb_babel/m_locale.rb', line 63

def default_language
  Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_language] : nil
end

#default_localeObject

Defaults set in the plugin settings You can change the default settings by overwriting the Merb::Plugins.config hash in your settings



59
60
61
# File 'lib/merb_babel/m_locale.rb', line 59

def default_locale
  Merb::Plugins.config[:merb_babel] ? Merb::Plugins.config[:merb_babel][:default_locale] : nil
end

#languageObject

Many people don’t care about locales, they might just want to use languages instead



31
32
33
# File 'lib/merb_babel/m_locale.rb', line 31

def language
  request.env[:language] || params[:language] || language_from_locale || (session ? session[:language] : nil) || default_language
end

#language_from_localeObject

Extract the language from the locale



41
42
43
44
45
46
47
48
# File 'lib/merb_babel/m_locale.rb', line 41

def language_from_locale
  if request.env[:locale] && request.env[:locale] =~ locale_regexp
    language, country = request.env[:locale].match(locale_regexp).captures
    return language
  else 
    return nil
  end
end

#localeObject

A locale is made of a language + country code, such as en-UK or en-US



26
27
28
# File 'lib/merb_babel/m_locale.rb', line 26

def locale 
  request.env[:locale] || params[:locale] || (session ? session[:locale] : nil) || locale_from_request || default_locale
end

#locale_from_requestObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/merb_babel/m_locale.rb', line 5

def locale_from_request
  if hal = request.env["HTTP_ACCEPT_LANGUAGE"]
    hal.gsub!(/\s/, "")
    result = hal.split(/,/).map do |v|
      v.split(";q=")
    end.map do |j|
      [j[0], j[1] ? j[1].to_f : 1.0]
    end.sort do |a,b|
      -(a[1] <=> b[1])
    end.map do |v|
      Locale::Tag.parse(v[0])
    end.first
    return nil if result.nil?
    language = result.language
    country = result.country ||
      LocaleDetector.country_from_language(language)
    request.env[:locale] = "#{language}-#{country}"
  end
end