Permalink
Browse files

initial import into trunk

  • Loading branch information...
0 parents commit fc9b064cf628d7fc45391c1efd05cc816e41c66e @peburrows committed Jan 30, 2008
48 README
@@ -0,0 +1,48 @@
+= Translator Extension
+
+Created By: Phil Burrows, January 2008
+-------------------------
+peburrows@gmail.com
+http://philburrows.com
+http://svn.philburrows.com/radiant-extensions/translator
+-------------------------
+
+== Description
+
+This extension allows for different page parts to be rendered
+depending on the user's Accept-Language header.
+
+== Disclaimer
+
+This extension works well for me, but YMMV.
+
+== Dependencies
+
+* None, really
+
+== Usage
+
+In the pages you wish to have different languages available,
+simply create a page part with a two letter language abbreviation
+suffix for every part you wish to have available in multiple languages.
+
+NOTE: the default language is english, so page parts without the
+two-letter language suffix will be considered english.
+
+Examples:
+---------
+Page Parts:
+ 'body' (English)
+ 'body_de' (German translation)
+ 'body_fr' (French translation)
+
+ 'sidebar' (English)
+ 'sidebar_de' (German translation)
+ 'sidebar_fr' (French translation)
+
+Then, in your layout, simply use the translator tags the same way you would use the standard Radiant content tag
+
+<r:translator:content name="body" />
+<r:translator:content name="sidebar" inherit="true" />
+
+each of these will render the appropriate page part, determined by the browser's Accept-Language
@@ -0,0 +1,25 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the translator extension.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the translator extension.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'TranslatorExtension'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+# Load any custom rakefiles for extension
+Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
@@ -0,0 +1,19 @@
+class LanguageController < ApplicationController
+ # Remove this line if your controller should only be accessible to users
+ # that are logged in:
+ no_login_required
+
+ def set_lang
+ if params[:lang].downcase == 'reset'
+ session[:language] = nil
+ else
+ session[:language] = params[:lang].downcase
+ end
+
+ if !params[:from].blank?
+ redirect_to params[:from] and return
+ else
+ redirect_to '/' and return
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module LanguageHelper
+end
@@ -0,0 +1,75 @@
+module TranslatorTags
+ include Radiant::Taggable
+
+ tag 'translator' do |tag|
+ tag.expand
+ end
+
+
+ # we're ripping this from Radiant's StandardTags module and molding it to fit our needs
+ desc %{
+ Works just like the standard Radiant tag @<r:content />@
+
+ The translator tag, however, renders the page part that is suffixed with the browser's
+ Language-Accept header.
+
+ *Usage:*
+ <pre><code><r:translator:content [part="part_name"] [inherit="true|false"] [contextual="true|false"] /></code></pre>
+ <pre><code><r:translator:content part="body" /></code></pre>
+
+ 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|
+ page = tag.locals.page
+
+ # 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
+ 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
+
+ logger.error(request.session)
+
+ # and now the part's suffix will be determined by the accept-language
+ suffix = lang.match(/^en/i) ? "" : "_#{lang}"
+
+ base_part_name = tag_part_name(tag)
+ part_name = base_part_name + "#{suffix}"
+
+ boolean_attr = proc do |attribute_name, default|
+ attribute = (tag.attr[attribute_name] || default).to_s
+ raise TagError.new(%{`#{attribute_name}' attribute of `content' tag must be set to either "true" or "false"}) unless attribute =~ /true|false/i
+ (attribute.downcase == 'true') ? true : false
+ end
+ inherit = boolean_attr['inherit', false]
+ part_page = page
+ if inherit
+ while (part_page.part(part_name).nil? and part_page.part(base_part_name).nil? and (not part_page.parent.nil?)) do
+ part_page = part_page.parent
+ end
+ end
+ contextual = boolean_attr['contextual', true]
+ if inherit and contextual
+ if part_page.part(part_name).nil?
+ part = part_page.part(base_part_name)
+ else
+ part = part_page.part(part_name)
+ end
+ page.render_snippet(part) unless part.nil?
+ else
+ if part_page.part(part_name).nil?
+ part_page.render_part(base_part_name)
+ else
+ part_page.render_part(part_name)
+ end
+ end
+ end
+
+end
@@ -0,0 +1,28 @@
+namespace :radiant do
+ namespace :extensions do
+ namespace :translator do
+
+ desc "Runs the migration of the Translator extension"
+ task :migrate => :environment do
+ require 'radiant/extension_migrator'
+ if ENV["VERSION"]
+ TranslatorExtension.migrator.migrate(ENV["VERSION"].to_i)
+ else
+ TranslatorExtension.migrator.migrate
+ end
+ end
+
+ desc "Copies public assets of the Translator to the instance public/ directory."
+ task :update => :environment do
+ is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
+ Dir[TranslatorExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
+ path = file.sub(TranslatorExtension.root, '')
+ directory = File.dirname(path)
+ puts "Copying #{path}..."
+ mkdir_p RAILS_ROOT + directory
+ cp file, RAILS_ROOT + path
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,17 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+# Re-raise errors caught by the controller.
+LanguageController.class_eval { def rescue_action(e) raise e end }
+
+class LanguageControllerTest < Test::Unit::TestCase
+ def setup
+ @controller = LanguageController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end
@@ -0,0 +1,15 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class TranslatorExtensionTest < Test::Unit::TestCase
+
+ # Replace this with your real tests.
+ def test_this_extension
+ flunk
+ end
+
+ def test_initialization
+ assert_equal File.join(File.expand_path(RAILS_ROOT), 'vendor', 'extensions', 'translator'), TranslatorExtension.root
+ assert_equal 'Translator', TranslatorExtension.extension_name
+ end
+
+end
@@ -0,0 +1,19 @@
+require 'test/unit'
+# # Load the environment
+unless defined? RADIANT_ROOT
+ ENV["RAILS_ENV"] = "test"
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
+end
+require "#{RADIANT_ROOT}/test/test_helper"
+
+class Test::Unit::TestCase
+
+ # Include a helper to make testing Radius tags easier
+ test_helper :extension_tags
+
+ # Add the fixture directory to the fixture path
+ self.fixture_path << File.dirname(__FILE__) + "/fixtures"
+
+ # Add more helper methods to be used by all extension tests here...
+
+end
@@ -0,0 +1,22 @@
+# Uncomment this if you reference any of your controllers in activate
+require_dependency 'application'
+
+class TranslatorExtension < Radiant::Extension
+ version "0.1"
+ description "Allows you to render your pages in different languages based upon the browser's Accept-Language."
+ url "http://philburrows.com"
+
+ define_routes do |map|
+ map.connect 'language/set/:lang', :controller => 'language', :action => 'set_lang'
+ end
+
+ def activate
+ Page.send :include, TranslatorTags
+ SiteController.class_eval{session :disabled => false}
+ end
+
+ def deactivate
+ # don't really need anything here
+ end
+
+end

0 comments on commit fc9b064

Please sign in to comment.