Sinatra路线和i18n

时间:2011-02-22 15:16:06

标签: ruby sinatra

set :locales, %w[en it]
set :default_locale, 'it'
set :locale_pattern, /^\/?(#{Regexp.union(settings.locales)})(\/.+)$/

helpers do
  def locale
    @locale || settings.default_locale
  end
end

before('/:locale/*') { |params| @locale = params.first } # params shouldn't be a Hash?

我无法获得以/ en /:

开头的其他页面
get '/attivita/:activity' do |activity|
  erb "attivita/#{activity.to_sym}".to_sym
end

我应该使用:locale任何地方的路线吗?感谢

1 个答案:

答案 0 :(得分:2)

您的before屏蔽在wrong number of arguments (2 for 1)上为Sinatra 1.1.3提供了{。}}。

来自How to detect language from URL in Sinatrabefore适用于我:

before do
  @locale, request.path_info = $1, $2 if request.path_info =~ settings.locale_pattern
end