代码之家  ›  专栏  ›  技术社区  ›  Glory to Russia

如何正确国际化Sinatra页面?

  •  2
  • Glory to Russia  · 技术社区  · 10 年前

    我有一个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 在上述示例中)。

    1 回复  |  直到 10 年前
        1
  •  4
  •   Wand Maker    10 年前

    在同一个 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
    

    自从 i18n gem也用于Rails,国际化的基本方面也可以从 Rails guide

    推荐文章