renpy-renpy对话内容汉化

前言

最近下载了一些renpy视觉小说内容,发现对话都为英文,因此我在想能否提取出这些对话然后汉化后再封装回原文件,将其汉化
当然汉化过程是机器翻译,汉化其他语言同理,大概5分钟左右就可以自动机翻完毕,还是挺高效的
最后实现了,流程如下,一起来看看吧

思路

步骤分为如下几步:
1,提取出renpy游戏中的对话内容
2,汉化提取出的对话内容文件,与原文形成一一对应关系,建立一个字典映射关系(根据原文所处的哪个文件,第几行,将汉化的文字内容对原文进行替换,形成新文件覆盖源文件)
3.修改gui.rpy文件,添加对中文显示的支持,因为renpy默认只支持英文显示

实现

1,提取对话内容

下载renpy的sdk文件,https://www.renpy.org/latest.html
将renpy游戏复制到sdk目录下会自动识别,然后
注意,为了保险起见,建议将renpy游戏复制保存一份,拿另一份来进行修改,以便出错时可以及时使用原文件修复
在这里插入图片描述

在这里插入图片描述
如下设置后会在renpy游戏目录生成一个dialog.tab文件,
如果提取出的dialog.tab文件为空,查看game目录下是否是rpa压缩文件,如果有rpa文件,从百度上搜rpa文件解压工具,将其中的rpy文件解压出来放到原目录即可,将解压出来的文件和原本的rpa文件放到同一目录下,重新提取对话文件

2,汉化对话内容文件

使用python,设置好提取出的tab文件路径,执行如下代码,python会提取出tab文件中的对话内容,封装到tab文件同级目录下的aaa.txt文件中

import csv
import os
import threading
import docx
originfile=r"dialogue.tab所在目录"
fileBase=originfile+"\\{}"
filetxt="aaa.txt"
realFIle=fileBase.format(filetxt)
dialogFile=originfile+"\\dialogue.tab"
print(dialogFile)
tempFileFlag=[]
with open(dialogFile, 'r',encoding= "utf8") as file:
    reader = csv.reader(file, delimiter='\t')
    header = next(reader)  # read the header row
    print(header)  # print the header row
    for row in reader:
        tempFileFlag.append([row[3],row[4],row[2]])
fileNameList=[]
for i in tempFileFlag:
    fileNameList.append(i[0])
fileNameList=list(set(fileNameList))
print(fileNameList)
def createFIle(path,content):
    with open(path, 'w',encoding="utf-8") as file:
        file.write(content)
    print("文件创建完成")
allTxt=""
for i in tempFileFlag:
    # print(i[2])
    allTxt+=i[2]+"\n"
# 所有对话提取完毕
createFIle(realFIle,allTxt)

从上述代码中会得到一个aaa.txt文件,这是游戏中所有的对话内容,接下来我们使用wps打开,然后将其另存为aaa.docx文件
然后将aaa.docx文件拖动到谷歌翻译的文件翻译中,谷歌翻译会自动翻译其中的对话内容,然后给我们反馈出一个新的已经汉化的aaa.docx文件,我们将其命名为aaa2.docx,放置到tab文件同级目录下
接下来,我们需要使用python,为aaa.docx,和aaa2.docx中的内容建立映射关系,然后使用这种映射关系来修改renpy游戏中的rpy文件,将renpy游戏中的英文对话进行汉化
汉化原理是,对renpy游戏总的rpy文件中的英文对话语句进行汉化替换,删除旧有的英文rpy文件,使用汉化的语句进行新建原本的文件
执行如下代码:

import csv
import os
import threading
import docx
originfile=r"dialogue.tab所在目录"
fileBase=originfile+"\\{}"
filetxt="aaa.txt"
realFIle=fileBase.format(filetxt)
dialogFile=originfile+"\\dialogue.tab"
print(dialogFile)
tempFileFlag=[]
with open(dialogFile, 'r',encoding= "utf8") as file:
    reader = csv.reader(file, delimiter='\t')
    header = next(reader)  # read the header row
    print(header)  # print the header row
    for row in reader:
        tempFileFlag.append([row[3],row[4],row[2]])
fileNameList=[]
for i in tempFileFlag:
    fileNameList.append(i[0])
fileNameList=list(set(fileNameList))
print(fileNameList)
def createFIle(path,content):
    with open(path, 'w',encoding="utf-8") as file:
        file.write(content)
    print("文件创建完成")
allTxt=""
for i in tempFileFlag:
    # print(i[2])
    allTxt+=i[2]+"\n"
# 所有对话提取完毕
#createFIle(realFIle,allTxt)




# 读取文档,完成列表替换,完成文件传输
def deleteFile(path):
    os.remove(path)
    print("文件删除完成")
def readdocx():
    cnTxt = []
    path = originfile+"\\aaa2.docx"
    print(path)
    # 打开文档
    doc = docx.Document(path)
    # 读取每个段落的内容
    for para in doc.paragraphs:
        # print(para.text)
        cnTxt.append(para.text)
    return cnTxt
cnTxt=readdocx()
print(len(cnTxt))
print(len(tempFileFlag))
allLen=len(tempFileFlag)
newTxtFlag=[]
# from pypinyin import pinyin, lazy_pinyin, Style
# def changeCntoPinyin(str):
#     result = lazy_pinyin(str)
#     strlist = ""
#     for i in result:
#         strlist += i + " "
#     return strlist
for i in range(allLen):
    temp=[]
    temp.append(tempFileFlag[i])
    temp.append(cnTxt[i])
    newTxtFlag.append(temp)
# for i in newTxtFlag:
#     print(i)
enTocn=[]
for i in newTxtFlag:
    enTocn.append([i[0][2],i[1]])
# for i in enTocn:
#     print(i)
list_of_lists=enTocn
column_dict = {}
for i, row in enumerate(list_of_lists):
    for j, content in enumerate(row):
        if content in column_dict:
            column_dict[content].append((i, j))
        else:
            column_dict[content] = [(i, j)]
def getdict():
    return column_dict,enTocn
        # 现在你可以通过字典快速找到任何内容对应的坐标
def getTranslateT(content_to_find):
    print(content_to_find)
    coordinates = column_dict.get(content_to_find)
    if coordinates:
        print("已经发现了此")
        print(enTocn[coordinates[0][0]][1])
        return enTocn[coordinates[0][0]][1]

    else:
        print("未发现")
        print(content_to_find)
        return content_to_find





# getTranslateT("Dalia! I... Um...")
newListByFile=[]
for i in fileNameList:
    temp=[]
    for j in newTxtFlag:
        if i==j[0][0]:
            temp.append(j)
    newListByFile.append(temp)
#     展示替换效果
for i in newListByFile:
    print(i)
def changeFile(path,listLine):
    with open(path, 'r',encoding="utf-8") as file:
        lines = file.readlines()
    # for i in lines:
    #     print(i)
    for i in listLine:
        textIndex=int(i[0][1])-1
        lines[textIndex]=lines[textIndex].replace(i[0][2],i[1])
        print(lines)
    deleteFile(path)
    allnewTxt=""
    for i in lines:
        allnewTxt+=i
    createFIle(path,allnewTxt)
    print("新文件创建完毕")

testi=[]
for i in newListByFile:
    filetxt = i[0][0][0].replace("/","\\")
    testFIle = fileBase.format(filetxt)
    # print(testFIle)
    changeFile(testFIle,i)
print("文件更新完毕")

当出现文件更新完毕,程序停止时,代表所有的对话文件已经更新完毕

3,修改gui文件,使得renpy游戏支持中文显示

使用simfang.ttf这个支持中文的ttf文件,对renpy游戏中的文件进行展示替换游戏默认的ttf文件
simfang.ttf是windows自带的文件,用于支持中文显示的样式文件,目录在C:\Windows\Fonts
可以直接搜到,找到后复制到renpy游戏的game目录下,和那些rpy,rpyc文件放在一起

打开gui.rpy文件,修改如下行

## The font used for in-game text.
define gui.text_font = "simfang.ttf"

## The font used for character names.
define gui.name_text_font = "simfang.ttf"

## The font used for out-of-game text.
define gui.interface_text_font = "simfang.ttf"
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值