Getting strings from i18n YAML file? - ruby-on-rails

I have some view code:
<span data-something="[<%= t('.asd') %>, <%= t('.dsf') %>]></span>
I use this code to get some dynamic strings translated into the view. My YAML is something like:
en:
feature:
asd: "Asdddd"
dsf: "adasdsadasda"
Is there a way I can use to dynamically get all the "features" from the YAML by locale and put it in the data-attribute?

This is pretty simple to do:
I18n.translate('feature').values.join(', ')
You'll end up with a string "Asdddd, adasdsadasda".

I think it's as easy as t('feature') to get the hash, you might want to just have the values so could you try t('feature').keys?

Related

How can I put CSS style in my I18n locale?

I have a following translation:
welcome: Hello there, %{general_kenobi}
Now I know, that I can put HTML tags in the translation, like <em> or so. But what if I want this general_kenobi to have my custom style? How do I implement it? I've tried to google it, but didn't find anythin useful (or maybe I'm just bad at googling). Thank in advance!
If you want to add something fancy like this to your locale you must add the _html extension to the key.
From the docs
Keys with a '_html' suffix and keys named 'html' are marked as HTML
safe. When you use them in views the HTML will not be escaped.
en:
welcome_html: "<b>Bolded text</b>"
then just the regular stuff in your views
<%= t('welcome_html')

Passing raw HTML to haml file

Is it possible to pass pre formatted HTML to the haml file. For example I pass a variable such as:
my_text = "<b>this is bold</b>"
Then in my haml file:
%p
=#my_text
I was hoping it would display This is bold
But it just returns the original string and ignores the tags surrounding "this is bold"
The goal is to highlight certain key words("one" and "two" in this example), here's a better example:
#my_text = "This <b>one</b> plus <b>one</b> is a total of <b>two</b>"
Not sure what you want to achieve, but i'd recommend that you keep your markup in the haml and plug in your copy in the instance variable like this.
MyTextController.rb
#my_text = this is bold
my_text.html.haml
%b
= #my_text
Edit after further clarification.
You can use sanitize helper for this.
my_text.html.haml
%p
= sanitize(#my_text, tags: %w(b))
Ahh I figured it out, looks like you can do:
%p
= raw #my_text

Have trouble implement the variable loop in haml file

I use ruby on rails to get some variables and suppose to send them in email format to some email address.
%div
- #msg.each do |line|
%p = "#{line}"
%br
The msg is the string array I passed in and would like to get each element in separate line. How can I achieve that. The above code won't work.
One of the key things of coding HAML is you really are only going to have one item per line, plus you automatically get a div tag as the default tag. So you might as well add a class to your div to differentiate it. Additionally, make sure your indentation is correct:
.messages
- #msg.each do |line|
%p= line
You don't really need a br because the p will break for you, and following a tag with '=' will automatically give you the interpolation.

How do I use HAML in a dynamic link?

I am trying to create a link using HAML which looks like the this
=link_to("Last updated on<%=#last_data.date_from.month %>",'/member/abc/def?month={Time.now.month}&range=xyz&year={Time.now.year}')
It is not taking the Ruby code and it is displaying that as a string
Last updated on<%=#last_data.date_from.month %>
and in the URL as well it is not taking the function Time.now.month or Time.now.year .
How do I pass Ruby code in URL and in the string ?
You should probably use something like this:
= link_to("Last updated on #{#last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}")
Note that in the second string, it's necessary to change the ' to ". Also if the link text is getting long, you can use something like this:
= link_to("/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}") do
Last updated on #{#last_data.date_from.month}
Everything after the = in HAML is a Ruby expression. Ruby doesn't interpolate strings the way HAML does, it has own way of such interpolation.
In Ruby, when you want to have string value of some variable inside another string, you could do.
"Some string #{Time.now}"
So, it should be:
= link_to "Last updated on #{#last_data.date_from.month}", "/member/abc/def?month=#{Time.now.month}&range=xyz&year=#{Time.now.year}"
A simple example with easy syntax:
link_to "Profile #{rubycode}", "profile_path(#profile)/#{ruby_code}", class: "active"

CSS style time_ago_in_words rails

I was wondering what would be the best solution for customising the rails helper: time_ago_in_words, so that I could do things like:
<span class="one">4</span>
<span class="two">hours</span>
<span class="three">ago</span>
or what the layout looks like by default?
Thanks
I would just create a helper that splits the result of time_ago_in_words on spaces and returns the array of words. Then you can format them however you want.
Instead of the method time_ago_in_words, you can use distance_of_time_in_words, which accepts locale in the option array: create a new locale where you can define "hours" with "hours", etc... and give it to distance_of_time_in_words. It should work, but I'm not sure how will be handled the HTML tags.

Resources