Sha256: b5af767b0789de5f2cd117e5c92e746bd2ae47f446c820600f61e32c15556994

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 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 [ String ] locale The locale we want the translation in
      # @param [ String ] 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, locale, scope = nil)
        locale ||= self.current_locale

        if scope.blank?
          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?
            translation
          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

    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.0.0.rc4 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.0.0.rc3 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/services/translator_service.rb
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/services/translator_service.rb