Class: SgtnClient::TranslationLoader::LocalTranslation

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

Constant Summary collapse

BUNDLE_PREFIX =
'messages_'.freeze
BUNDLE_SUFFIX =
'.json'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ LocalTranslation

Returns a new instance of LocalTranslation.



14
15
16
# File 'lib/sgtn-client/loader/local_translation.rb', line 14

def initialize(config)
  @base_path = Pathname.new(config.translation_bundle) + config.product_name + config.version.to_s
end

Instance Method Details

#available_bundlesObject



33
34
35
36
37
38
39
40
# File 'lib/sgtn-client/loader/local_translation.rb', line 33

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

  @available_bundles = @base_path.glob('*/*.json').reduce(Set.new) do |bundles, f|
    locale = f.basename.to_s.sub(/\A#{BUNDLE_PREFIX}/i, '').sub(/#{BUNDLE_SUFFIX}\z/i, '')
    bundles.add Common::BundleID.new(f.parent.basename.to_s, locale)
  end
end

#load_bundle(component, locale) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sgtn-client/loader/local_translation.rb', line 18

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

  file_name = BUNDLE_PREFIX + locale + BUNDLE_SUFFIX
  file_path = @base_path + component + file_name

  bundle_data = JSON.parse(File.read(file_path))
  messages = bundle_data['messages']

  raise SingletonError, "no messages in local bundle file: #{file_path}." unless messages

  Common::BundleData.new(messages, origin: self, component: component,
                                   locale: locale == CONSTS::REAL_SOURCE_LOCALE ? LocaleUtil.get_source_locale : locale)
end