Class: SgtnClient::TranslationLoader::Source

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Source

Returns a new instance of Source.



11
12
13
# File 'lib/sgtn-client/loader/source.rb', line 11

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

Instance Method Details

#available_bundlesObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sgtn-client/loader/source.rb', line 37

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|
      Pathname.glob(component + '**/*.{yml, yaml}') do |_|
        bundles << Common::BundleID.new(component.basename.to_s, LocaleUtil.get_source_locale)
        break bundles
      end || bundles
    end
  end
end

#load_bundle(component, locale = nil) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sgtn-client/loader/source.rb', line 15

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 = {}

  Pathname.glob(@source_bundle_path + component + '**/*.{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 SingletonError, "no local source messages for component #{component}" if total_messages.empty?

  Common::BundleData.new(total_messages, origin: self, component: component, locale: LocaleUtil.get_source_locale)
end