2016-07-22 87 views
0

我使用Ruby on Rails在应用程序中创建。所有内容都是英文的,但是当我在字段中输入东西时,错误信息是德文的。如何更改此讯息的语言(英文)? 这是我的看法代码:如何更改所需错误消息的语言?

<%=form_for(@electricity_generation) do |f| %> 
<% if @electricity_generation.errors.any? %> 
<div id="error_explanation"> 
    <h2><%= pluralize(@electricity_generation.errors.count, "error") %> 
     prohibited this electricity_generation from being saved:</h2> 

    <ul> 
     <% @electricity_generation.errors.full_messages.each do |message| %> 
     <li> 
      <%=m essage %> 
     </li> 
     <% end %> 
    </ul> 
</div> 
<% end %> 
<section> 
    <h2 align="center"> 
     Chosen Scenario: 
     <%=link_to @scenario_selections, scenarios_path %> 
    </h2> 
    <br> 
    <div class="table-row-2"> 
     <div align="center"> 
      <div class="field"> 
       <div class="table"> 
        <strong>Information</strong> 
       </div> 
       <div class="table"> 
        <strong>Value</strong> 
       </div> 
       <div class="table"> 
        <strong>Positive Deviation (1=100%)</strong> 
       </div> 
       <div class="table"> 
        <strong>Negative Deviation (1=100%)</strong> 
       </div> 
       <div class="table"> 
        <%=label_tag(:annual_solar_irradiation, 'Annual Solar Irradiation (kwh/m^2):') %> 
       </div> 
       <div class="table"> 
        <%=f.text_field :annual_solar_irradiation, type: "number", required: "required", placeholder: "kWh/m^2" %> 
       </div> 
       <div class="table"> 
        <%=f.text_field :asi_max, type: "number", min: "0", max: "1", step: "0.01", required: "required"%> 
       </div> 
       <div class="table"> 
        <%=f.text_field :asi_min, type: "number", min: "0", max: "1", step: "0.01", required: "required" %> 
       </div> 
      </div> 
     </div> 
    </div> 
    <div align="center"> 
     <div class="actions"> 
      <%=f .submit %> 
     </div> 
    </div> 
</section> 
<% end %> 

这是它是如何显示:

enter image description here

这是application.rb中:

enter image description here

+0

英语应该是默认值。你能检查一下你的'config/application.rb'吗?'config.i18n.default_locale =:de'?如果它在那里(并且没有注释,你想把它拿出来;) –

+0

感谢您的快速回复,它已被取消注释。我将编辑我的application.rb。 – Frank

+0

是不是浏览器提供的输入字段数字类型的错误消息?我的理解是,这些是基于浏览器语言环境和[无法由网页控制](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation)。如果要更改语言,则必须应用自定义消息。 –

回答

0

您使用您的表单中的HTML constraint validation。您看到的错误消息是由浏览器创建的。浏览器以其安装的语言提供此错误消息。

如果需要,可以使用input标记的title属性覆盖(或添加信息;取决于浏览器)此消息。

<%=f.text_field :asi_max, type: "number", min: "0", max: "1", step: "0.01", title: "Please enter a number between 0 and 1.", required: "required" %> 

如果你想在控制这个消息是完全,则需要使用JavaScript(见this问题)。有些库(如parsleyjs)可以帮助你做到这一点。