@@ -11,19 +11,19 @@ module Android
11
11
module LocalizeHelper
12
12
LIB_SOURCE_XML_ATTR = 'a8c-src-lib' . freeze
13
13
14
- # Checks if string_line has the content_override flag set
15
- def self . skip_string_by_tag ( string_line )
16
- skip = string_line . attr ( 'content_override' ) == 'true' unless string_line . attr ( 'content_override' ) . nil?
14
+ # Checks if `string_node` has the content_override flag set
15
+ def self . skip_string_by_tag? ( string_node )
16
+ skip = string_node . attr ( 'content_override' ) == 'true' unless string_node . attr ( 'content_override' ) . nil?
17
17
if skip
18
- UI . message " - Skipping #{ string_line . attr ( 'name' ) } string"
18
+ UI . message " - Skipping #{ string_node . attr ( 'name' ) } string"
19
19
return true
20
20
end
21
21
22
22
return false
23
23
end
24
24
25
- # Checks if string_name is in the excluesion list
26
- def self . skip_string_by_exclusion_list ( library , string_name )
25
+ # Checks if ` string_name` is in the exclusion list
26
+ def self . skip_string_by_exclusion_list? ( library , string_name )
27
27
return false if library [ :exclusions ] . nil?
28
28
29
29
skip = library [ :exclusions ] . include? ( string_name )
@@ -43,66 +43,66 @@ def self.add_xml_attributes!(string_node, library)
43
43
string_node [ LIB_SOURCE_XML_ATTR ] = library [ :source_id ] unless library [ :source_id ] . nil?
44
44
end
45
45
46
- # Merge string_line into main_string
47
- def self . merge_string ( main_strings , library , string_line )
48
- string_name = string_line . attr ( 'name' )
49
- string_content = string_line . content
46
+ # Merge a single `lib_string_node` XML node into the `main_strings_xml``
47
+ def self . merge_string_node ( main_strings_xml , library , lib_string_node )
48
+ string_name = lib_string_node . attr ( 'name' )
49
+ string_content = lib_string_node . content
50
50
51
51
# Skip strings in the exclusions list
52
- return :skipped if skip_string_by_exclusion_list ( library , string_name )
52
+ return :skipped if skip_string_by_exclusion_list? ( library , string_name )
53
53
54
54
# Search for the string in the main file
55
55
result = :added
56
- main_strings . xpath ( '//string' ) . each do |this_string |
57
- if this_string . attr ( 'name' ) == string_name
56
+ main_strings_xml . xpath ( '//string' ) . each do |main_string_node |
57
+ if main_string_node . attr ( 'name' ) == string_name
58
58
# Skip if the string has the content_override tag
59
- return :skipped if skip_string_by_tag ( this_string )
59
+ return :skipped if skip_string_by_tag? ( main_string_node )
60
60
61
61
# If nodes are equivalent, skip
62
- return :found if string_line =~ this_string
62
+ return :found if lib_string_node =~ main_string_node
63
63
64
64
# The string needs an update
65
65
result = :updated
66
- if this_string . attr ( 'tools:ignore' ) . nil?
66
+ if main_string_node . attr ( 'tools:ignore' ) . nil?
67
67
# It can be updated, so remove the current one and move ahead
68
- this_string . remove
68
+ main_string_node . remove
69
69
break
70
70
else
71
71
# It has the tools:ignore flag, so update the content without touching the other attributes
72
- this_string . content = string_content
73
- add_xml_attributes! ( this_string , library )
72
+ main_string_node . content = string_content
73
+ add_xml_attributes! ( main_string_node , library )
74
74
return result
75
75
end
76
76
end
77
77
end
78
78
79
79
# String not found, or removed because needing update and not in the exclusion list: add to the main file
80
- add_xml_attributes! ( string_line , library )
81
- main_strings . xpath ( '//string' ) . last ( ) . add_next_sibling ( "\n #{ ' ' * 4 } #{ string_line . to_xml ( ) . strip } " )
80
+ add_xml_attributes! ( lib_string_node , library )
81
+ main_strings_xml . xpath ( '//string' ) . last ( ) . add_next_sibling ( "\n #{ ' ' * 4 } #{ lib_string_node . to_xml ( ) . strip } " )
82
82
return result
83
83
end
84
84
85
- # Verify a string
86
- def self . verify_string ( main_strings , library , string_line )
87
- string_name = string_line . attr ( 'name' )
88
- string_content = string_line . content
85
+ # Verify a string node from a library has properly been merged into the main one
86
+ def self . verify_string ( main_strings_xml , library , lib_string_node )
87
+ string_name = lib_string_node . attr ( 'name' )
88
+ string_content = lib_string_node . content
89
89
90
90
# Skip strings in the exclusions list
91
- return if skip_string_by_exclusion_list ( library , string_name )
91
+ return if skip_string_by_exclusion_list? ( library , string_name )
92
92
93
93
# Search for the string in the main file
94
- main_strings . xpath ( '//string' ) . each do |this_string |
95
- if this_string . attr ( 'name' ) == string_name
94
+ main_strings_xml . xpath ( '//string' ) . each do |main_string_node |
95
+ if main_string_node . attr ( 'name' ) == string_name
96
96
# Skip if the string has the content_override tag
97
- return if skip_string_by_tag ( this_string )
97
+ return if skip_string_by_tag? ( main_string_node )
98
98
99
- # Update if needed
100
- UI . user_error! ( "String #{ string_name } [#{ string_content } ] has been updated in the main file but not in the library #{ library [ :library ] } ." ) if this_string . content != string_content
99
+ # Check if up-to-date
100
+ UI . user_error! ( "String #{ string_name } [#{ string_content } ] has been updated in the main file but not in the library #{ library [ :library ] } ." ) if main_string_node . content != string_content
101
101
return
102
102
end
103
103
end
104
104
105
- # String not found and not in the exclusion list:
105
+ # String not found and not in the exclusion list
106
106
UI . user_error! ( "String #{ string_name } [#{ string_content } ] was found in library #{ library [ :library ] } but not in the main file." )
107
107
end
108
108
@@ -122,23 +122,23 @@ def self.verify_string(main_strings, library, string_line)
122
122
#
123
123
def self . merge_lib ( main , library )
124
124
UI . message ( "Merging #{ library [ :library ] } strings into #{ main } " )
125
- main_strings = File . open ( main ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
126
- lib_strings = File . open ( library [ :strings_path ] ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
125
+ main_strings_xml = File . open ( main ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
126
+ lib_strings_xml = File . open ( library [ :strings_path ] ) { |f | Nokogiri ::XML ( f , nil , Encoding ::UTF_8 . to_s ) }
127
127
128
128
updated_count = 0
129
129
untouched_count = 0
130
130
added_count = 0
131
131
skipped_count = 0
132
- lib_strings . xpath ( '//string' ) . each do |string_line |
133
- res = merge_string ( main_strings , library , string_line )
132
+ lib_strings_xml . xpath ( '//string' ) . each do |string_node |
133
+ res = merge_string_node ( main_strings_xml , library , string_node )
134
134
case res
135
135
when :updated
136
- UI . verbose "#{ string_line . attr ( 'name' ) } updated."
136
+ UI . verbose "#{ string_node . attr ( 'name' ) } updated."
137
137
updated_count = updated_count + 1
138
138
when :found
139
139
untouched_count = untouched_count + 1
140
140
when :added
141
- UI . verbose "#{ string_line . attr ( 'name' ) } added."
141
+ UI . verbose "#{ string_node . attr ( 'name' ) } added."
142
142
added_count = added_count + 1
143
143
when :skipped
144
144
skipped_count = skipped_count + 1
@@ -148,7 +148,7 @@ def self.merge_lib(main, library)
148
148
end
149
149
150
150
File . open ( main , 'w:UTF-8' ) do |f |
151
- f . write ( main_strings . to_xml ( indent : 4 ) )
151
+ f . write ( main_strings_xml . to_xml ( indent : 4 ) )
152
152
end
153
153
154
154
UI . message ( "Done (#{ added_count } added, #{ updated_count } updated, #{ untouched_count } untouched, #{ skipped_count } skipped)." )
@@ -164,8 +164,8 @@ def self.verify_diff(diff_string, main_strings, lib_strings, library)
164
164
165
165
diff_string = diff_string . slice ( 0 ..( end_index - 1 ) )
166
166
167
- lib_strings . xpath ( '//string' ) . each do |string_line |
168
- res = verify_string ( main_strings , library , string_line ) if string_line . attr ( 'name' ) == diff_string
167
+ lib_strings . xpath ( '//string' ) . each do |string_node |
168
+ res = verify_string ( main_strings , library , string_node ) if string_node . attr ( 'name' ) == diff_string
169
169
end
170
170
end
171
171
end
0 commit comments