Skip to content

Instantly share code, notes, and snippets.

@davesag
Created September 27, 2010 02:25
Show Gist options
  • Save davesag/598515 to your computer and use it in GitHub Desktop.
Save davesag/598515 to your computer and use it in GitHub Desktop.
i18n aware haml and erb template loader for Sinatra
helpers do
def haml(template, options = {}, *)
# template will either be the name of a template or the body of a template.
# if it's the body then it will contain a "%" symbol and so we can skip any processing
template_name = template.to_s
do_not_localise = false
if template_name.include?('%')
# it's actually the template content we have here, not a template name
super
else
# it's a template name we have here.
# note layout.haml files must never hold untranslated text
if template_name.include?('chunks/')
options[:layout] ||= false
do_not_localise = true
elsif template_name.include?('mail/')
options[:layout] ||= false
elsif is_logged_in? || template_name.include?('in/')
options[:layout] ||= :'in/layout'
end
# now if template_bits[0] is a locale code then just pass through
if do_not_localise
# "Don't bother localising chunks.
super
else
# there is no locale code in front of the template name
# now make an adjustment to the template path depending on locale.
local_template_file = "views/#{r18n.locale.code.downcase}/#{template_name}.haml"
if File.exists? local_template_file
# Found a localised template so we'll use that one
local_template = File.read(local_template_file)
return haml(local_template, options)
elsif r18n.locale.sublocales != nil && r18n.locale.sublocales.size > 0
# Couldn't find a template for that specific locale.
local_template_file = "views/#{r18n.locale.sublocales[0].downcase}/#{template_name}.haml"
if File.exists? local_template_file
# but there is a more generic language file so use that.
# note if I really wanted to I could loop through in case sublocales[0] doesn't exist but other one does.
# too complicated for now though and simply not needed. TODO: polish this up later.
local_template = File.read(local_template_file)
return haml(local_template, options)
else
# No localsied version of this template exists. Okay use the template we were supplied.
super
end
else
# That locale has no sublocales so just use the template we were supplied.
super
end
end
end
end
def erb(template, options = {}, *)
# template will either be the name of a template or the body of a template.
# if it's the body then it will contain a "%" symbol and so we can skip any processing
template_name = template.to_s
if template_name.include?('%')
# it's actually the template content we have here, not a template name
super
else
# it's a template name we have here.
# now make an adjustment to the template path depending on locale.
local_template_file = "views/#{r18n.locale.code.downcase}/#{template_name}.erb"
if File.exists? local_template_file
# Found a localised template so we'll use that one
local_template = File.read(local_template_file)
return erb(local_template, options)
elsif r18n.locale.sublocales != nil && r18n.locale.sublocales.size > 0
# Couldn't find a template for that specific locale.
local_template_file = "views/#{r18n.locale.sublocales[0].downcase}/#{template_name}.erb"
if File.exists? local_template_file
# but there is a more generic language file so use that.
# note if I really wanted to I could loop through in case sublocales[0] doesn't exist but other one does.
# too complicated for now though and simply not needed. TODO: polish this up later.
local_template = File.read(local_template_file)
return erb(local_template, options)
else
# No localsied version of this template exists. Okay use the template we were supplied.
super
end
else
# That locale has no sublocales so just use the template we were supplied.
super
end
end
end
end
@davesag
Copy link
Author

davesag commented Sep 27, 2010

This works perfectly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment