【问题标题】:Using I18n markdown strings in HAML在 HAML 中使用 I18n 降价字符串
【发布时间】:2013-02-23 12:03:59
【问题描述】:

我在我的语言文件中本地化了 Markdown 字符串,我正在寻找一种更简洁的方式在 HAML 中执行以下操作:

#text_for_something
  :markdown
    #{ t(:text_in_markown) }

或者,等效地:

#text_for_something!= Maruku.new( t(:text_in_markown) ).to_html

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 internationalization haml markdown


    【解决方案1】:

    我知道这不是您想要的,但您可以将以下帮助程序添加到 helpers/application_helper.rb

    def render_md(key)
        Maruku.new( t(key) ).to_html
    end
    

    然后像这样在你的 HAML 中使用它:

    #text_for_something!= render_md :text_in_markown
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我最终为my Rails Tutorial sample app 的 i18n 做了类似的事情。不确定这是否重要,但我使用RDiscount 来渲染 Markdown。

      综上,我将每个i18n化的markdown文件归档在config/locales下的controller/action目录下,并确定需要在controller中渲染哪个页面.例如,对于一个简单的 About 页面,这里是 :en markdown 文件所在的位置:

      config/locales/static_pages/about/about.en.md

      About Us
      ========
      
      Some more markdown text...
      

      要渲染的文件的路径在控制器中确定并分配给@page,然后文件本身在相关的HAML部分中渲染出来:

      app/controllers/static_pages_controller.rb

      class StaticPagesController < ApplicationController
      
        before_filter :localized_page
      
        def about
          # ...
        end
      
        # ...
      
        protected
      
          def localized_page
            locale = params[:locale]
            @page = "#{Rails.root}/config/locales/#{controller_name}/"\
                    "#{action_name}/#{action_name}.#{locale}.md"
          end
      end
      

      app/views/static_pages/about.html.haml

      = render 'static_page', title: t('.about_us'), page: @page
      

      app/views/static_pages/_static_page.html.haml

      - provide(:title, title) if title
      :markdown
        #{render file: page}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-31
        • 2019-05-02
        • 1970-01-01
        • 2021-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多