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.



19
20
21
# File 'lib/sgtn-client/loader/local_translation.rb', line 19

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

Instance Method Details

#available_bundlesObject



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

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

  @available_bundles ||= begin
    @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
end

#load_bundle(component, locale) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sgtn-client/loader/local_translation.rb', line 23

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 SgtnClient::SingletonError, "no messages in local bundle file: #{file_path}." unless messages

  messages
end