简体   繁体   中英

Rails I18n - Strings with format

I'm using an HTML template that allows the use of highlighted (bold) texts applying the following format:

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

That will be rendered as something like:

Visit details

I want to add an I18n layer to the title, but i find that in the different languages, the text highlighting order may vary:

(en) Visit details

(es) Detalles de visita

I find disgusting the idea of putting the HTML in the language files (i could like to use the 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 to handle this kind of formatted i18n texts?

There is nothing stopping you to do as the 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM