@@ -10,6 +10,7 @@ class MetadataDownloader
10
10
def initialize ( target_folder , target_files )
11
11
@target_folder = target_folder
12
12
@target_files = target_files
13
+ @alternates = { }
13
14
end
14
15
15
16
# Downloads data from GlotPress,
@@ -21,8 +22,10 @@ def download(target_locale, glotpress_url, is_source)
21
22
response = Net ::HTTP . get_response ( URI . parse ( response . header [ 'location' ] ) )
22
23
end
23
24
25
+ @alternates . clear
24
26
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 )
26
29
end
27
30
28
31
# Parse JSON data and update the local files
@@ -42,16 +45,41 @@ def parse_data(target_locale, loc_data, is_source)
42
45
if ( file [ 0 ] . to_s == key )
43
46
data = file [ 1 ]
44
47
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 )
50
49
end
51
50
end
52
51
end
53
52
end
54
53
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
+
55
83
# Writes the downloaded content
56
84
# to the target file
57
85
def save_metadata ( locale , file_name , content )
0 commit comments