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

5

34

I have a terms.en.yml file with some localization, for example:

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.\n\ Some new line'

How could i break a line or create a paragraph there?

Here`s some info but it did not helped to me,i had been doing something wrong. http://yaml.org/spec/1.1/#b-paragraph-separator

Average answered 11/6, 2012 at 15:3 Comment(1)
As you are using Rails you need to use some precautions both on Rails and YAML side. See my answer below to more details and please consider to mark it as correct answer. Thanks.Mama
N
61

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"
Nutbrown answered 11/6, 2012 at 15:21 Comment(2)
This sets the correct string, but devise does not create HTML <br>s, so it has no effect?Muddle
In this way you only have linebreaks in the YAML code, but not in the HTML output. Tried this in a normal .yml file with Rails 4.2.6 and it doesn't work, both "|" and ">" options. See my answer below to more details and please consider to mark it as correct answer. Thanks.Mama
M
32

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 break lines to easily understand the final output and then have that exact output also in our HTML, generated by Rails.

For do that we need some easy precautions both on .yml file and in our .html or .erb or .slim file (based on what you use).

Below how I do it.

welcome_page.html.slim

h4 = t('welcome_html')

Here note the _html suffix. If your translation key ends with _html suffix, 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. To also easily read and understand how will appear the output we would like to use the | yaml special character 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.

Mama answered 23/9, 2016 at 10:29 Comment(5)
Thank you so much @Diego this worked well for me and was exactly what I needed!Pulido
the _html free escaping is a nugget!Pardew
yup </br> did the trick...accepted ans above seems didn't workNoctule
@SebastianPeter you must use <br> into YAML file along with _html suffix into HTML file as I explained. Please carefully read my answer.Mama
You are absoluteley right. I deleted my comment.Enticement
S
7

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
Screenplay answered 12/6, 2012 at 15:11 Comment(0)
A
3

Diego D's answer with using _html as YAML prefix mostly works, but when it doesn't (for example in a flash alert) you can also try using .html_safe on your localised string in the template.

So as an example:

<% flash.each do |name, msg| -%>
  <%= content_tag :div, msg.html_safe, class: name %>
<% end -%>
Adige answered 24/8, 2017 at 11:2 Comment(1)
You're a star! :)Elbertelberta
O
1

Just in case someone else is also having trouble to get Diego D's answer working:
I needed to replace the </br> with <br> or <br />.

Overstrain answered 20/5, 2019 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.