Sha256: 34d4ca589ac796d926655ad24e62b4ff7d2db2eac159083d335ae50a0bf94e7d

Contents?: true

Size: 1.78 KB

Versions: 25

Compression:

Stored size: 1.78 KB

Contents

module Locomotive
  module Steam

    class TranslatorService

      attr_accessor_initialize :repository, :current_locale

      # Return the translation described by a key.
      #
      # @param [ String ] key The key of the translation.
      # @param [ Hash ] options This includes the following options: count, locale (The locale we want the translation in), scope (If specified, instead of looking in the translations, it will use I18n instead)
      #
      # @return [ String ] the translated text or nil if not found
      #
      def translate(input, options = {})
        locale  = options['locale'] || self.current_locale
        scope   = options.delete('scope')

        if scope.blank?
          input = "#{input}_#{pluralize_prefix(options['count'])}" if options['count']

          values = repository.by_key(input).try(:values) || {}

          # FIXME: important to check if the returned value is nil (instead of nil + false)
          # false being reserved for an existing key but without provided translation)
          if (translation = values[locale.to_s]).present?
            _translate(translation, options)
          else
            Locomotive::Common::Logger.warn "Missing translation '#{input}' for the '#{locale}' locale".yellow
            ActiveSupport::Notifications.instrument('steam.missing_translation', input: input, locale: locale)
            input
          end
        else
          I18n.t(input, scope: scope.split('.'), locale: locale)
        end
      end

      private

      def _translate(string, options)
        ::Liquid::Template.parse(string).render(options)
      end

      def pluralize_prefix(count)
        case count.to_i
        when 0 then 'zero'
        when 1 then 'one'
        when 2 then 'two'
        else 'other'
        end
      end

    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.4.1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.4.0.rc1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.4.0.pre.rc.1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.3.0 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.3.0.rc2 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.1.2 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.2.1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.3.0.rc1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.2.0 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.2.0.rc3 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.2.0.rc2 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.2.0.rc1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.2.0.beta1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.1.1 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.1.0 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.1.0.rc3 lib/locomotive/steam/services/translator_service.rb