Rails i18n显示变量但不显示值(翻译)

时间:2017-06-09 17:16:44

标签: ruby-on-rails ruby internationalization rails-i18n

我试图在我的rails项目中为多语言实现i18n。部分它有效,例如。在导航中,它是application.html.erb的一部分,并按如下方式实现:

<%=t :about %>

application.rb的结构如下:

require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)

module Regional
  class Application < Rails::Application
    I18n.available_locales = [:en, :de]
    I18n.default_locale = :en
  end
end

application_controller.rb如下所示:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :set_locale

  private

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

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

着陆页的主体位于另一个文件index.html.erb中,i18n的使用方式如下:

<div class="section-title text-center center">
    <div class="overlay">
      <h2><%=t :regionalTitle %></h2>
      <hr>
      <p><%=t :regionalSubTitle %></p>
    </div>
  </div>

但是网站只显示变量而不是值,而不是获得正确的翻译: i18n doesnt show value

信息更新:它甚至不会显示英文文本。

我也尝试将i18n控制器代码用于着陆页,但没有帮助

    private 
    def set_locale
        I18n.locale = params[:locale] || I18n.default_locale
      end

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

更新: 这是日志文件。首先,当我只是重新加载文件时,第二次选择英语时。

Started GET "/" for 127.0.0.1 at 2017-06-10 18:27:16 +0200
Processing by LandingpageController#index as HTML
  Rendering landingpage/index.html.erb within layouts/application
  Rendered landingpage/index.html.erb within layouts/application (19.9ms)
Completed 200 OK in 89ms (Views: 85.6ms | ActiveRecord: 0.0ms)


Started GET "/en" for 127.0.0.1 at 2017-06-10 18:27:19 +0200
Processing by LandingpageController#index as HTML
  Parameters: {"locale"=>"en"}
  Rendering landingpage/index.html.erb within layouts/application
  Rendered landingpage/index.html.erb within layouts/application (19.8ms)
Completed 200 OK in 105ms (Views: 101.9ms | ActiveRecord: 0.0ms)

Started GET "/de" for 127.0.0.1 at 2017-06-10 18:33:43 +0200
Processing by LandingpageController#index as HTML
  Parameters: {"locale"=>"de"}
  Rendering landingpage/index.html.erb within layouts/application
  Rendered landingpage/index.html.erb within layouts/application (25.9ms)
Completed 200 OK in 95ms (Views: 92.1ms | ActiveRecord: 0.0ms)

更新: 文件夹的路径和层次结构。Hierarchy and file name

1 个答案:

答案 0 :(得分:1)

似乎params[:locale]似乎是nil,因此您I18n.default_locale:en作为default_locale。因此,您看到普通文本(:en)而不是翻译文本:de)。

您可以通过发送如下所示的显式值来调试它,以确保params[:locale]nil

def set_locale
  I18n.locale = 'de' || I18n.default_locale
end