如何在rails上的.yml本地化文件中打破行?

Roo*_* V. 26 yaml localization ruby-on-rails

我有一个带有一些本地化的terms.en.yml文件,例如:

en:
  devise:
    registrations:
      terms:
        text: 'This agreement was written in English (US). To the extent any translated version of this agreement conflicts with the English version, the English version controls.  Please note that Section 16 contains certain changes to the general terms for users outside the United States.\n\ Some new line'
Run Code Online (Sandbox Code Playgroud)

我怎么能在那里打破一条线或创建一个段落?

这里有一些信息,但它对我没有帮助,我做错了什么.http://yaml.org/spec/1.1/#b-paragraph-separator

jdo*_*doe 52

这对我有用:

en:
  hello: "Hello world"
  bye: |
    Bye!
    See you!
  error: >
    Something happend.
    Try later.
Run Code Online (Sandbox Code Playgroud)

用法:

irb(main):001:0> I18n.t 'bye'
=> "Bye!\nSee you!\n"
irb(main):002:0> I18n.t 'error'
=> "Something happend. Try later.\n"
Run Code Online (Sandbox Code Playgroud)

  • 这样,您只能在YAML代码中使用换行符,但不能在HTML输出中使用换行符.使用Rails 4.2.6在普通的.yml文件中尝试这个并且它不起作用,两者都是"|" 和">"选项.请参阅下面的答案以了解更多详情,请考虑将其标记为正确答案.谢谢. (3认同)
  • 使用`simple_format`帮助器? (2认同)

Die*_*o D 12

我认为你的答案意味着:

"如何使用断行和make rails输出(HTML)在.yml(YAML)文件中编写短语/段落包含那些断行?"

因此,对我来说,目标是在.yml(YAML)文件中使用分隔线编写一个短语,以便轻松理解最终输出,然后在我们的HTML中生成由Rails生成的精确输出.

为此,我们需要在.yml文件和.html.erb | .slim文件中采取一些简单的预防措施.

这是我怎么做的.

welcome_page.html.slim

h4.white = t('welcome_html')
Run Code Online (Sandbox Code Playgroud)

请注意_html最后一部分.如果您的翻译密钥以_html结尾,则您可以免费转义.资料来源:http://guides.rubyonrails.org/i18n.html#using-safe-html-translations

en.yml

en:
  welcome_html: |
    Welcome on StackOverflow!</br>
    This is your personal dashboard!
Run Code Online (Sandbox Code Playgroud)

所以现在在我们的.yml文件中,我们可以使用</ br>将被转义的HTML标记. 为了便于阅读和理解将如何出现我们想要使用的输出"|" yaml选项让我们在.yml文件中也有断行.但提醒一下"|" 这里的选项只适合我们,对开发人员来说更具人性化和友好性."|" YAML选项不会影响输出!我们也可以使用其他类似的YAML选项:

en:
  welcome_html: >
    Welcome on StackOverflow!</br>
    This is your personal dashboard!
Run Code Online (Sandbox Code Playgroud)

要么:

en:
  welcome_html:
    Welcome on StackOverflow!</br>
    This is your personal dashboard!
Run Code Online (Sandbox Code Playgroud)

要么:

en:
  welcome_html: "Welcome on StackOverflow!</br>This is your personal dashboard!"
Run Code Online (Sandbox Code Playgroud)

它们都产生相同的输出,所以现在你的HTML页面将反映该断行.

  • `_html` 自由转义是一个金块! (2认同)

cla*_*ans 7

如果要在代码中包含换行符,而不是在输出中:

en:   
    devise:
    registrations:
      terms:
        text: >
          This agreement was written in English (US). 
          To the extent any translated version of this 
          agreement conflicts with the English version, 
          the English version controls.  Please note 
          that Section 16 contains certain changes to 
          the general terms for users outside the 
          United States.

          Some new line
Run Code Online (Sandbox Code Playgroud)