X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=lib%2Fmcollective%2Fvendor%2Fi18n%2Flib%2Fi18n%2Finterpolate%2Fruby.rb;fp=lib%2Fmcollective%2Fvendor%2Fi18n%2Flib%2Fi18n%2Finterpolate%2Fruby.rb;h=29b28146ffb211cc6d2076b6525b256e25d98843;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/lib/mcollective/vendor/i18n/lib/i18n/interpolate/ruby.rb b/lib/mcollective/vendor/i18n/lib/i18n/interpolate/ruby.rb new file mode 100644 index 0000000..29b2814 --- /dev/null +++ b/lib/mcollective/vendor/i18n/lib/i18n/interpolate/ruby.rb @@ -0,0 +1,31 @@ +# heavily based on Masao Mutoh's gettext String interpolation extension +# http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb + +module I18n + INTERPOLATION_PATTERN = Regexp.union( + /%%/, + /%\{(\w+)\}/, # matches placeholders like "%{foo}" + /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%.d" + ) + + class << self + def interpolate(string, values) + raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ RESERVED_KEYS_PATTERN + raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash) + interpolate_hash(string, values) + end + + def interpolate_hash(string, values) + string.gsub(INTERPOLATION_PATTERN) do |match| + if match == '%%' + '%' + else + key = ($1 || $2).to_sym + value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(values, string)) + value = value.call(values) if value.respond_to?(:call) + $3 ? sprintf("%#{$3}", value) : value + end + end + end + end +end