Module | ArkanisDevelopment::SimpleLocalization::LocalizedTemplates |
In: |
lib/features/localized_templates.rb
|
# File lib/features/localized_templates.rb, line 20 20: def self.included(base) 21: base.class_eval do 22: 23: alias_method :render_file_without_localization, :render_file 24: 25: # Name of file extensions which are handled internally in rails. Other types 26: # like liquid has to register through register_handler. 27: # The erb extension is used to handle .html.erb templates. 28: @@native_extensions = /\.(rjs|rhtml|rxml|erb)$/ 29: 30: @@localized_path_cache = {} 31: 32: def render_file(template_path, use_full_path = true, local_assigns = {}) 33: @first_render ||= template_path 34: 35: localized_path, template_extension = locate_localized_path(template_path, use_full_path) 36: 37: # Delegate templates are picked by the template extension and if 38: # use_full_path is true Rails does not search for an extension and so 39: # delegate templates won't work. To fix this try to convert the path 40: # back to a relative one. 41: if use_full_path 42: localized_path.gsub!(/#{Regexp.escape('.' + template_extension)}$/, '') if template_extension 43: 44: # Make this rails edge secure. Edgy uses an array called view_paths 45: # to store paths of the view files. Rails 1.2 stors just on path in 46: # the @base_path variable. 47: if self.respond_to?(:view_paths) 48: self.view_paths.each do |view_path| 49: localized_path.gsub!(/^#{Regexp.escape(view_path)}\//, '') 50: end 51: else 52: localized_path.gsub!(/^#{Regexp.escape(@base_path)}\//, '') 53: end 54: end 55: 56: # don't use_full_path -- we've already expanded the path 57: # FALSE: doing this will break delegate templates! 58: render_file_without_localization(localized_path, use_full_path, local_assigns) 59: end 60: 61: private 62: 63: alias_method :path_and_extension_without_localization, :path_and_extension 64: 65: # Override because the original version is too minimalist 66: def path_and_extension(template_path) #:nodoc: 67: template_path_without_extension = template_path.sub(@@native_extensions, '') 68: [ template_path_without_extension, $1 ] 69: end 70: 71: def locate_localized_path(template_path, use_full_path) 72: current_language = Language.current_language 73: 74: cache_key = "#{current_language}:#{template_path}" 75: cached = @@localized_path_cache[cache_key] 76: return cached if cached 77: 78: if use_full_path 79: template_path_without_extension, template_extension = path_and_extension(template_path) 80: 81: if template_extension 82: template_file_name = full_template_path(template_path_without_extension, template_extension) 83: else 84: template_extension = pick_template_extension(template_path).to_s 85: template_file_name = full_template_path(template_path, template_extension) 86: end 87: else 88: template_file_name = template_path 89: template_extension = path_and_extension(template_path).last 90: end 91: 92: # template_extension is nil if the specified template does not use a 93: # template engine (like render :file => ... with a .html, .txt, ect. 94: # extension). In this case just pass the template name as it is. 95: if template_extension 96: pn = Pathname.new(template_file_name) 97: dir, filename = pn.dirname, pn.basename('.' + template_extension) 98: 99: localized_path = dir + "#{filename}.#{current_language}.#{template_extension}" 100: 101: unless localized_path.exist? 102: localized_path = template_file_name 103: end 104: else 105: localized_path = template_file_name 106: end 107: 108: [@@localized_path_cache[cache_key] = localized_path.to_s, template_extension] 109: end 110: 111: end 112: end
# File lib/features/localized_templates.rb, line 71 71: def locate_localized_path(template_path, use_full_path) 72: current_language = Language.current_language 73: 74: cache_key = "#{current_language}:#{template_path}" 75: cached = @@localized_path_cache[cache_key] 76: return cached if cached 77: 78: if use_full_path 79: template_path_without_extension, template_extension = path_and_extension(template_path) 80: 81: if template_extension 82: template_file_name = full_template_path(template_path_without_extension, template_extension) 83: else 84: template_extension = pick_template_extension(template_path).to_s 85: template_file_name = full_template_path(template_path, template_extension) 86: end 87: else 88: template_file_name = template_path 89: template_extension = path_and_extension(template_path).last 90: end 91: 92: # template_extension is nil if the specified template does not use a 93: # template engine (like render :file => ... with a .html, .txt, ect. 94: # extension). In this case just pass the template name as it is. 95: if template_extension 96: pn = Pathname.new(template_file_name) 97: dir, filename = pn.dirname, pn.basename('.' + template_extension) 98: 99: localized_path = dir + "#{filename}.#{current_language}.#{template_extension}" 100: 101: unless localized_path.exist? 102: localized_path = template_file_name 103: end 104: else 105: localized_path = template_file_name 106: end 107: 108: [@@localized_path_cache[cache_key] = localized_path.to_s, template_extension] 109: end
# File lib/features/localized_templates.rb, line 32 32: def render_file(template_path, use_full_path = true, local_assigns = {}) 33: @first_render ||= template_path 34: 35: localized_path, template_extension = locate_localized_path(template_path, use_full_path) 36: 37: # Delegate templates are picked by the template extension and if 38: # use_full_path is true Rails does not search for an extension and so 39: # delegate templates won't work. To fix this try to convert the path 40: # back to a relative one. 41: if use_full_path 42: localized_path.gsub!(/#{Regexp.escape('.' + template_extension)}$/, '') if template_extension 43: 44: # Make this rails edge secure. Edgy uses an array called view_paths 45: # to store paths of the view files. Rails 1.2 stors just on path in 46: # the @base_path variable. 47: if self.respond_to?(:view_paths) 48: self.view_paths.each do |view_path| 49: localized_path.gsub!(/^#{Regexp.escape(view_path)}\//, '') 50: end 51: else 52: localized_path.gsub!(/^#{Regexp.escape(@base_path)}\//, '') 53: end 54: end 55: 56: # don't use_full_path -- we've already expanded the path 57: # FALSE: doing this will break delegate templates! 58: render_file_without_localization(localized_path, use_full_path, local_assigns) 59: end