From e2edd16826c5bffab60240d66cc36b39d9a900f0 Mon Sep 17 00:00:00 2001 From: Phil Burrows Date: Thu, 31 Jan 2008 23:45:19 +0000 Subject: [PATCH] updating some documentation and also refactoring some things --- README | 13 ++++++- app/models/translator_tags.rb | 66 ++++++++++++++++------------------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/README b/README index 473bce4..4eb2a5a 100644 --- a/README +++ b/README @@ -3,7 +3,7 @@ Created By: Phil Burrows, January 2008 peburrows@gmail.com http://philburrows.com -http://svn.philburrows.com/radiant-extensions/translator +http://dev.philburrows.com/svn/radiant-extensions/translator/trunk ------------------------- @@ -46,3 +46,14 @@ Then, in your layout, simply use the translator tags the same way you would use each of these will render the appropriate page part, determined by the browser's Accept-Language + +You can also take advantage of the tag which will render the normal page title by default, +but will first check for a config page part that specifies other titles for the different languages. +Sample config page part --> + +translator: + es: + title: éste es el título + de: + title: dieses ist der titel + diff --git a/app/models/translator_tags.rb b/app/models/translator_tags.rb index 29a512c..663297f 100644 --- a/app/models/translator_tags.rb +++ b/app/models/translator_tags.rb @@ -15,14 +15,24 @@ module TranslatorTags *Usage:*
+ + *Then in a config page part do the following* +

+    translator:
+      es:
+        title: éste es el título
+      de:
+        title: dieses ist der titel
+    
} tag 'translator:title' do |tag| page = tag.locals.page title = page.title - config_content = page.render_part(:translator_config) + config_content = page.render_part(:config) unless config_content.blank? lang = language(tag) config = YAML::load(config_content) + config = (config.blank? || config['translator'].blank?) ? {} : config['translator'] if config[lang] config[lang]['title'].blank? ? page.title : config[lang]['title'] else @@ -48,25 +58,8 @@ module TranslatorTags } tag 'translator:content' do |tag| page = tag.locals.page - lang = language(tag) - suffix = lang.blank? ? "" : "_#{lang}" - # # 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}" + + suffix = suffixize(language(tag)) base_part_name = tag_part_name(tag) part_name = base_part_name + "#{suffix}" @@ -104,23 +97,26 @@ module TranslatorTags def language(tag) # this is where we need to grab the Accept-Language - request = tag.globals.page.request - lang = request.env['HTTP_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) + # 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 - # and now the part's suffix will be determined by the accept-language - # suffix = lang.match(/^en/i) ? "" : "_#{lang}" - lang.match(/^en/i) ? "" : lang + def suffixize(lang) + lang.blank? ? "" : "_#{lang}" end end \ No newline at end of file