Rails I18n - Formatted Strings

I am using an HTML template that allows highlighted (bold) texts using the following format:

<h3><span class="semi-bold">Visit</span> details</h3>

      

It will display something like:

Visit for details

I want to add an I18n layer to the title, but I find that the order of text highlighting can be different in different languages:

(ru) Visit more

(s) Detalles de visita strong>

I find the idea of ​​putting HTML in language files disgusting (I could use strings in a different context):

en:
  visit_details: '<span class="semi-bold">Visit</span> details'
es:
  visit_details: 'Detalles de <span class="semi-bold">visita</span>'

      

Does Rails offer a better method for handling such i18n formatted texts?

+3


source to share


1 answer


There is nothing to stop you from following:

# view
t('menus.object_details', object: content_tag(:span, t('menus.visit'), class: 'semi-bold')).html_safe

# en.yml
en:
  menus:
    object_details: "%{object} details"
    visit: "Visit"

# some_other_locale.yml
some_other_locale:
  menus:
    object_details: "Details %{object}"
    visit: "Visit"

      



As you can see, the I18n file does not contain any hard-coded HTML element.

The trick here is how to handle masculine / feminine (/ neutral) nouns (ex: French, German).

+6


source







All Articles