如何在我的.po文件的空字段中添加文本

发布于 2025-01-29 19:51:10 字数 1108 浏览 5 评论 0原文

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“某事在这里”

msgstr“在这里无”

提取“这里的东西”,我翻译并放回“这里”

目的

parse parse parse texte值

plass

  • msgid“我joe”
  • msgstr“”

所需的输出

  • msgid“我joe”
  • msgstr“ je suis joe”

替换函数不起作用。如何修复它

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2025-02-05 19:51:10

有两种方法可以做到这一点,使用输入文件“ text.txt”带有内容:

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

所需的输出为:

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

(选项1)单独的读取文件和写文件

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()

这将输出内容中的内容 new 文件“ textout.txt”,而原始文件“ text.txt”保持完整。

如果您不需要翻译列表中的翻译行,则可以使用以下代码(相同输出):

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()

(选项2)替换同一文件中的文本

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='')

此将更新“ text.txt”中的内容,而原始文件则将其重命名为“ text.txt.bak”

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文