Module: SgtnClient::Translation::Implementation

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

Instance Method Summary collapse

Instance Method Details

#get_translations(component, locale = nil) ⇒ Object



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

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 || self.locale, component)
  messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)

  { 'component' => component, 'locale' => actual_locale, 'messages' => messages } if messages
rescue StandardError => e
  SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] translations are missing. {#{component}, #{locale}}. #{e}"
  nil
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

#localeObject



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

def locale
  RequestStore.store[:locale] ||= LocaleUtil.get_fallback_locale
end

#locale=(value) ⇒ Object



85
86
87
# File 'lib/sgtn-client/api/translation.rb', line 85

def locale=(value)
  RequestStore.store[:locale] = value
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sgtn-client/api/translation.rb', line 42

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

  begin
    best_match_locale = LocaleUtil.get_best_locale(locale || self.locale, component)
    messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
    result = messages&.fetch(key, nil)
  rescue StandardError => e
    SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] translation is missing. {#{key}, #{component}, #{locale}}. #{e}" }
    result = nil
  end

  if result.nil?
    return key unless block_given?

    result = yield
    return if result.nil?
  end

  if kwargs.empty?
    result
  else
    result.localize(actual_locale) % kwargs
  end
end