Module: SgtnClient::Translation::Implementation

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

Instance Method Summary collapse

Instance Method Details

#get_translation!(key, component, locale) ⇒ Object



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

def get_translation!(key, component, locale)
  [get_bundle!(component, locale)[key], locale]
end

#get_translations(component, locale = nil) ⇒ Object



75
76
77
78
79
80
# File 'lib/sgtn-client/api/translation.rb', line 75

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



82
83
84
85
86
87
88
89
# File 'lib/sgtn-client/api/translation.rb', line 82

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

  best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
  messages = get_bundle!(component, best_match_locale)

  { 'component' => component, 'locale' => best_match_locale, 'messages' => messages } if messages
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



46
47
48
49
50
51
# File 'lib/sgtn-client/api/translation.rb', line 46

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sgtn-client/api/translation.rb', line 55

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

  begin
    best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
    result, actual_locale = get_translation!(key, component, best_match_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 : result.localize(actual_locale) % kwargs
end