def self.registered(app)
app.helpers Padrino::Contrib::AutoLocale::Helpers
app.extend ClassMethods
app.set :locales, [:en]
app.set :locale_exclusive_paths, []
@@exclusive_paths = false
app.before do
unless @@exclusive_paths.is_a?(Array)
if settings.respond_to?(:assets) and
settings.assets.respond_to?(:served) and
settings.assets.served.is_a?(Hash)
@@exclusive_paths = settings.locale_exclusive_paths + settings.assets.served.keys
else
@@exclusive_paths = settings.locale_exclusive_paths
end
end
I18n.locale = settings.locales.first
if request.path_info =~ /^\/(#{settings.locales.join('|')})\b/
I18n.locale = $1.to_sym
elsif AutoLocale.excluded_path?(request.path_info, @@exclusive_paths)
next
elsif request.path_info =~ /^\/?$/
for browser_locale in (request.env['HTTP_ACCEPT_LANGUAGE'] || '').split(",")
locale = browser_locale.split(";").first.downcase.sub('-', '_')
if settings.locales.include?(locale.to_sym)
I18n.locale = locale.to_sym
break
end
end
redirect url("/#{I18n.locale.to_s}/", false)
else
not_found
end
end
def self.padrino_route_added(route, verb, path, args, options, block)
return unless route.original_path.is_a?(String)
excluded_paths = block.binding.eval('settings').locale_exclusive_paths
return if AutoLocale.excluded_path?(route.original_path, excluded_paths)
route.path = "/:lang#{route.original_path}" unless route.original_path =~ /:lang/
end
def self.excluded_path?(path, excluded_paths)
excluded_paths.detect do |excluded_path|
if excluded_path.is_a?(Regexp)
!!excluded_path.match(path)
elsif excluded_path.is_a?(String)
path.start_with?(excluded_path.end_with?("/") ? excluded_path : "#{excluded_path}/")
end
end
end
end