From 341e7f29c397deac43171dc5b7c39b22e2e150cb Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 21 Sep 2013 01:06:04 -0500 Subject: [PATCH] Remove untranslated string-array and plurals --- bin/clean_translations.rb | 72 +++++++++++++++++++++++++++++++-------- bin/getloc.rb | 32 +++++++++-------- 2 files changed, 74 insertions(+), 30 deletions(-) diff --git a/bin/clean_translations.rb b/bin/clean_translations.rb index c0365a458..2a30b3000 100644 --- a/bin/clean_translations.rb +++ b/bin/clean_translations.rb @@ -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 "" @@ -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 \ No newline at end of file diff --git a/bin/getloc.rb b/bin/getloc.rb index 1baca00b6..8879ab1c1 100755 --- a/bin/getloc.rb +++ b/bin/getloc.rb @@ -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