Sha256: 323dec7bdeac7ce7e355df8d754c77ffb464b22dd98b97800b0c58c5f8f9ffbc

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

require 'globalize/backend/pluralizing'
require 'globalize/locale/fallbacks'
require 'globalize/translation'

module Globalize
  module Backend
    class Static < Pluralizing
      def initialize(*args)
        add(*args) unless args.empty?
      end

      def translate(locale, key, options = {})
        result, default, fallback = nil, options.delete(:default), nil
        I18n.fallbacks[locale].each do |fallback|
          begin
            result = super(fallback, key, options) and break
          rescue I18n::MissingTranslationData
          end
        end
        result ||= default locale, default, options

        attrs = {:requested_locale => locale, :locale => fallback, :key => key, :options => options}
        translation(result, attrs) || raise(I18n::MissingTranslationData.new(locale, key, options))
      end

      protected

        alias :orig_interpolate :interpolate unless method_defined? :orig_interpolate
        def interpolate(locale, string, values = {})
          result = orig_interpolate(locale, string, values)
          translation = translation(string)
          translation.nil? ? result : translation.replace(result)
        end

        def translation(result, meta = nil)
          return unless result

          case result
          when Numeric
            result
          when String
            result = Translation::Static.new(result) unless result.is_a? Translation::Static
            result.set_meta meta
            result
          when Hash
            Hash[*result.map do |key, value|
              [key, translation(value, meta)]
            end.flatten]
          when Array
            result.map do |value|
              translation(value, meta)
            end
          else
            result
            # raise "unexpected translation type: #{result.inspect}"
          end
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
simonmenke-globalize2-0.0.1 lib/globalize/backend/static.rb
simonmenke-globalize2-0.0.4 lib/globalize/backend/static.rb
simonmenke-globalize2-0.0.5 lib/globalize/backend/static.rb
simonmenke-globalize2-0.0.6 lib/globalize/backend/static.rb
simonmenke-simonmenke-globalize2-0.0.2 lib/globalize/backend/static.rb
simonmenke-globalize2-0.0.7 lib/globalize/backend/static.rb