Class: AdLocalize::Platform::PlatformFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_localize/platform/platform_formatter.rb

Constant Summary collapse

PLURAL_KEY_SYMBOL =
:plural
SINGULAR_KEY_SYMBOL =
:singular
OPTIONS =
{output_path: ".."}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_locale, output_path, add_intermediate_platform_dir) ⇒ PlatformFormatter

Returns a new instance of PlatformFormatter.



10
11
12
13
14
15
16
# File 'lib/ad_localize/platform/platform_formatter.rb', line 10

def initialize(default_locale, output_path, add_intermediate_platform_dir)
  @default_locale = default_locale.to_sym
  @output_path = output_path # Must be set before platform_dir
  @platform_dir = add_intermediate_platform_dir ? export_base_directory.join(platform.to_s) : export_base_directory
  @add_intermediate_platform_dir = add_intermediate_platform_dir
  create_platform_dir
end

Instance Attribute Details

#add_intermediate_platform_dirObject (readonly)

Returns the value of attribute add_intermediate_platform_dir.



8
9
10
# File 'lib/ad_localize/platform/platform_formatter.rb', line 8

def add_intermediate_platform_dir
  @add_intermediate_platform_dir
end

#default_localeObject (readonly)

Returns the value of attribute default_locale.



8
9
10
# File 'lib/ad_localize/platform/platform_formatter.rb', line 8

def default_locale
  @default_locale
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



8
9
10
# File 'lib/ad_localize/platform/platform_formatter.rb', line 8

def output_path
  @output_path
end

#platform_dirObject

Returns the value of attribute platform_dir.



7
8
9
# File 'lib/ad_localize/platform/platform_formatter.rb', line 7

def platform_dir
  @platform_dir
end

Instance Method Details

#export(locale, data, export_extension, substitution_format) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ad_localize/platform/platform_formatter.rb', line 22

def export(locale, data, export_extension, substitution_format)
  locale = locale.to_sym
  formatted_data = data.each_with_object({}) do |(key, wording), hash_acc|
    hash_acc[locale.to_s] = {} unless hash_acc.key? locale.to_s
    if wording.dig(locale)&.key? :singular
      value = ios_converter(wording.dig(locale, :singular))
      hash_acc[locale.to_s][key.to_s] = value
    end
    if wording.dig(locale)&.key? :plural
      hash_acc[locale.to_s][key.to_s] = {}
      wording.dig(locale, :plural).each do |plural_type, plural_text|
        value = ios_converter(plural_text)
        hash_acc[locale.to_s][key.to_s][plural_type.to_s] = value
      end
    end
  end

  platform_dir.join("#{locale.to_s}.#{export_extension}").open("w") do |file|
    yield(formatted_data, file)
  end

  AdLocalize::LOGGER.log(:debug, :green, "#{platform.to_s.upcase} [#{locale}] ---> DONE!")
end

#platformObject



18
19
20
# File 'lib/ad_localize/platform/platform_formatter.rb', line 18

def platform
  raise 'Please override me!'
end