Skip to content

Commit

Permalink
enabling four-letter language usage
Browse files Browse the repository at this point in the history
  • Loading branch information
peburrows committed Feb 11, 2008
1 parent 97b0a97 commit 389a7e9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/controllers/language_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class LanguageController < ApplicationController


def set_lang def set_lang
# setting up the :language session variable which will be referenced in the tags # setting up the :language session variable which will be referenced in the tags
if params[:lang].downcase == 'reset' if params[:language].downcase == 'reset'
session[:language] = nil session[:language] = nil
else else
session[:language] = params[:lang].downcase session[:language] = params[:language].downcase
end end


if !params[:from].blank? if !params[:from].blank?
Expand Down
51 changes: 35 additions & 16 deletions app/models/translate_response_cache.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,31 +29,50 @@ def do_something_bad
} }
end end


ActionController::AbstractRequest.class_eval { ActionController::AbstractRequest.class_eval {
def language # for unusual default mappings, i.e. ones that do not match the typical 'ab' => 'ab-AB' pattern
# this is where we need to grab the Accept-Language # examples: en-UK, en-US, es-MX
lang = self.env['HTTP_ACCEPT_LANGUAGE'] @@mappings = {
'en' => 'en-US'
}
cattr_accessor :mappings


# grab the two letter abbreviation -- some browsers pass multiple languages, but we just want the first one (for now) # when the language is requested, we'll standardize how we'd like it returned
# there's quite a bit more we could do with this, like falling back to the other languages that the user-agent requests # currently, we're defaulting to the four letter variety. However, the normal
# for now, it's a simple hit or miss on the first in the series # <r:translator:content /> and <r:translator:title /> tags chop off the end and only
m = lang.match(/^([a-zA-Z][a-zA-Z])(.)+$/) # use the first two letters of the requested language
if m && !self.session[:language] def proper_language(two_letter)
lang = m.captures.first.downcase if two_letter.length == 2
if @@mappings[two_letter]
@@mappings[two_letter]
else
two_letter.downcase + '-' + two_letter.upcase
end
else else
lang = self.session[:language] || TranslatorExtension.defaults[:lang] if m = two_letter.match(/^[a-zA-Z]{2}\-([a-zA-Z]{2})?/)
splitter = m[0].split('-')
splitter[0].downcase + '-' + splitter[1].upcase
else
# otherwise, send back the default
TranslatorExtension.defaults[:language]
end
end end
# send back the two-letter abbreviation, or a blank string if it's english
lang.match(Regexp.new("^#{TranslatorExtension.defaults[:lang]}")) ? "" : lang
end end


def language_full # return the requested language of the current request
def language
return proper_language(self.parameters[:language]) if self.parameters[:language]
return session_lang = proper_language(self.session[:language]) if self.session[:language]

lang = self.env['HTTP_ACCEPT_LANGUAGE'] lang = self.env['HTTP_ACCEPT_LANGUAGE']
m = lang.match(/^[a-zA-Z]{2}(\-[a-zA-Z]{2})?/) m = lang.match(/^[a-zA-Z]{2}(\-[a-zA-Z]{2})?/)
# return the four letter (or two if no four was found) language string or nil if nothing was found
m ? m[0] : nil return TranslatorExtension.defaults[:language] unless m
match = m[0]
return proper_language(match)
end end


# turn the requested language into a proper suffix for the translator extension tags
def suffixize(lang) def suffixize(lang)
lang.blank? ? "" : "_#{lang}" lang.blank? ? "" : "_#{lang}"
end end
Expand Down
37 changes: 34 additions & 3 deletions app/models/translator_tags.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module TranslatorTags
title = page.title title = page.title
config_content = page.render_part(:config) config_content = page.render_part(:config)
unless config_content.blank? unless config_content.blank?
lang = request.language lang = request.language.split('-').first
config = YAML::load(config_content) config = YAML::load(config_content)
config = (config.blank? || config['translator'].blank?) ? {} : config['translator'] config = (config.blank? || config['translator'].blank?) ? {} : config['translator']
if config[lang] if config[lang]
Expand Down Expand Up @@ -61,11 +61,38 @@ module TranslatorTags
req = tag.globals.page.request req = tag.globals.page.request
page = tag.locals.page page = tag.locals.page


suffix = req.suffixize(req.language) suffix = req.suffixize(req.language.split('-').first)


base_part_name = tag_part_name(tag) base_part_name = tag_part_name(tag)
part_name = base_part_name + "#{suffix}" # part_name = base_part_name + "#{suffix}"


render_translated_page_part(tag, page, req, base_part_name, suffix)
end

tag 'translator:four' do |tag|
tag.expand
end

tag 'translator:four:content' do |tag|
# here's where we'll render the content for the page
req = tag.globals.page.request
page = tag.locals.page

suffix = req.suffixize(req.language)
base_part_name = tag_part_name(tag)
render_translated_page_part(tag, page, req, base_part_name, suffix)
end

tag 'translator:four:title' do |tag|
'still needs to be implemented'
end

protected
def render_translated_page_part(tag, page, req, base_part_name, suffix)
part_name = base_part_name + suffix

logger.error("\n\n\n\n\n::::::I am going to attempt to render the following part::::: #{part_name}\n\n\n\n\n")

boolean_attr = proc do |attribute_name, default| boolean_attr = proc do |attribute_name, default|
attribute = (tag.attr[attribute_name] || default).to_s 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 raise TagError.new(%{`#{attribute_name}' attribute of `content' tag must be set to either "true" or "false"}) unless attribute =~ /true|false/i
Expand All @@ -81,15 +108,19 @@ module TranslatorTags
contextual = boolean_attr['contextual', true] contextual = boolean_attr['contextual', true]
if inherit and contextual if inherit and contextual
if part_page.part(part_name).nil? if part_page.part(part_name).nil?
logger.error("\n\ncouldn't find a part with the name #{part_name}\n\n")
part = part_page.part(base_part_name) part = part_page.part(base_part_name)
else else
logger.error("\n\nfound the part with the name #{part_name}\n\n")
part = part_page.part(part_name) part = part_page.part(part_name)
end end
page.render_snippet(part) unless part.nil? page.render_snippet(part) unless part.nil?
else else
if part_page.part(part_name).nil? if part_page.part(part_name).nil?
logger.error("\n\ncouldn't find a part with the name #{part_name}\n\n")
part_page.render_part(base_part_name) part_page.render_part(base_part_name)
else else
logger.error("\n\nfound the part with the name #{part_name}\n\n")
part_page.render_part(part_name) part_page.render_part(part_name)
end end
end end
Expand Down
16 changes: 9 additions & 7 deletions translator_extension.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class TranslatorExtension < Radiant::Extension


# still plenty of work that needs to be done on this # still plenty of work that needs to be done on this
@@defaults = { @@defaults = {
:lang => 'en' :language => 'en-US'
} }
cattr_accessor :defaults cattr_accessor :defaults


define_routes do |map| define_routes do |map|
map.connect 'language/set/:lang', :controller => 'language', :action => 'set_lang' map.connect 'language/set/:language', :controller => 'language', :action => 'set_lang'
map.connect ':lang/*url', map.connect ':language/*url',
:controller => 'site', :controller => 'site',
:action => 'show_page', :action => 'show_page',
:requirements => { :requirements => {
:lang => /[a-zA-Z]{2}/ :language => /[a-zA-Z]{2}/
} }
end end


Expand All @@ -29,9 +29,11 @@ def activate
before_filter :set_up_lang before_filter :set_up_lang
private private
def set_up_lang def set_up_lang
logger.error("we're setting the language \n\n\n#{params[:lang]}\n\n\n") if params[:language]
if params[:lang] # we want to save the four-letter language code, not just the two
session[:language] = params[:lang] # logger.error("we're setting the language \n\n\n#{params[:language]}\n\n\n")
# session[:language] = params[:language]
session[:language] = request.language
end end
end end
} }
Expand Down

0 comments on commit 389a7e9

Please sign in to comment.