Remove untranslated string-array and plurals

pull/25/head
Alex Baker 12 years ago
parent 6c6b309394
commit 341e7f29c3

@ -1,6 +1,8 @@
require 'rexml/document'
STRINGS = {}
STRING_ARRAYS = {}
PLURALS = {}
def load(path)
file = File.new(path)
@ -9,26 +11,68 @@ def load(path)
doc
end
def get_items(elem)
items = []
elem.each_element('item') do |item|
items << item.text
end
items
end
def get_plurals(elem)
plurals = {}
elem.each_element('item') do |item|
plurals[item.attributes['quantity']] = item.text
end
plurals
end
def index_elements(doc)
doc.elements['resources'].each_element('string') do |elem|
doc.elements['resources'].each_element('string') { |elem| STRINGS[elem.attributes['name']] = elem.text }
doc.elements['resources'].each_element('string-array') { |elem| STRING_ARRAYS[elem.attributes['name']] = get_items(elem) }
doc.elements['resources'].each_element('plurals') { |elem| PLURALS[elem.attributes['name']] = get_plurals(elem) }
end
def find_duplicate_plurals(doc)
dups = []
doc.elements['resources'].each_element('plurals') do |elem|
string_name = elem.attributes['name']
raise "duplicate string" if STRINGS.has_key? string_name
STRINGS[string_name] = elem.text
plurals = get_plurals(elem)
dups << string_name if plurals.eql? PLURALS[string_name]
end
dups
end
def clean(path)
doc = load(path)
to_remove = []
doc.elements['resources'].each_element('string') do |elem|
def find_duplicate_string_arrays(doc)
dups = []
doc.elements['resources'].each_element('string-array') do |elem|
string_name = elem.attributes['name']
to_remove << string_name if elem.text.eql? STRINGS[string_name]
items = get_items(elem)
dups << string_name if items.eql? STRING_ARRAYS[string_name]
end
to_remove.each do |name|
doc.elements.delete("resources/string[@name='#{name}']")
dups
end
def find_duplicate_strings(doc)
dups = []
doc.elements['resources'].each_element('string') do |elem|
string_name = elem.attributes['name']
dups << string_name if elem.text.eql? STRINGS[string_name]
end
dups
end
def remove_items(doc, type, names)
names.each { |name| doc.elements.delete("resources/#{type}[@name='#{name}']") }
end
def clean(path)
doc = load(path)
remove_items(doc, 'string', find_duplicate_strings(doc))
remove_items(doc, 'string-array', find_duplicate_string_arrays(doc))
remove_items(doc, 'plurals', find_duplicate_plurals(doc))
head, *tail = doc.to_s.split("\n").reject { |x| x.strip.eql? "" }
File.open(path, 'w') do |f|
head, *tail = doc.to_s.split("\n").reject { |x| x.strip.eql? ""}
f.puts head
f.puts "<!-- ************************************************************** -->"
f.puts "<!-- ********* THIS FILE IS GENERATED BY GETLOCALIZATION ********** -->"
@ -41,13 +85,11 @@ end
def remove_untranslated_strings(*string_files)
index_elements(load("api/src/main/res/values/strings.xml"))
Dir.glob("astrid/src/main/res/values/strings*.xml").each do |path|
index_elements(load(path))
end
Dir.glob("astrid/src/main/res/values/strings*.xml").each { |path| index_elements(load(path)) }
string_files.each { |path| clean path }
end
if __FILE__ == $0
lang = ARGV[0]
remove_untranslated_strings("../api/src/main/res/values-#{lang}/strings.xml", "../astrid/src/main/res/values-#{lang}/strings.xml")
remove_untranslated_strings("api/src/main/res/values-#{lang}/strings.xml", "astrid/src/main/res/values-#{lang}/strings.xml")
end

@ -92,7 +92,7 @@ def import(tmp_files, lang, dst_files_block)
puts "Moving #{tmp_files[i]} to #{dst_files[i]}"
%x(mv #{tmp_files[i]} #{dst_files[i]})
end
remove_untranslated_strings *dst_files
remove_untranslated_strings(*dst_files)
end
end
@ -115,27 +115,29 @@ end
# Main function for invoking the GetLocalization tools
# cmd (String): Command to invoke. Must be 'import' or 'export'
# lang (String): Language code. Can also be 'master' to specify master files for export or all languages for import.
def getloc(cmd, lang)
def getloc(cmd, languages)
cmd = cmd.to_sym
raise "must set GETLOC_USER and GETLOC_PASS environment variables" if ENV['GETLOC_USER'].nil? or ENV['GETLOC_PASS'].nil?
@user = ENV['GETLOC_USER']
@password = ENV['GETLOC_PASS']
platform_class = Android
case cmd
when :export
puts "Exporting #{lang} files"
export(platform_class.tmp_files, lang, platform_class.src_files(cmd, lang))
when :import
puts "Importing #{lang} files"
import(platform_class.tmp_files, lang, platform_class.src_files(cmd, lang))
else
puts "Command #{cmd} not recognized. Should be one of 'export' or 'import'."
return
end
languages.split(',').each do |lang|
case cmd
when :export
puts "Exporting #{lang} files"
export(platform_class.tmp_files, lang, platform_class.src_files(cmd, lang))
when :import
puts "Importing #{lang} files"
import(platform_class.tmp_files, lang, platform_class.src_files(cmd, lang))
else
puts "Command #{cmd} not recognized. Should be one of 'export' or 'import'."
return
end
platform_class.tmp_files.each do |f|
%x(rm -f #{f})
platform_class.tmp_files.each do |f|
%x(rm -f #{f})
end
end
end

Loading…
Cancel
Save