Browse Source

make better output

Ray Lee 7 years ago
parent
commit
4bfa84c77d
6 changed files with 20 additions and 32 deletions
  1. 2 2
      .dockerignore
  2. 1 2
      .gitignore
  3. 15 14
      main.rb
  4. 0 12
      output/fr.xml-not-found
  5. 2 2
      readme.md
  6. 0 0
      template/.keep

+ 2 - 2
.dockerignore

@@ -1,4 +1,4 @@
 input/*.csv
-output/*.xml
-output/*.xml-not-found
+template/*.csv
+output/*.txt
 .DS_Store

+ 1 - 2
.gitignore

@@ -1,5 +1,4 @@
 input/*.csv
 template/*.csv
-output/*.xml
-output/*.xml-not-found
+output/*.txt
 .DS_Store

+ 15 - 14
main.rb

@@ -4,14 +4,14 @@ require 'nokogiri'
 module MultiLangTrans
   ORINGIN_PATH = 'template/Resources.resw'
   TEMPLATE_PATH = 'template/template.csv'
-  
+
   def self.run
     make_template
     input_file_paths = Dir.glob('input/*.csv')
     input_file_paths.each do |input_file_path|
       input_content = read_csv(input_file_path)
-      output_file_path = to_output_path(input_file_path)
-      write_xml(output_file_path, input_content)
+      output_file_paths = get_output_paths(input_file_path)
+      write_xml(output_file_paths, input_content)
     end
   end
 
@@ -36,18 +36,21 @@ module MultiLangTrans
     input_content
   end
 
-  def self.to_output_path(input_path)
+  def self.get_output_paths(input_path)
+    output_paths = {}
     output_path = input_path.sub('input/', "output/")
-    output_path = output_path.sub('.csv', ".xml")
+    output_paths[:matched] = output_path.sub('.csv', ".txt")
+    output_paths[:dismatched] = output_path.sub('.csv', "-not-found.txt")
+    output_paths
   end
 
-  def self.write_xml(xml_file_path, content_rows)
-    not_found = []
-    File.open(xml_file_path, 'w') do |f|
-      content_rows.each do |row|
+  def self.write_xml(xml_file_paths, content_rows)
+    not_found_rows = []
+    File.open(xml_file_paths[:matched], 'w') do |f|
+      content_rows.each_with_index do |row, index|
         template_row = @template_content.find{|t| t['trans_value'] == row['template_value']}
         if template_row.nil?
-          not_found << row['template_value']
+          not_found_rows << "line:#{index + 2}.\t#{row['template_value']}"
         else
           f.puts("<data name=\"#{template_row['name']}\" xml:space=\"preserve\">")
           f.puts("  <value>#{row['trans_value']}</valie>") unless row['trans_value'].nil?
@@ -56,10 +59,8 @@ module MultiLangTrans
         end
       end
     end
-    File.open("#{xml_file_path}-not-found", 'w') do |f|
-      not_found.each_with_index do |row, index|
-        f.puts("#{index}. [#{row}]")
-      end
+    File.open(xml_file_paths[:dismatched], 'w') do |f|
+      not_found_rows.each { |row| f.puts(row) }
     end
   end
 end

+ 0 - 12
output/fr.xml-not-found

@@ -1,12 +0,0 @@
-0. [Search Kdan Creative Store for related accessories and services to upgrade your mobile experience.]
-1. [Stay organized and make beautiful notes with the world's most multifaceted notebook app.]
-2. [Work seamlessly with PDF Reader on your desktop and phones!]
-3. [If you no longer have access to the email you registered, please replace it with a new one.]
-4. [By signing up, you agree to our ]
-5. [,]
-6. [We have sent a verification link to
-{0}
-
-Please click the link in the email to complete verification process.]
-7. [OK, I got it]
-8. [Before uploading files, please verify your email to activate the account and receive 500MB of free space.]

+ 2 - 2
readme.md

@@ -7,11 +7,11 @@ In case, you already have a completed English version.
     gem install nokogiri
 
 ## Usage
-1. Put your English translated file into ./Resources.resw
+1. Put your English translated file into ./template/Resources.resw
 2. Copy English sentences and another language(Franch, for example) sentences into ./input/fr.csv
 3. Notice that header of fr.csv should be **template_value** and **trans_value**
 4. Run the script
 
     ruby main.rb
 
-5. output file would be ./output/fr.xml (and ./output/fr.xml-not-found if any not-found sentence)
+5. output file would be ./output/fr.txt (and ./output/fr-not-found.txt if any not-found sentence)

+ 0 - 0
template/.keep