ruby-on-rails - rails 5 : How Do I Get My Error Pages in the Public Folder for Different Locales to Display Other than "en?

标签 ruby-on-rails ruby-on-rails-4 heroku error-handling internationalization

当我将我的 Rails 应用程序从 3.2.13 转换为 Rails 4 时,我问了这个问题:Rails 4 - Would Like To Do Custom Error Pages in Public Folder Using I18n .我发现我所要做的就是为每个错误代码和语言环境创建错误文件。在我的 Rails 4 应用程序中,我有 404.en.html , 404.fr.html , 500.en.html 500.fr.html 在我的公共(public)文件夹中。他们按预期工作。

我最近将我所有的 Rails 应用程序都转换为 Rails 5,但直到现在还没有检查它是否仍然有效。我重新设计了我的一个应用程序并升级到 Rails 5.1.1。我将错误页面重新创建为我的新网站格式。在将更改部署到 Heroku 之前,我正在尝试在 localhost 中测试我的错误页面。我在 /config/environments/development.rb 中有以下临时配置所以我可以看到我的错误页面。

config.consider_all_requests_local = false
config.action_dispatch.show_exceptions = true

当我用英文显示我的网站并强制出错时,英文错误页面 404.en.html 按预期显示。但是,当我显示页面的法语版本并强制显示相同的错误时,显示的是英文错误页面,而不是法语错误页面 404.fr.html .我多次验证法语错误文件中的文本是法语,而不是英语。

我一直在搜索 Stack Overflow 和一般的网络,但我没有找到任何关于如何为 Rails 5 执行此操作的信息。几乎所有这些信息都适用于 Rails 2、3 或 4。

我决定部署到 Heroku,看看问题是否与我使用 localhost 的测试有关。不幸的是我有同样的问题。

我再次搜索并看到在 Rails 文档中他们使用救援页面,所以我在 Guides 网站上搜索了 5.0 并找到了 following关于简要提到救援文件的本地化 View 。

应用程序 Controller
before_action :set_locale

def default_url_options(options={}) 
  { :locale => I18n.locale }
end

private

  def set_locale
    I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || I18n.default_locale || 'en'
    cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale
  end

这是我的日志,语言环境为“es”

在 2017-08-12 10:00:34 -0500 开始为 127.0.0.1 获取“/es”
PagesController#home 处理为 HTML
参数:{"locale"=>"es"}
es

以下是使用 locale es 显示英文错误页面的方式。当我单击此页面上的任何链接时,它会显示我期望的网站的西类牙语版本,因为语言环境是 es。

enter image description here

这是我的公共(public)文件夹中的 es 错误页面。您将看到文本是西类牙语,而不是英语。

enter image description here

最佳答案

将国际化添加到错误页面的一种方法是将自定义路由添加到 Controller 并使用 config/locales 上的 yml 文件。实现不同语言的内容。

首先,定义错误路由并设置一个 Controller 来处理这些路由

配置/路由.rb

AwesomeApp::Application.routes.draw do
  # custom error routes
  match '/404' => 'errors#not_found', :via => :all
  match '/422' => 'errors#unprocessable_entity', :via => :all
  match '/500' => 'errors#internal_server_error', :via => :all

  ...
end

应用程序/ Controller /errors_controller.rb
class ErrorsController < ApplicationController
  def internal_server_error
    render(:status => 500)
  end

  def not_found
    render(:status => 404)
  end

  def unprocessable_entity
    render(:status => 422)
  end
end

然后,为不同的语言添加 I18n 定义配置。
例如,对于英语和法语:

config/locales/en.yml
en:
  page_not_found: "The page you were looking for doesn't exist."

配置/语言环境/fr.yml
fr:
  page_not_found: "La page que vous recherchiez n'existe pas."

然后,在ErrorController的相应 View 文件上使用它的 Action 。基于你I18n.locale值, View 将被翻译成所需的语言。

app/views/errors/not_found.html.erb
<div class="container">
  <div class="row">
    <div class="align-center">
      <h3><%= t('page_not_found') %></h3>
    </div>
  </div>
</div>

与为公用文件夹上的不同错误维护不同文件相比,这是一种设置错误页面的更简洁的方法。

更新

正如建议的那样,config/application.rb 上需要此配置为此:
config.exceptions_app = self.routes

关于ruby-on-rails - rails 5 : How Do I Get My Error Pages in the Public Folder for Different Locales to Display Other than "en?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44725689/

相关文章:

ruby-on-rails - Rails 帮助 ActionView::Template::Error 提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换

ruby-on-rails - 错误 : column "pitch_count" does not exist when it exists in an alias

heroku - WebSocket 推送数据库更新

ruby-on-rails - 我应该在哪个 HTML5 标签中包装 Rails flash 通知?

ruby-on-rails - friendly_id slug 在更新时没有改变

ruby-on-rails-4 - ActiveAdmin 嵌套表单多选

ruby-on-rails - Rails 的常用 XSS 保护有多防弹?

ruby-on-rails - Heroku 应用程序在启动时无提示地失败

ruby-on-rails - 功能得分属性可根据无法与 Elasticsearch 和 rails 配合使用的点击对搜索进行排名

ruby-on-rails - 在 Ruby on Rails 中的一个页面上多次重用部分