Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

enabling four-letter language usage

  • Loading branch information...
commit 389a7e9e4df209e9e9abb5299a4808b36a20dcba 1 parent 97b0a97
@peburrows authored
View
4 app/controllers/language_controller.rb
@@ -5,10 +5,10 @@ class LanguageController < ApplicationController
def set_lang
# 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
else
- session[:language] = params[:lang].downcase
+ session[:language] = params[:language].downcase
end
if !params[:from].blank?
View
51 app/models/translate_response_cache.rb
@@ -29,31 +29,50 @@ def do_something_bad
}
end
- ActionController::AbstractRequest.class_eval {
- def language
- # this is where we need to grab the Accept-Language
- lang = self.env['HTTP_ACCEPT_LANGUAGE']
+ ActionController::AbstractRequest.class_eval {
+ # for unusual default mappings, i.e. ones that do not match the typical 'ab' => 'ab-AB' pattern
+ # examples: en-UK, en-US, es-MX
+ @@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)
- # 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
+ # when the language is requested, we'll standardize how we'd like it returned
+ # currently, we're defaulting to the four letter variety. However, the normal
+ # <r:translator:content /> and <r:translator:title /> tags chop off the end and only
+ # use the first two letters of the requested language
+ def proper_language(two_letter)
+ if two_letter.length == 2
+ if @@mappings[two_letter]
+ @@mappings[two_letter]
+ else
+ two_letter.downcase + '-' + two_letter.upcase
+ end
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
- # send back the two-letter abbreviation, or a blank string if it's english
- lang.match(Regexp.new("^#{TranslatorExtension.defaults[:lang]}")) ? "" : lang
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']
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
+ # turn the requested language into a proper suffix for the translator extension tags
def suffixize(lang)
lang.blank? ? "" : "_#{lang}"
end
View
37 app/models/translator_tags.rb
@@ -31,7 +31,7 @@ module TranslatorTags
title = page.title
config_content = page.render_part(:config)
unless config_content.blank?
- lang = request.language
+ lang = request.language.split('-').first
config = YAML::load(config_content)
config = (config.blank? || config['translator'].blank?) ? {} : config['translator']
if config[lang]
@@ -61,11 +61,38 @@ module TranslatorTags
req = tag.globals.page.request
page = tag.locals.page
- suffix = req.suffixize(req.language)
+ suffix = req.suffixize(req.language.split('-').first)
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|
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
@@ -81,15 +108,19 @@ module TranslatorTags
contextual = boolean_attr['contextual', true]
if inherit and contextual
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)
else
+ logger.error("\n\nfound the part with the name #{part_name}\n\n")
part = part_page.part(part_name)
end
page.render_snippet(part) unless part.nil?
else
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)
else
+ logger.error("\n\nfound the part with the name #{part_name}\n\n")
part_page.render_part(part_name)
end
end
View
16 translator_extension.rb
@@ -8,17 +8,17 @@ class TranslatorExtension < Radiant::Extension
# still plenty of work that needs to be done on this
@@defaults = {
- :lang => 'en'
+ :language => 'en-US'
}
cattr_accessor :defaults
define_routes do |map|
- map.connect 'language/set/:lang', :controller => 'language', :action => 'set_lang'
- map.connect ':lang/*url',
+ map.connect 'language/set/:language', :controller => 'language', :action => 'set_lang'
+ map.connect ':language/*url',
:controller => 'site',
:action => 'show_page',
:requirements => {
- :lang => /[a-zA-Z]{2}/
+ :language => /[a-zA-Z]{2}/
}
end
@@ -29,9 +29,11 @@ def activate
before_filter :set_up_lang
private
def set_up_lang
- logger.error("we're setting the language \n\n\n#{params[:lang]}\n\n\n")
- if params[:lang]
- session[:language] = params[:lang]
+ if params[:language]
+ # we want to save the four-letter language code, not just the two
+ # 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
}
Please sign in to comment.
Something went wrong with that request. Please try again.