Class: Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/helper.rb

Overview

The Helper class provides utility methods for various tasks.

Class Method Summary collapse

Class Method Details

.find_lproj(path) ⇒ Array<String>

Finds lproj localization files in the specified path.

Parameters:

  • path (String)

    The path to search for localization files.

Returns:

  • (Array<String>)

    An array of language codes found in the path.



11
12
13
14
# File 'lib/helper.rb', line 11

def self.find_lproj(path)
  lproj_files = Dir.glob("#{path}/*.lproj").select { |f| File.directory? f }
  lproj_files.map { |f| File.basename(f, ".lproj") }
end

.find_missing(file_a, file_b) ⇒ Array<String>

Finds missing keys in the target localization file compared to the base file.

Parameters:

  • file_a (String)

    The base localization file path.

  • file_b (String)

    The target localization file path.

Returns:

  • (Array<String>)

    An array of missing keys in the target file.



38
39
40
41
42
43
44
45
46
47
# File 'lib/helper.rb', line 38

def self.find_missing(file_a, file_b)
  strings_a = LocoStrings.load(file_a).read
  strings_b = LocoStrings.load(file_b).read

  lost_keys = []
  strings_a.each do |key, _string_a|
    lost_keys << key unless strings_b.key?(key)
  end
  lost_keys
end

.make_keys_dict(base_file, target_file) ⇒ Hash<String, String>

Creates a dictionary mapping keys between two localization files.

Parameters:

  • base_file (String)

    The base localization file path.

  • target_file (String)

    The target localization file path.

Returns:

  • (Hash<String, String>)

    A hash containing key mappings.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/helper.rb', line 21

def self.make_keys_dict(base_file, target_file)
  strings_base = LocoStrings.load(base_file).read
  strings_target = LocoStrings.load(target_file).read
  keys = {}
  strings_base.each do |key_base, string_base|
    strings_target.each do |key_target, string_target|
      keys[key_base] = key_target if string_base.value == string_target.value
    end
  end
  keys
end

.merge(source, _target, keys, lost_keys) ⇒ void

This method returns an undefined value.

Merges the source strings into the target strings

Parameters:

  • source (Hash)

    The source strings hash

  • _target (Hash)

    The target strings hash (not used in this method)

  • keys (Hash)

    A dictionary mapping source keys to target keys

  • lost_keys (Array)

    An array of lost keys to be updated



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/helper.rb', line 57

def self.merge(source, _target, keys, lost_keys)
  source.each do |key, source_string|
    target_key = keys[key]
    next unless target_key
    next unless lost_keys.include?(target_key)

    strings_b_file.update(target_key, source_string.value)
    lost_keys.delete(target_key)
    Logger.string_value(target_key, source_string.value)
  end
end