Skip to content

Commit 3a82fae

Browse files
committed
Handle fallback keys for translations
1 parent 116952d commit 3a82fae

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class MetadataDownloader
1010
def initialize(target_folder, target_files)
1111
@target_folder = target_folder
1212
@target_files = target_files
13+
@alternates = {}
1314
end
1415

1516
# Downloads data from GlotPress,
@@ -21,8 +22,10 @@ def download(target_locale, glotpress_url, is_source)
2122
response = Net::HTTP.get_response(URI.parse(response.header['location']))
2223
end
2324

25+
@alternates.clear
2426
loc_data = JSON.parse(response.body) rescue loc_data = nil
25-
parse_data(target_locale, loc_data, is_source)
27+
parse_data(target_locale, loc_data, is_source)
28+
reparse_alternates(target_locale, loc_data, is_source) unless (@alternates.length == 0)
2629
end
2730

2831
# Parse JSON data and update the local files
@@ -42,16 +45,41 @@ def parse_data(target_locale, loc_data, is_source)
4245
if (file[0].to_s == key)
4346
data = file[1]
4447
msg = is_source ? source : d[1]
45-
if (data.key?(:max_size)) && (data[:max_size] != 0) && ((msg.to_s.length - 3) > data[:max_size]) then
46-
UI.message("Rejecting #{target_locale} traslation for #{key}: translation length: #{msg.to_s.length} - max allowed length: #{data[:max_size]}")
47-
else
48-
save_metadata(target_locale, file[1][:desc], msg)
49-
end
48+
update_key(target_locale, key, file, data, msg)
5049
end
5150
end
5251
end
5352
end
5453

54+
# Parse JSON data and update the local files
55+
def reparse_alternates(target_locale, loc_data, is_source)
56+
loc_data.each do | d |
57+
key = d[0].split(/\u0004/).first
58+
source = d[0].split(/\u0004/).last
59+
60+
@alternates.each do | file |
61+
if (file[0].to_s == key)
62+
data = file[1]
63+
msg = is_source ? source : d[1]
64+
update_key(target_locale, key, file, data, msg)
65+
end
66+
end
67+
end
68+
end
69+
70+
def update_key(target_locale, key, file, data, msg)
71+
if (data.key?(:max_size)) && (data[:max_size] != 0) && ((msg.to_s.length - 3) > data[:max_size]) then
72+
if (data.key?(:alternate_key)) then
73+
UI.message("#{target_locale} traslation for #{key} exceeds maximum lenght (#{msg.to_s.length}). Switching to the alternate translation.")
74+
@alternates[data[:alternate_key]] = {desc: data[:desc], max_size: data[:max_size] }
75+
else
76+
UI.message("Rejecting #{target_locale} traslation for #{key}: translation length: #{msg.to_s.length} - max allowed length: #{data[:max_size]}")
77+
end
78+
else
79+
save_metadata(target_locale, file[1][:desc], msg)
80+
end
81+
end
82+
5583
# Writes the downloaded content
5684
# to the target file
5785
def save_metadata(locale, file_name, content)

0 commit comments

Comments
 (0)