Module: SgtnClient::Translation::Implementation

Included in:
Sgtn, SgtnClient::Translation
Defined in:
lib/sgtn-client/api/translation.rb

Instance Method Summary collapse

Instance Method Details

#get_translations(component, locale = nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/sgtn-client/api/translation.rb', line 71

def get_translations(component, locale = nil)
  get_translations!(component, locale)
rescue StandardError => e
  SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] {#{component}, #{locale}}. #{e}"
  nil
end

#get_translations!(component, locale = nil) ⇒ Object



78
79
80
81
82
83
# File 'lib/sgtn-client/api/translation.rb', line 78

def get_translations!(component, locale = nil)
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}" }

  picked_locale = pickup_locale(locale || SgtnClient.locale, component)
  get_bundle!(component, picked_locale)
end

#getString(component, key, locale) ⇒ Object

DEPRECATED: Please use Sgtn:translate instead.



10
11
12
13
# File 'lib/sgtn-client/api/translation.rb', line 10

def getString(component, key, locale)
  SgtnClient.logger.debug { "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}" }
  translate(key, component, locale) { nil }
end

#getString_f(component, key, args, locale, *_optionals) ⇒ Object

DEPRECATED: Please use Sgtn:translate instead.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sgtn-client/api/translation.rb', line 22

def getString_f(component, key, args, locale, *_optionals)
  SgtnClient.logger.debug { "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}" }
  s = translate(key, component, locale) { nil }
  return nil if s.nil?

  if args.is_a?(Hash)
    args.each do |source, arg|
      s.gsub! "{#{source}}", arg
    end
  elsif args.is_a?(Array)
    s = s % args
  end
  s
end

#getString_p(component, key, plural_args, locale) ⇒ Object

DEPRECATED: Please use Sgtn:translate instead.



16
17
18
19
# File 'lib/sgtn-client/api/translation.rb', line 16

def getString_p(component, key, plural_args, locale)
  SgtnClient.logger.debug { "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}" }
  translate(key, component, locale, **plural_args) { nil }
end

#getStrings(component, locale) ⇒ Object

DEPRECATED: Please use Sgtn:get_translations instead.



38
39
40
# File 'lib/sgtn-client/api/translation.rb', line 38

def getStrings(component, locale)
  get_translations(component, locale)
end

#translate(key, component, locale = nil, **kwargs, &block) ⇒ Object Also known as: t



42
43
44
45
46
47
# File 'lib/sgtn-client/api/translation.rb', line 42

def translate(key, component, locale = nil, **kwargs, &block)
  translate!(key, component, locale, **kwargs, &block)
rescue StandardError => e
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] {#{key}, #{component}, #{locale}}. #{e}" }
  key
end

#translate!(key, component, locale = nil, **kwargs, &block) ⇒ Object Also known as: t!

raise error when translation is not found



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sgtn-client/api/translation.rb', line 51

def translate!(key, component, locale = nil, **kwargs, &block)
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}" }

  begin
    picked_locale = pickup_locale(locale || SgtnClient.locale, component)
    result, actual_locale = get_string!(key, component, picked_locale)
  rescue StandardError => e
    raise e if block.nil?
  end
  if result.nil?
    raise SingletonError, 'translation is missing.' if block.nil?

    result = block.call
    return if result.nil?
  end

  kwargs.empty? ? result : interpolate(result, actual_locale, **kwargs)
end