forked from Jehreg/redmine-clf2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clf2_application_controller_patch.rb
148 lines (129 loc) · 5.37 KB
/
clf2_application_controller_patch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Patched to the ApplicationController
module Clf2
module ApplicationController
module Patch
def self.included(base)
base.extend(::Clf2::ApplicationController::ClassMethods)
base.class_eval do
cattr_accessor :clf2_subdomain_languages
base.send(:include, ::Clf2::ApplicationController::InstanceMethods)
# Skip Redmine's default :set_localization
skip_before_filter :set_localization
# Add our :set_localization method to the start of the chain
prepend_before_filter_if_not_already_added :set_localization
# Add set_localization_from_domain to the very beginning
prepend_before_filter_if_not_already_added :switch_language_from_domain
alias_method_chain :set_localization, :clf_mods
base.load_clf2_subdomains_file
helper :clf2
# Include the current language in the url as a query string (ISO 639-2)
def url_for_with_language_in_url(options)
# Pass without language if options isn't a hash (e.g. url string)
unless options.respond_to?(:merge)
return url_for_without_language_in_url(options)
end
case current_language
when :en
url_for_without_language_in_url(options.merge(:lang => 'eng'))
when :fr
url_for_without_language_in_url(options.merge(:lang => 'fra'))
else
url_for_without_language_in_url(options)
end
end
alias_method_chain :url_for, :language_in_url
end
end
end
module ClassMethods
# Wraps +prepend_before_filter+ in a test to make sure a filter
# isn't being added multiple times, which can happen with class
# reloading in development mode
def prepend_before_filter_if_not_already_added(method)
unless filter_already_added? method
prepend_before_filter method
end
end
# Wraps +before_filter+ in a test to make sure a filter
# isn't being added multiple times, which can happen with class
# reloading in development mode
def before_filter_if_not_already_added(method)
unless filter_already_added? method
before_filter method
end
end
# Checks if a filter has already been added to the filter_chain
def filter_already_added?(filter)
return self.filter_chain.collect(&:method).include?(filter)
end
# Load the clf2_subdomains.yml file to configure the subdomain
# to language mapping. In development mode this will be
# reloaded with each request but in production, it will be cached.
def load_clf2_subdomains_file
domain_file = File.join(Rails.plugins['redmine-franglish'].directory,'config','clf2_subdomains.yml')
if File.exists?(domain_file)
self.clf2_subdomain_languages = YAML::load(File.read(domain_file))
logger.debug "Loaded CLF2 subdomain file"
else
logger.error "CLF2 subdomain file not found at #{domain_file}. Subdomain specific languages will not be used."
end
end
end
# Additional InstanceMethods
module InstanceMethods
def switch_language_to(language)
# Guard against a loop since this runs for the
# LanguageSwitcherController also
unless params[:controller] == 'language_switcher'
redirect_to :controller => 'language_switcher', :action => language
end
end
def switch_language_from_domain
# Skip if language is already set
return true if session[:language]
request.subdomains.each do |subdomain|
# if subdomain && self.clf2_subdomain_languages.respond_to?(:key?) && self.clf2_subdomain_languages.key?(subdomain)
# logger.debug "Switching language from domain"
#
# if self.clf2_subdomain_languages[subdomain] == :fr
# switch_language_to(:french)
# break
# elsif self.clf2_subdomain_languages[subdomain] == :en
# switch_language_to(:english)
# break
# else
# # Fallback to the other detection methods
# end
# end
end
end
def set_current_language_from_session
if session[:language]
User.current.language = session[:language]
current_language = session[:language]
else
User.current.language = nil unless User.current.logged?
end
end
def set_localization_with_clf_mods
logger.debug "In set_localization_with_clf_mods"
switch_language_to(:french) if request.request_uri =~ /\/french$/
switch_language_to(:english) if request.request_uri =~ /\/english$/
set_current_language_from_session
# Most of this is copied from the core, except as noted.
lang = nil
if User.current.language.present? # Modified from core
lang = find_language(User.current.language)
end
if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE']
accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase
if !accept_lang.blank?
lang = find_language(accept_lang) || find_language(accept_lang.split('-').first)
end
end
lang ||= Setting.default_language
set_language_if_valid(lang)
end
end # InstanceMethods
end
end