0

I created in Application with Ruby on Rails. All is in english, but when the i enter something in the fields the error message is in german. How can I change the language (english) of this message? This is my code for the view:

<%=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 %>

This is how it is displayed:

enter image description here

This is the application.rb:

enter image description here

Frank
  • 45
  • 5
  • English should be the default. Could you check your `config/application.rb` for something like `config.i18n.default_locale = :de`? If it is in there (and uncommented, you want to take it out. ;) – Julian Kniephoff Jul 22 '16 at 13:43
  • Thanks for your quick reply, it was already uncommented. i will edit my application.rb. – Frank Jul 22 '16 at 14:00
  • Isn't that the error message provided by the browser for inputs fields of type number? My understanding is that those are based on the browser locale and [cannot be controlled by the web page](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation). You'll have to apply custom messages if you want to change the language. – Justin Ko Jul 22 '16 at 15:06

1 Answers1

0

You use HTML constraint validation in your form. The error message you see is created by the browser. The browser provides this error messages in the language it is installed.

If you want, you can override (or add information; depends on the browser) this message by using the title attribute of the input tag.

<%=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" %>

If you want to be completely in control over this message, you need to use Javascript (see also this question). There are libraries (like parsleyjs) which help you doing that.

Community
  • 1
  • 1
Robin
  • 8,162
  • 7
  • 56
  • 101