【问题标题】:How to add extension for rails internationalisation?如何为 Rails 国际化添加扩展?
【发布时间】:2016-04-23 16:16:30
【问题描述】:

现在我得到了适用于两种语言的 Rails 应用程序。英俄。

但应用程序现在不代表 url 中的语言:example.com/posts 用于 eng 和 rus,但我需要 example.com/ru/posts 和常规的英文版 example.com/posts

我该怎么做?

路由文件:

  root 'static_pages#home'
  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'

  get '/change_locale/:locale', to: 'settings#change_locale', as: :change_locale

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 routes internationalization


    【解决方案1】:

    Rails guides 报道这个案例并提供示例。

    # app/controllers/application_controller.rb
    def default_url_options(options = {})
      { locale: I18n.locale }.merge options
    end
    
    # config/routes.rb
    scope "(:locale)", locale: /en|ru/ do
      resources :books
    end
    
    get '/:locale' => 'your_root#page_action'
    

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多