How to break lines in .yml localization files on rails? How to break lines in .yml localization files on rails? ruby-on-rails ruby-on-rails

How to break lines in .yml localization files on rails?


This works for me:

en:  hello: "Hello world"  bye: |    Bye!    See you!  error: >    Something happend.    Try later.

Usage:

irb(main):001:0> I18n.t 'bye'=> "Bye!\nSee you!\n"irb(main):002:0> I18n.t 'error'=> "Something happend. Try later.\n"


I consider that your answer means:

"How to write a phrase/paragraph in .yml (YAML) file with break lines and make rails output (HTML) contain those break lines?"

So for me the goal is to write a phrase inside .yml (YAML) file with breaklines, to easily understand the final output and then have that exact output in our HTML, generated by Rails.

For do that we need some easy precautions both on .yml file and in our .html.erb|.slim file.

This how I do it.

welcome_page.html.slim

h4.white = t('welcome_html')

Please here note the _html final part. If your translation key ends with _html, you get escaping for free.Source: http://guides.rubyonrails.org/i18n.html#using-safe-html-translations

en.yml

en:  welcome_html: |    Welcome on StackOverflow!</br>    This is your personal dashboard!

So now inside our .yml file we can use < /br > HTML tag that will be escaped.For read and understand easily how will appear the output we would like to use the "|" yaml option that let us have break lines also inside .yml file. But remind that "|" option here is just for us, to be more human readable and friendly to the developer. "|" YAML option won't affect the output!Also we can use other YAML options like these:

en:  welcome_html: >    Welcome on StackOverflow!</br>    This is your personal dashboard!

or:

en:  welcome_html:    Welcome on StackOverflow!</br>    This is your personal dashboard!

or:

en:  welcome_html: "Welcome on StackOverflow!</br>This is your personal dashboard!"

They all produce the same output, so now your HTML page will reflect that break line.


If you want to have linebreaks in the code, but not in the output:

en:       devise:    registrations:      terms:        text: >          This agreement was written in English (US).           To the extent any translated version of this           agreement conflicts with the English version,           the English version controls.  Please note           that Section 16 contains certain changes to           the general terms for users outside the           United States.          Some new line