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



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

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

  locale = locale.nil? ? self.locale : SgtnClient::LocaleUtil.get_best_locale(locale)
  items = get_bundle(component, locale)
  if items.nil? && !LocaleUtil.is_source_locale(locale)
    items = get_bundle(component, LocaleUtil.get_source_locale)
    locale = LocaleUtil.get_source_locale
  end

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



83
84
85
# File 'lib/sgtn-client/api/translation.rb', line 83

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

#locale=(value) ⇒ Object



87
88
89
# File 'lib/sgtn-client/api/translation.rb', line 87

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

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

raise error when translation is not found



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

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

  locale = locale.nil? ? self.locale : SgtnClient::LocaleUtil.get_best_locale(locale)

  result = get_bundle(component, locale)&.fetch(key, nil)
  if result.nil? && !LocaleUtil.is_source_locale(locale)
    locale = LocaleUtil.get_source_locale
    result = get_bundle(component, locale)&.fetch(key, nil)
  end

  if result.nil?
    return key unless block_given?

    result = yield
    return if result.nil?
  end

  if kwargs.empty?
    result
  else
    locale = result.locale if result.is_a?(SgtnClient::StringUtil)
    result.localize(locale) % kwargs
  end
end