Class: SgtnClient::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/sgtn-client/api/translation.rb

Direct Known Subclasses

T

Class Method Summary collapse

Class Method Details

.getString(component, key, locale) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sgtn-client/api/translation.rb', line 17

def self.getString(component, key, locale)
  SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
  str = getTranslation(component, key, locale)
  if str.nil?
    str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
    if str.nil?
      SgtnClient.logger.debug "[Translation][getString] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
    end
  end
  str
end

.getString_f(component, key, args, locale, *optionals) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sgtn-client/api/translation.rb', line 44

def self.getString_f(component, key, args, locale, *optionals)
   SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
   s = getString(component, key, locale, *optionals)
   if s.nil?
    return nil
   end
   if args.is_a?(Hash)
    args.each do |source, arg|
      s.gsub! "{#{source}}", arg
    end
   elsif args.is_a?(Array)
    s = sprintf s % args
   end
   return s
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sgtn-client/api/translation.rb', line 29

def self.getString_p(component, key, plural_args, locale)
  SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
  str = getTranslation(component, key, locale)
  if str.nil?
    str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
    if str.nil?
      SgtnClient.logger.debug "[Translation][getString_p] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
      return nil
    end
    str.to_plural_s(:en, plural_args)
  else
    str.to_plural_s(locale, plural_args)
  end
end

.getStrings(component, locale) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sgtn-client/api/translation.rb', line 60

def self.getStrings(component, locale)
 SgtnClient.logger.debug "[Translation][getStrings]component=#{component}, locale=#{locale}"
 locale = SgtnClient::LocaleUtil.get_best_locale(locale)
 items = get_cs(component, locale)
 default = SgtnClient::Config.configurations.default
 if items.nil? || items["messages"] == nil
   items = {}
   s = SgtnClient::Source.getSources(component, default)
   if s.nil?
     SgtnClient.logger.error "[Translation][getStrings] Missing component: #{component}, locale: #{locale}"
   else
     default_component, value = s.first
     items["component"] = component
     items["messages"] = value
     items["locale"] = 'source'
   end
 end
 return items
end