2015-11-12 17 views
0

我有一個使用多種語言的應用程序,現在我想在URL中整合語言。類似這樣的:https://example.com/en/main/carshttps://example.com/fr/main/cars等等。該應用程序完美適用於所有語言,只有URL中缺少該語言。如何將語言整合到Sinatra的URL中

我已經檢查過thisthis但我沒有成功。我會感謝任何幫助。

helpers.rb我:

def set_locale(new_locale) 
    allowed_locales = %w[ de en fr it sk ] 
    if allowed_locales.map{|l| l.to_sym }.include?(new_locale.to_sym) 
    session[:locale] = new_locale.to_sym 
    else 
    session[:locale] = default_locale 
    end 
end 
alias_method :set_lang, :set_locale 

def locale 
    session[:locale] || default_locale 
end 

def default_locale 
    :de 
end 

def t(*args) 
    I18n.locale = locale 
    I18n.t(*args) 
end 

def l(*args) 
    I18n.locale = locale 
    I18n.l(*args) 
end 

app.rb我定義是這樣的:

Class Something < Sinatra::Application 
    helpers Sinatra::Something::Helpers 
    I18n.load_path += Dir[File.join(settings.root, 'locales', '??.yml').to_s] 

    before "/*" do 
    set_locale params[:locale] if params[:locale] 
    set_locale params[:lang] if params[:lang] 
    set_locale params[:l]  if params[:l] 
    end 
end 

回答

0

給你顯示的代碼,你是無處設置您的網址的第一部分作爲語言PARAM。嘗試從您鏈接的文檔添加之前的過濾器:

before '/:locale/*' do 
    I18n.locale  =  params[:locale] 
    request.path_info = '/' + params[:splat ][0] 
end 

或者,您可以編輯您的路由以包含並命名此參數。

相關問題