aboutsummaryrefslogtreecommitdiffstats
path: root/lib/i18n_fixes.rb
blob: 9d7a999dd4813aae3835594664807a11401606e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# override behaviour in fast_gettext/translation.rb
# so that we can interpolate our translation strings nicely

def _(key, options = {})
  translation = FastGettext._(key) || key
  gettext_interpolate(translation, options)
end

INTERPOLATION_RESERVED_KEYS = %w(scope default)
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/

def gettext_interpolate(string, values)
  return string unless string.is_a?(String)
  if values.is_a?(Hash)
    string.gsub(MATCH) do
      escaped, pattern, key = $1, $2, $2.to_sym
      
      if escaped
        pattern
      elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
        raise ReservedInterpolationKey.new(pattern, string)
      elsif !values.include?(key)
        raise MissingInterpolationArgument.new(pattern, string)
      else
        values[key].to_s
      end
    end
  else
    reserved_keys = if defined?(I18n::RESERVED_KEYS) # rails 3+
                      I18n::RESERVED_KEYS
                    else
                      I18n::Backend::Base::RESERVED_KEYS
                    end

    string % values.except(*reserved_keys)
  end
end