Class: Helper
- Inherits:
-
Object
- Object
- Helper
- Defined in:
- lib/helper.rb
Overview
The Helper class provides utility methods for various tasks.
Class Method Summary collapse
-
.find_lproj(path) ⇒ Array<String>
Finds lproj localization files in the specified path.
-
.find_missing(file_a, file_b) ⇒ Array<String>
Finds missing keys in the target localization file compared to the base file.
-
.make_keys_dict(base_file, target_file) ⇒ Hash<String, String>
Creates a dictionary mapping keys between two localization files.
-
.merge(source, _target, keys, lost_keys) ⇒ void
Merges the source strings into the target strings.
-
.oversize(file, target_lang, gap) ⇒ void
Finds oversize strings in the specified file.
-
.sort(file, sort) ⇒ Array
Sorts the strings in the specified file.
-
.transform_keys(strings, transform_type) ⇒ void
Transforms the keys in the specified file.
Class Method Details
.find_lproj(path) ⇒ Array<String>
Finds lproj localization files in the specified 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.
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.
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
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) _target.update(target_key, source_string.value) lost_keys.delete(target_key) Logger.string_value(target_key, source_string.value) end end |
.oversize(file, target_lang, gap) ⇒ void
This method returns an undefined value.
Finds oversize strings in the specified file
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/helper.rb', line 124 def self.oversize(file, target_lang, gap) strings = file.read strings.each do |key, string| target_string = file.value(key, target_lang) limit_length = string.value.length + gap next unless limit_length < target_string.length Logger.oversize(string, target_string) end end |
.sort(file, sort) ⇒ Array
Sorts the strings in the specified file
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/helper.rb', line 75 def self.sort(file, sort) strings = LocoStrings.load(file).read sorted = [] strings.each do |_key, string| sorted << string end if sort if sort == "asc" sorted.sort! { |a, b| a.key <=> b.key } elsif sort == "desc" sorted.sort! { |a, b| b.key <=> a.key } else Logger.error("Invalid sort option") exit end end sorted end |
.transform_keys(strings, transform_type) ⇒ void
This method returns an undefined value.
Transforms the keys in the specified file
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/helper.rb', line 100 def self.transform_keys(strings, transform_type) return unless transform_type strings.each do |string| old_key = string.key if transform_type == "lower" string.key = string.key.downcase elsif transform_type == "upper" string.key = string.key.upcase else Logger.error("Invalid transform type") exit end Logger.key_transform(old_key, string) end end |