I have 2 files, origin.txt and end.txt
Origin.txt have text like this:
msgid "Page not found"
msgstr "Page not found translation"
msgid "Latest Listings"
msgstr "Latest Listings translation"
end.txt hasn't translations
msgid "Page not found"
msgstr ""
msgid "Latest Listings"
msgstr ""
I want to copy the translated words from origin.txt to end.txt. I decided to create a hash in ruby with all the words from origin.txt and then replace the translation between the "" in the line msgstr "". This is my current code:
tr = {}
msgidline = ""
msgstrline = ""
f = File.open("origin.txt", "r")
target = open("end.txt", 'r+')
# Create a dictionary
f.each_line do |line|
if line.start_with?("msgid", "msgstr") && line.start_with?(%Q(msgid "")) == false && line.start_with?(%Q(msgstr "")) == false
if line.start_with?("msgid") == true
msgidline = line.scan(/"([^"]*)"/).join()
elsif line.start_with?("msgstr") == true
msgstrline = line.scan(/"([^"]*)"/).join()
end
end
tr[msgidline] = msgstrline
end
msgidlineReference = ""
target.each_line do |line|
if line.start_with?("msgid", "msgstr") && line.start_with?(%Q(msgid "")) == false
if line.start_with?("msgid") == true
msgidReference = line.scan(/"([^"]*)"/).join()
elsif line.start_with?("msgstr") == true
msgstrtext = line.scan(/"([^"]*)"/).join()
line.gsub(msgstrtext, tr[msgidReference])
end
end
end
target.close
f.close
I am getting a nil error:
translate.rb:29:in `gsub': no implicit conversion of nil into String (TypeError)
I don't know hot to make work the sub or gsub commands and replace the words in end.txt. This is the way to go? or do you think there is a better way to do it?
Edit update: I added ".join() at the end of scan. Now the nil error is gone. Thanks rantingsonrails. Now I need to write or replace in the file because it is not changing. Do you have any ideas?