本地化文件中的文本样式——粗体、斜体等

Styling text in localization files — bold, italic, etc

有哪些 techniques/methods 用于处理本地化字符串中强调或风格化的文本?

例如,下面这个简短的标题(4-8 个字)如何显示为斜体?

Welcome to the Machine, John.

有没有比3次查找更高效的方式,加上一个变量来定义结构?

查看 documentation 它说:

Third, it'll mark the translation as safe HTML if the key has the suffix “_html” or the last element of the key is the word “html”. For example, calling translate(“footer_html”) or translate(“footer.html”) will return a safe HTML string that won't be escaped by other HTML helper methods. This naming convention helps to identify translations that include HTML tags so that you know what kind of output to expect when you call translate in a template.

因此您需要将后缀 _html 添加到您的密钥,现在您可以在您的语言环境中使用 html。

例如,这是您的语言环境文件:

en:
  welcome:
    html: '<b>Hello!</b> This is first way to style your locales!'
  welcome_html: '<b>Hi again!</b> This is second way to style your locales!'

现在只需在您的视图中设置 I18n.t 'en.welcome.html'I18n.t 'en.welcome_html'。就这样! :)