Rails 3 i18n as * not * avoiding newlines

I use i18n in my .text.haml mailer templates and I want to have a line in en.yml with a new line, but t () always escapes them even if I use html_safe or the key name suffix using _html.

Is there any way to do this?

p3_html: > You love monkeys: \n- You look like one \n- Your smell like one \n- Your account has been flagged 

In my html.haml template:

 != t('emails.post.twitter_forbidden.p3_html').html_safe 

No matter what \ n is hiding. I cannot use% br or anything else because these are text patterns. I know that I can break it down into 4 lines of i18n, but that would be very sad.

By the way, I checked, and this i18n escapes, not haml.

+6
source share
5 answers

You can do something like this:

t('emails.post.twitter_forbidden.p3_html').html_safe.gsub("\n", '<br/>')

As far as I know, this is the only way.

Edit

Actually, after some digging, I found the simple_format .

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

+8
source

A couple of options here: As mentioned above, simple_format will help. Format your yml file as follows:

  p3_html: | Some text: - Point 1 - Point 2 - Point 3 

And then use

  =simple_format t(:p3_html) 

This will give you something like

  <p>Some text <br> - Point 1 <br> - Point 2 <br> - Point 3 </p> 

Or if you want a new paragraph to appear on each line:

  p3_html: | Some text: - Point 1 - Point 2 - Point 3 

Which should give you the following:

  <p>Some text</p> <p>- Point 1</p> <p>- Point 2</p> <p>- Point 3</p> 

or something like this more flexible

  <% t(:p3_html).each_line do |line| %> <li>= |line|</li> <% end %> 

Allows you to enter various formatting:

  <li>- Point 1</li> <li>- Point 2</li> <li>- Point 3</li> 

The final option is to use an array in yaml:

  p3_html: - Some text: - - Point 1 - - Point 2 - - Point 3 <% t(:p3_html).each do |line| %> <p>= |line|</p> <% end %> 

Possibly cleaner, although I think it will play hell with hellish commas, and the advantage over this version is that you can switch between formats without having to change your yaml

+3
source

I18n just uses yaml, and yaml can have arrays. :-)

So, I would study the possibility of using this parameter.

In your yaml file you will have a key named p3_html

 # http://en.wikipedia.org/wiki/YAML#Lists_of_associative_arrays p3_html: - some text - some more text - some more more text 

And then in your haml view you will have this HAML code:

 = t('p3_html').each do |x| %p= x 

Or in one line if you prefer

 = t('p3_html').each {|x| haml_tag :p, x } 

Also remember, if you move this to a helper, you probably have to use the haml_concat helper in front of your ruby ​​variable. I'm not sure.

You will also need to adjust the relative / absolute name of your yaml translate t ('p3_html') variable for your application name.

Hope this helps!

0
source

Just, just do it to get the text in a new line:

en.yml

 long_text: | Lorem ipsum dolor sit amet. Consectetur adipisicing elit. 

application / views / sample / file.html.erb

 <%= simple_format t(:'long_text') %> 

See: http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format

0
source

I ended up using tags in locale files:

 a: b: "Some <br /> thing" 

And then in the template I have them .html_safe :

 <%= t('a.b').html_safe %> 
0
source

Source: https://habr.com/ru/post/919183/


All Articles