Rails Internationalization (i18n): The Complete Guide

So, for English there are only two possible cases: one error or many errors (of course, there can be no errors, but in this case the message won’t be displayed at all).en: global: forms: submit: Submit messages: errors: one: "One error prohibited this feedback from being saved" other: "%{count} errors prohibited this feedback from being saved"The %{count} here is interpolation – we take the passed value and place it right into the string.Now take care of the Russian locale which has more possible cases:ru: global: forms: submit: Отправить messages: errors: one: "Не удалось сохранить отзыв!.Найдена одна ошибка:" few: "Не удалось сохранить отзыв!.Найдены %{count} ошибки:" many: "Не удалось сохранить отзыв!.Найдено %{count} ошибок:" other: "Не удалось сохранить отзыв!.Найдена %{count} ошибка:"Having this in place, just utilize these translation:<!– views/feedbacks/_form.html.erb –> <!– ….—> <%= form_with(model: feedback, local: true) do |form| %> <% if feedback.errors.any?.%> <div id="error_explanation"> <h2><%= t 'global.forms.messages.errors', count: feedback.errors.count %></h2> <!– errors….–> </ul> </div> <% end %> <!– form fields –> <% end %>Note that in this case we pass the translation key as well as the value for the count variable..Rails will take the proper translation variant based on this number..Also the value of the count will be interpolated into each %{count} placeholder.Our next stop is the _feedback.html.erb partial..Here we need to localize two strings: “Posted by…” and datetime (created_at field)..As for “Posted by…”, let’s just utilize the interpolation again:en: global: feedback: posted_by: "Posted by %{author}"ru: global: feedback: posted_by: "Автор: %{author}"<!– views/feedbacks/_feedback.html.erb –> <article> <em> <%= tag.time feedback.created_at, datetime: feedback.created_at %><br> <%= t 'global.feedback.posted_by', author: feedback.author %> </em> <p> <%= feedback.message %> </p> <hr> </article>But what about the created_at?. More details

Leave a Reply