所述
我想用英语编程,但我的输出路由必须是另一种语言。
今天我使用以下方法,我没有得到index
的动作,new
和edit
仍然缺少翻译。我试过使用test: one: other:
,但没有成功。
resource :activities, path: I18n.t('routes.test')
# config/routes.rb
activities POST /teste(.:format) activities#create
new_activities GET /teste/new(.:format) activities#new
edit_activities GET /teste/edit(.:format) activities#edit
GET /teste(.:format) activities#show
PATCH /teste(.:format) activities#update
PUT /teste(.:format) activities#update
DELETE /teste(.:format) activities#destroy
# config/locales/routes.yml
fr:
routes:
test: "teste"
我怎么才能做到呢?
你应该创建一个路由。以routes:为主键的Yml文件,你想要更深一层翻译的路由
# config/locales/routes.yml
routes:
activities: "something_else"
并在routes.rb
中这样调用它resources :activities, path: I18n.t('activities', locale: :routes)
那么你就独立于I18n.locale
中的语言集了输出如下:
Prefix Verb URI Pattern Controller#Action
activities GET /something_else(.:format) activities#index
POST /something_else(.:format) activities#create
new_activity GET /something_else/new(.:format) activities#new
edit_activity GET /something_else/:id/edit(.:format) activities#edit
activity GET /something_else/:id(.:format) activities#show
PATCH /something_else/:id(.:format) activities#update
PUT /something_else/:id(.:format) activities#update
DELETE /something_else/:id(.:format) activities#destroy
编辑:忽略你也想翻译新的和编辑,你可以这样做:
scope(path_names: { new: I18n.t('new', locale: :routes), edit: I18n.t('edit', locale: :routes) }) do
resources :activities, path: I18n.t('activities', locale: :routes)
end
或者你可以在路由中重写它。如http://guides.rubyonrails.org/routing.html#translated-paths