Class: SgtnClient::TranslationLoader::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/sgtn-client/loader/source.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Source

Returns a new instance of Source.



16
17
18
# File 'lib/sgtn-client/loader/source.rb', line 16

def initialize(config)
  @source_bundle_path = Pathname.new(config['source_bundle'])
end

Instance Method Details

#available_bundlesObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sgtn-client/loader/source.rb', line 42

def available_bundles
  SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}]"

  @available_bundles ||= begin
    @source_bundle_path.children.select(&:directory?).reduce(Set.new) do |bundles, component|
      component.glob('**/*.{yml, yaml}') do |_|
        bundles << Common::BundleID.new(component.basename.to_s, SgtnClient::LocaleUtil.get_source_locale)
        break bundles
      end || bundles
    end
  end
end

#load_bundle(component, locale = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sgtn-client/loader/source.rb', line 20

def load_bundle(component, locale = nil)
  return if locale && locale != CONSTS::REAL_SOURCE_LOCALE # return when NOT querying source

  SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component=#{component}"

  total_messages = {}

  (@source_bundle_path + component).glob('**/*.{yml, yaml}') do |f|
    bundle = YAML.load(File.read(f))
    messages = bundle&.first&.last # TODO: Warn about inconsistent source locale
    if messages.is_a?(Hash)
      total_messages.merge!(messages)
    else
      SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] invalid bundle data in #{f}"
    end
  end

  raise SgtnClient::SingletonError, "no local source messages for component #{component}" if total_messages.empty?

  total_messages
end