Question

I need to pluralize a text based on the number. so my translation file has this,

   en:
     people:
       one: %{count} People
       other: %{count} Peoples

The problem is that, I need to bold the number. So in my view I have this,

 raw t(.people, count: content_tag(:strong, 4)) 

Since I am including a content_tag does this make 'count' a string with <strong> 4 </strong> and pass it to the translation and returning the 'Peoples' always?

or is there any better way to do this.

Thanks

Was it helpful?

Solution

One option is to use the raw i18n output and put HTML in the i18n:

en:
 people:
   one: <strong>%{count}</strong> People
   other: <strong>%{count}</strong> Peoples

Then in your translation use:

<%= raw t(:people, count: 4) %>

It's not ideal but your use case might be simple enough where this is best solution.

Another would be to use two different translations and nest them, one for just the number wrapping it in the strong tag, and another for the pluralization of the word, passing in the strong HTML, similar to how you do links:

text_with_anchor: This is a link. %{href} to click it!

t(:text_with_anchor, href: link_to(t(:another_translation), some_path))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top