Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Created February 6, 2015 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivabudh/f4d170fd817b1ffc97e2 to your computer and use it in GitHub Desktop.
Save sivabudh/f4d170fd817b1ffc97e2 to your computer and use it in GitHub Desktop.
$DICTIONARY_FILE = "Main.old.strings"
$FILE_TO_TRANSLATE = "Main.strings"
$TRANSLATED_FILE = "Main.new.strings"
module StringsExtraction
def self.extract_english_phrase line
cleaned_up_line = line.delete "/*\n"
tokens = cleaned_up_line.split(";")
token_with_english_phrase = tokens[1]
english_phrase = token_with_english_phrase.split(" = ")[1]
english_phrase
end
def self.extract_japanese_translation line
cleaned_up_line = line.delete(";\n")
tokens = cleaned_up_line.split(" = ")
japanese_translation = tokens[1]
japanese_translation
end
end
class TranslationFile
def open(file_name)
@file_name = file_name
return self
end
def each
file = File.open @file_name, 'r:utf-8'
lines = file
.readlines
.reject { |line| line.start_with? "\n"}
lines.each_slice(2) do |comment_line, translation_line|
english_word = StringsExtraction.extract_english_phrase(comment_line)
japanese_translation = StringsExtraction.extract_japanese_translation(translation_line)
yield english_word, japanese_translation
end
file.close
end
end
# Create dictionary
dictionary = {}
translation_file = TranslationFile.new
translation_file.open($DICTIONARY_FILE).each do |english_word, japanese_translation|
dictionary[english_word.downcase] = japanese_translation
end
# Start translating the intended file
output_file = File.open($FILE_TO_TRANSLATE)
lines = output_file.readlines
translated_lines = lines.map do |line|
if line.start_with? %{"}
english_phrase = StringsExtraction.extract_japanese_translation line
japanese_translation = dictionary[english_phrase.downcase]
if japanese_translation
tokens = line.split(" = ")
tokens[1].gsub!(/"([^"]*)";/, %{#{japanese_translation};})
tokens.join(" = ")
else
line
end
else
line
end
end
# Output the translated file as a new file
File.open($TRANSLATED_FILE, 'w:utf-8').write translated_lines.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment