Class: Locomotive::Steam::TranslatorService

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/services/translator_service.rb

Instance Method Summary collapse

Instance Method Details

#translate(input, options = {}) ⇒ String

Return the translation described by a key.

Parameters:

  • key (String)

    The key of the translation.

  • options (Hash) (defaults to: {})

    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)

Returns:

  • (String)

    the translated text or nil if not found



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/locomotive/steam/services/translator_service.rb', line 15

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