Module CASServer::Localization
In: lib/casserver/localization.rb

Methods

Included Modules

GetText GetText

Public Class methods

[Source]

    # File lib/casserver/localization.rb, line 7
 7:     def self.included(mod)
 8:       mod.module_eval do
 9:         include GetText
10:       end
11:     end

Public Instance methods

[Source]

    # File lib/casserver/localization.rb, line 87
87:     def available_locales
88:       (Dir.glob(File.join(File.dirname(File.expand_path(__FILE__)), "../../locale/[a-z]*")).map{|path| File.basename(path)} << "en").uniq.collect{|l| l.gsub('_','-')}
89:     end

[Source]

    # File lib/casserver/localization.rb, line 16
16:     def determine_locale(request)
17:       source = nil
18:       lang = case
19:       when !request.params['lang'].blank?
20:         source = "'lang' request variable"
21:         request.cookies['lang'] = request.params['lang']
22:         request.params['lang']
23:       when !request.cookies['lang'].blank?
24:         source = "'lang' cookie"
25:         request.cookies['lang']
26:       when !request.env['HTTP_ACCEPT_LANGUAGE'].blank?
27:         source = "'HTTP_ACCEPT_LANGUAGE' header"
28:         lang = request.env['HTTP_ACCEPT_LANGUAGE']
29:       when !request.env['HTTP_USER_AGENT'].blank? && request.env['HTTP_USER_AGENT'] =~ /[^a-z]([a-z]{2}(-[a-z]{2})?)[^a-z]/i
30:         source = "'HTTP_USER_AGENT' header"
31:         $~[1]
32: #      when !$CONF['default_locale'].blank?
33: #        source = "'default_locale' config option"
34: #        $CONF[:default_locale]
35:       else
36:         source = "default"
37:         "en"
38:       end
39: 
40:       $LOG.debug "Detected locale is #{lang.inspect} (from #{source})"
41: 
42:       lang.gsub!('_','-')
43: 
44:       # TODO: Need to confirm that this method of splitting the accepted
45:       #       language string is correct.
46:       if lang =~ /[,;\|]/
47:         langs = lang.split(/[,;\|]/)
48:       else
49:         langs = [lang]
50:       end
51: 
52:       # TODO: This method of selecting the desired language might not be
53:       #       standards-compliant. For example, http://www.w3.org/TR/ltli/
54:       #       suggests that de-de and de-*-DE might be acceptable identifiers
55:       #       for selecting various wildcards. The algorithm below does not
56:       #       currently support anything like this.
57: 
58:       available = available_locales
59: 
60:       if available.length == 1
61:         $LOG.warn "Only the #{available.first.inspect} localization is available. You should run `rake localization:mo` to compile support for additional languages!"
62:       elsif available.length == 0 # this should never actually happen
63:         $LOG.error "No localizations available! Run `rake localization:mo` to compile support for additional languages."
64:       end
65: 
66:       # Try to pick a locale exactly matching the desired identifier, otherwise
67:       # fall back to locale without region (i.e. given "en-US; de-DE", we would
68:       # first look for "en-US", then "en", then "de-DE", then "de").
69: 
70:       chosen_lang = nil
71:       langs.each do |l|
72:         a = available.find{ |a| a =~ Regexp.new("\\A#{l}\\Z", 'i') ||
73:                                 a =~ Regexp.new("#{l}-\w*",   'i')    }
74:         if a
75:           chosen_lang = a
76:           break
77:         end
78:       end
79: 
80:       chosen_lang = "en" if chosen_lang.blank?
81: 
82:       $LOG.debug "Chosen locale is #{chosen_lang.inspect}"
83: 
84:       return chosen_lang
85:     end

[Validate]