Rails Engine 多语言路由如何写?
默认语言en, 路由前缀:
- en => /
- es => /es
- fr => /fr
- it => /it …
路由 routes
1
2
3
4
5
6
7
# config/routes.rb
Rails.application.routes.draw do
# 正则最后是重点,用来配首页
scope '(:locale)', locale: /es|fr|it|de|ja|zh|/ do
mount Blorgh::Engine, at: '/'
end
end
控制器 controller
1
2
3
4
5
6
7
8
9
10
11
12
13
module Blorgh
class ApplicationController < ActionController::Base
before_action :set_locale
private
def set_locale
I18n.locale = if params[:locale].present?
params[:locale]
else
I18n.default_locale
end
end
end
end
本文由作者按照 CC BY 4.0 进行授权