File: i18n_interpolation_deprecation.rb

package info (click to toggle)
rails 2.3.5-1.2%2Bsqueeze8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 18,248 kB
  • ctags: 20,944
  • sloc: ruby: 122,413; makefile: 72; sql: 43; sh: 1
file content (26 lines) | stat: -rw-r--r-- 931 bytes parent folder | download | duplicates (3)
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
# Deprecates the use of the former message interpolation syntax in activerecord
# as in "must have %d characters". The new syntax uses explicit variable names
# as in "{{value}} must have {{count}} characters".

require 'i18n/backend/simple'
module I18n
  module Backend
    class Simple
      DEPRECATED_INTERPOLATORS = { '%d' => '{{count}}', '%s' => '{{value}}' }

      protected
        def interpolate_with_deprecated_syntax(locale, string, values = {})
          return string unless string.is_a?(String) && !values.empty?

          string = string.gsub(/%d|%s/) do |s|
            instead = DEPRECATED_INTERPOLATORS[s]
            ActiveSupport::Deprecation.warn "using #{s} in messages is deprecated; use #{instead} instead."
            instead
          end

          interpolate_without_deprecated_syntax(locale, string, values)
        end
        alias_method_chain :interpolate, :deprecated_syntax
    end
  end
end