From 943544f8bf472f32ddc246b0811d2dcb3303bb0e Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 20 Sep 2013 12:36:19 -0500 Subject: [PATCH] Remove untranslated strings on import --- bin/clean_translations.rb | 53 +++++++++++++++++++++++++++++++++++++++ bin/getloc.rb | 4 +++ 2 files changed, 57 insertions(+) create mode 100644 bin/clean_translations.rb diff --git a/bin/clean_translations.rb b/bin/clean_translations.rb new file mode 100644 index 000000000..c0365a458 --- /dev/null +++ b/bin/clean_translations.rb @@ -0,0 +1,53 @@ +require 'rexml/document' + +STRINGS = {} + +def load(path) + file = File.new(path) + doc = REXML::Document.new(file) + doc.context[:attribute_quote] = :quote + doc +end + +def index_elements(doc) + doc.elements['resources'].each_element('string') do |elem| + string_name = elem.attributes['name'] + raise "duplicate string" if STRINGS.has_key? string_name + STRINGS[string_name] = elem.text + end +end + +def clean(path) + doc = load(path) + to_remove = [] + doc.elements['resources'].each_element('string') do |elem| + string_name = elem.attributes['name'] + to_remove << string_name if elem.text.eql? STRINGS[string_name] + end + to_remove.each do |name| + doc.elements.delete("resources/string[@name='#{name}']") + end + 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 "" + f.puts "" + f.puts "" + f.puts "" + f.print tail.join("\n") + end +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 + 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") +end \ No newline at end of file diff --git a/bin/getloc.rb b/bin/getloc.rb index 60b75e71d..1baca00b6 100755 --- a/bin/getloc.rb +++ b/bin/getloc.rb @@ -1,4 +1,7 @@ #!/usr/bin/env ruby +$:.unshift File.dirname(__FILE__) +require 'clean_translations' + # Script for invoking the GetLocalization tools # IMPORTANT: Right now, must be invoked from the project's root directory. # Usage: ./bin/getloc.rb [cmd] [lang] @@ -89,6 +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 end end