Permalink
Browse files

fixing some issues concerning caching. pages should now be classified…

… in the cache by language as well to keep from serving up a cached page in the wrong language.
  • Loading branch information...
1 parent d6ff02a commit 059246e291bdaec71b90d256ffa5b40014e8ea74 @peburrows committed Feb 8, 2008
Showing with 81 additions and 49 deletions.
  1. +70 −20 app/models/translate_response_cache.rb
  2. +4 −28 app/models/translator_tags.rb
  3. +7 −1 translator_extension.rb
@@ -1,29 +1,79 @@
class TranslateResponseCache
- ResponseCache.class_eval{
- private
- def page_cache_path(path)
- logger.error("\n\n-------------------\n i was called? \n---------------------\n\n")
+ [SiteController].each do |klass|
+ klass.class_eval {
+ # yes, i know this is really bad, but we need it. otherwise, we'll have to reinvent the wheel
+ around_filter :do_something_bad
+
+ protected
+ def do_something_bad
+ klasses = [ResponseCache]
+ methods = ["session", "cookies", "params", "request"]
+
+ methods.each do |need|
+ please = instance_variable_get(:"@_#{need}")
+
+ klasses.each do |klass|
+ klass.send(:define_method, "kk_#{need}", proc { please })
+ end
+ end
- # set up '/' as '/index' so we don't have to worry about having cache files in the wrong directory
- path = page_cache_file(path)
- path = translator_path(path)
- root_dir = File.expand_path(page_cache_directory)
- cache_path = File.expand_path(File.join(root_dir,path), root_dir)
- if(cache_path.index(root_dir) == 0)
- logger.error(cache_path)
- cache_path
+ yield
+
+ methods.each do |need|
+ klasses.each do |klass|
+ klass.send :remove_method, "kk_#{need}"
+ end
+ end
+ end
+ }
+ end
+
+ ActionController::AbstractRequest.class_eval {
+ def language
+ # this is where we need to grab the Accept-Language
+ lang = self.env['HTTP_ACCEPT_LANGUAGE']
+
+ # grab the two letter abbreviation -- some browsers pass multiple languages, but we just want the first one (for now)
+ # there's quite a bit more we could do with this, like falling back to the other languages that the user-agent requests
+ # for now, it's a simple hit or miss on the first in the series
+ m = lang.match(/^([a-zA-Z][a-zA-Z])(.)+$/)
+ if m && !self.session[:language]
+ lang = m.captures.first.downcase
+ else
+ lang = self.session[:language] || TranslatorExtension.defaults[:lang]
end
+ # send back the two-letter abbreviation, or a blank string if it's english
+ lang.match(Regexp.new("^#{TranslatorExtension.defaults[:lang]}")) ? "" : lang
end
-
- # def page_cache_file(path)
- # name = ((path.empty? || path == "/") ? "/index" : URI.unescape(path))
- # end
-
- def translator_path(path)
- path = path + '_en' unless path.match(/[\.css|\.js]$/)
- path
+
+ def suffixize(lang)
+ lang.blank? ? "" : "_#{lang}"
end
}
+ ResponseCache.class_eval {
+ # in here, we're just adding a two-letter language suffix to cached pages to make sure that the wrong
+ # language doesn't get served up because it has been cached inappropriately. we could change this to
+ # cache in a separate directory (i.e. en/), but for now, we're just adding the extension
+ private
+ def translator_path(path)
+ req = kk_request
+ path = path + req.suffixize(req.language) unless path.match(/[\.css|\.js]$/)
+ path
+ end
+
+ def page_cache_path(path)
+ # set up '/' as '/index' so we don't have to worry about having cache files in the wrong directory
+ # this is an error in the radiant caching
+ path = page_cache_file(path)
+ path = translator_path(path)
+ root_dir = File.expand_path(page_cache_directory)
+ cache_path = File.expand_path(File.join(root_dir,path), root_dir)
+ if(cache_path.index(root_dir) == 0)
+ logger.error(cache_path)
+ cache_path
+ end
+ end
+ }
end
@@ -26,11 +26,12 @@ module TranslatorTags
</pre></code>
}
tag 'translator:title' do |tag|
+ request = tag.globals.page.request
page = tag.locals.page
title = page.title
config_content = page.render_part(:config)
unless config_content.blank?
- lang = language(tag)
+ lang = request.language
config = YAML::load(config_content)
config = (config.blank? || config['translator'].blank?) ? {} : config['translator']
if config[lang]
@@ -57,9 +58,10 @@ module TranslatorTags
If the Language-Accept header was set to fr-ca (French, Canadian), it would render the "body_fr" content part.
}
tag 'translator:content' do |tag|
+ req = tag.globals.page.request
page = tag.locals.page
- suffix = suffixize(language(tag))
+ suffix = req.suffixize(req.language)
base_part_name = tag_part_name(tag)
part_name = base_part_name + "#{suffix}"
@@ -93,30 +95,4 @@ module TranslatorTags
end
end
-protected
-
- def language(tag)
- # this is where we need to grab the Accept-Language
- request = tag.globals.page.request
- lang = request.env['HTTP_ACCEPT_LANGUAGE']
-
- # grab the two letter abbreviation -- some browsers pass multiple languages, but we just want the first one (for now)
- # there's quite a bit more we could do with this, like falling back to the other languages that the user-agent requests
- # for now, it's a simple hit or miss on the first in the series
- m = lang.match(/^([a-zA-Z][a-zA-Z])(.)+$/)
- if m && !request.session[:language]
- lang = m.captures.first.downcase
- else
- # english is set as the default
- lang = request.session[:language] || "en"
- end
-
- # send back the two-letter abbreviation, or a blank string if it's english
- lang.match(/^en/i) ? "" : lang
- end
-
- def suffixize(lang)
- lang.blank? ? "" : "_#{lang}"
- end
-
end
View
@@ -2,10 +2,16 @@
require_dependency 'application'
class TranslatorExtension < Radiant::Extension
- version "0.1"
+ version "0.2"
description "Allows you to render your pages in different languages based upon the browser's Accept-Language."
url "http://dev.philburrows.com/svn/radiant-extensions/translator/trunk"
+ # still plenty of work that needs to be done on this
+ @@defaults = {
+ :lang => 'en'
+ }
+ cattr_accessor :defaults
+
define_routes do |map|
map.connect 'language/set/:lang', :controller => 'language', :action => 'set_lang'
end

0 comments on commit 059246e

Please sign in to comment.