Skip to content

Commit

Permalink
updating some documentation and also refactoring some things
Browse files Browse the repository at this point in the history
  • Loading branch information
peburrows committed Jan 31, 2008
1 parent a5c3b52 commit e2edd16
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
13 changes: 12 additions & 1 deletion README
Expand Up @@ -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

-------------------------

Expand Down Expand Up @@ -46,3 +46,14 @@ Then, in your layout, simply use the translator tags the same way you would use
<r:translator:content name="sidebar" inherit="true" />

each of these will render the appropriate page part, determined by the browser's Accept-Language

You can also take advantage of the <r:translator:title /> 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

66 changes: 31 additions & 35 deletions app/models/translator_tags.rb
Expand Up @@ -15,14 +15,24 @@ module TranslatorTags
*Usage:*
<pre><code><r:translator:title /></code></pre>
*Then in a config page part do the following*
<pre><code>
translator:
es:
title: éste es el título
de:
title: dieses ist der titel
</pre></code>
}
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
Expand All @@ -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}"
Expand Down Expand Up @@ -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

0 comments on commit e2edd16

Please sign in to comment.