Interpolation in I18n array

前端 未结 1 1199
耶瑟儿~
耶瑟儿~ 2021-02-06 13:45

I\'m using arrays in a locale file to be able to generate blocks of text in various output methods (ActionMailer templates, Prawn documents, HAML templates) that are locale-spec

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 14:05

    I think such thing is not supported natively in i18n gem, but it's doable using some meta-programming magic.

    In an initializer (say, config/initializers/i18n.rb) add the following

    I18n.backend.instance_eval do
      def interpolate(locale, string, values = {})
        if string.is_a?(::Array) && !values.empty?
          string.map { |el| super(locale, el, values) }
        else
          super
        end
      end
    end
    

    0 讨论(0)
提交回复
热议问题