how to add text in the empty fields of my .po file

Viewed 31
import re
from deep_translator import GoogleTranslator


fname = input('Enter file name: ')
if (len(fname) < 1): fname = 'text.po'


fh = open(fname, 'r+')


translated=list()


for line in fh:
    if  not line.startswith('msgid '):
        continue
    piece=re.findall('"(.*)"', line)
    texte = GoogleTranslator(source='auto', target='french').translate(piece[0])
    translated.append(texte)
    print(translated)


    for line in fh:
        i =+ 0
        if  not line.startswith('msgstr '):
            continue
        piece2=re.findall('"()"', line)
        for space in piece2:
            fh.write(line.replace(space,  translated[i] ))


fh.close()

msgid "Something goes here"

msgstr "nothing here"

extract "something goes here" which I translate and put back into the "nothing here"

purpose

parse texte value in piece2

text.po file

  • msgid "i'm Joe"
  • msgstr ""

desired output

  • msgid "i'm Joe"
  • msgstr "Je suis Joe"

replace function not working. how to fix it please

1 Answers

There are 2 ways to do it, using the input file "text.txt" with contents:

msgid "i'm Joe"
msgstr ""
msgid "i'm Jack"
msgstr ""

And the desired output is:

msgid "i'm Joe"
msgstr "je suis Joe"
msgid "i'm Jack"
msgstr "je suis Jack"

(Option 1) separate read file and write file

import re
from deep_translator import GoogleTranslator

fin = open('text.txt', 'r')
fout = open('textout.txt', 'w')
i, texte, translated = -1, '', []

for line in fin:
    if line.startswith('msgid '):
        piece = re.findall('"(.*)"', line)
        texte = GoogleTranslator(source='auto', target='french').translate(piece[0])
        translated.append(texte)
        i += 1
        fout.write(line)

    if line.startswith('msgstr '):
        fout.write(line.replace('""', '"' + translated[i] + '"'))

fin.close()
fout.close()

This will output the contents in a new file "textout.txt", while the original file "text.txt" remains intact.

If you do not need the translated lines in the translated list, you could use below code (same output):

fin = open('text.txt', 'r')
fout = open('textout.txt', 'w')
texte = ''

for line in fin:
    if line.startswith('msgid '):
        piece = re.findall('"(.*)"', line)
        texte = GoogleTranslator(source='auto', target='french').translate(piece[0])
    fout.write(line.replace('""', '"' + texte + '"'))

fin.close()
fout.close()

(Option 2) replace text in the same file

import re
from deep_translator import GoogleTranslator
import fileinput

texte = ''
with fileinput.FileInput('text.txt', inplace=True, backup='.bak') as file:
    for line in file:
        if line.startswith('msgid '):
            piece = re.findall('"(.*)"', line)
            texte = GoogleTranslator(source='auto', target='french').translate(piece[0])
        print(line.replace('""', '"' + texte + '"'), end='')

This will update the contents in "text.txt", while the original file is renamed as "text.txt.bak"

Related