【问题标题】:How to properly internationalize a Sinatra page?如何正确国际化 Sinatra 页面?
【发布时间】:2016-02-05 05:22:32
【问题描述】:

我有一个 Sinatra route file,它显示了一些 page

<h2>Free, open song contest</h2>
<h3>Sign up</h3>
<form action="/signup" method="post">
    E-mail: <input type="text" name="email" placeholder="Your e-mail"><br/>
    Password: <input type="password" name="password"><br/>
    Repeat password: <input type="password" name="password2"><br/>
    Account type: <input type="radio" checked="true" name="type" value="fan">Fan
    <input type="radio" name="type" value="musician">Musician
</form>
<h3>Log in</h3>
<form action="/login" method="post">
    E-mail: <input type="text" name="email" placeholder="Your e-mail"><br/>
    Password: <input type="password" name="password"><br/>
</form>

如果我想将此页面中的所有字符串国际化(免费开放歌曲比赛注册电子邮件等) ,这样做的正确方法是什么?

我找到了这个i18n recipe,但它没有说明如何将国际化字符串插入到模板中(上例中的home.erb)。

【问题讨论】:

    标签: ruby internationalization sinatra


    【解决方案1】:

    在同一个documentation 页面中,它后来说:

    选择本地化的字符串/对象很容易,因为它只需要使用 来自 I18n 的标准方法

    I18n.t(:token) 
    I18n.l(Time.now)
    

    所以,你需要这样做:

    <h2>I18n.t(:contest_page_title')</h2>
    

    而且,在您的locales/en.yml 中,您将拥有:

    en:
      contest_page_title: Free, open song contest
    

    由于 Rails 中也使用了 i18n gem,所以国际化的基本方面也可以从 Rails guide 推断出来

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多