Home > Enterprise >  Replace next line using regular expression
Replace next line using regular expression

Time:12-17

I have a huge file (it is a translation file for WordPress LocoTranslate plugin), and I need to translate what is in the msgstr quotes from what is in the msgid quotes. For instance:

#: ../../app/Models/SettingsAPI.php:105, 
#: ../../app/Controllers/Settings/AdminSettings.php:354
msgid "Your settings have been saved."
msgstr "Tus opciones fueron guardadas."

#: ../../app/Models/SettingsAPI.php:695, 
#: ../../app/Controllers/Admin/Meta/AddTermMetaField.php:357, 
#: ../../app/Controllers/Admin/Meta/AddTermMetaField.php:469
msgid "Add Image"
msgstr "Agregar Imagen"

I have tried using Google Sheets and the Google Translate API, but it is completely inaccurate, so I decided to go small at first and just transpose the string like:

#: ../../app/Models/SettingsAPI.php:105, 
#: ../../app/Controllers/Settings/AdminSettings.php:354
msgid "Your settings have been saved."
msgstr "Your settings have been saved."

I thought some text editor like Notepad could do this magic with a regular expression msgid \"(.*)\", but I wouldn't know how to replace the next line (msgstr.*) entirely.

I am pretty sure this question had to be made before actually making the tool to help me translate the file, but I was not that good with Notepad capabilities. Also, the tool does not work as expected.

Any suggestions?

CodePudding user response:

In Notepad do this:

  • Find what: ^(msgid ")(.*)("\s msgstr ").*(")
  • Replace with: $1$2$3$2$4
  • check (o) Regular expression, uncheck [_] , matches newline

The regex takes into account escaped " in the strings

CodePudding user response:

(msgid .*)               to keep
\n                       newline
.*                       to replace

.replace(/(msgid .*)\n.*/g, '$1')

  • Related