实现qt的多语言转换

 前言:qt实现多语言转换主要,用到lrelease.exe,在QT下运行图片和语言转换,需要对对应格式的内容进行转换。图片语言,甚至是字体均是通过添加.qrc配置,来转换。图片转换成.rcc格式。而语言通过在.excel编辑,转换成.ts文件。.ts文件通过lrelease.exe转换成.qm文件。前者可能更简单一点,后者可以通过python来实现。

一、.qrc添加图片资源转换成.rcc格式

newpng.qrc中填入以下内容来检索对应的图片的路径

<RCC>
  <qresource prefix="/">
    <file>pagestart.gif</file>
    <file>CutPeaks-Fillvalleys.png</file>
    <file>CutPeaks-Fillvalleys_press.png</file>
    <file>LoginCombox.png</file>
    <file>page1_1.png</file>
    <file>page1_change.png</file>
    <file>DM_N4.png</file>
    <file>DM_N5.png</file>
    <file>DM_N6.png</file>
    <file>TC_CS.png</file>
    <file>TC_HS.png</file>
    <file>TC_PQ.png</file>
 </qresource>
</RCC>

在linux的含qrc的文件下执行下面的命令

rcc -binary newpng.qrc -o newpng.rcc

得到newpng.rcc,将含.rcc的文件拷到对应编译的路径下,来实现图片资源的加载

二、多语言的实现

多语言,需要自行编辑一个excel文件,电脑需要安装有qt。

①将自己需要翻译的内容整理在excel中

②确保自己有python环境,博主是在vscode中运行的

python解释器安装教程(3.10版本)-CSDN博客

language.qrc用在qt中检索用的--没什么好说的

template.ts无用的文件

geni18nts.py让excel中的各列文件转换成.ts文件

tsToqm.py让.ts文件转换成.qm文件(这个是我自己弄的,hhh)

language.qrc

<RCC>
  <qresource prefix="/">
    <file>en.qm</file>
    <file>zh.qm</file>
    <file>jp.qm</file>
 </qresource>
</RCC>
geni18nts.py


# 我的表格如下,这是一个通过excel生成翻译文件.ts的表 的需求,请使用python实现
#
# en	zh	jp
# hujintao	胡金涛	hujintao_jp
# huhuhu	呼呼呼	huhuhu_jp
# 使用xlwing库操作excel 读取数据
# 1.第一行表头的数据为生成的翻译文件的文件名,例如 en.ts zh.ts jp.ts 有多少列就生成多少个文件
# 2.下面的数据为翻译文件的context 原文使用en列的数据,每个翻译文件的译文使用表格中对应的列的数据
# 例如 en列的数据为 hujintao 那么zh列的数据为 胡金涛 jp列的数据为 hujintao_jp
# 生成模版为
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE TS>
# <TS version="2.1" language="ja_JP">
# <context>
#     <message>
#         <source>huhuhu</source>
#         <translation type="unfinished">テキストラベル2</translation>
#     </message>
# </context>
# </TS>
# 及 在<context> 块中往下写 使用jinja2模版
import xlwings as xw
from jinja2 import Environment, FileSystemLoader
import subprocess
# 打开Excel文件并读取数据
xlsxApp = xw.App(visible=True, add_book=False)
xlsxApp.display_alerts = False
xlsxApp.screen_updating = False
wb = xlsxApp.books.open("./翻译源文件.xlsx")
sheet = wb.sheets[0]
data = sheet.range("A1").options(expand="table").value

# 读取第一行的数据作为文件名
file_names = data[0]

# 读取剩余的数据作为文件内容 去重
contents = data[1:]
en_set = set()
new_contents = []
# 遍历 contents ,删除重复的数据 ,并不是连续的
for content in contents:
    if(content[0] == "" or content[0] == None): # 去除空行
        continue
    if content[0] not in en_set:
        en_set.add(content[0])
        new_contents.append(content)


# 设置jinja2模板环境
env = Environment(loader=FileSystemLoader("./"))

# 对于每一列数据,生成一个.ts文件
for i, file_name in enumerate(file_names):
    # 加载模板
    template = env.get_template("./template.ts")

    # 生成文件内容
    file_content = template.render(contents=[(content[0], content[i]) for content in new_contents])

    # 写入文件
    with open(f"./{file_name}.ts", "w", encoding="utf-8") as f:
        f.write(file_content)

# 关闭Excel
wb.save()
wb.close()
xlsxApp.quit()
# 执行 cmd 命令 "E:\qt\qtEnv\sqlite3\aaa\bin\lrelease.exe D:/downloads/QtI18NProject-master/QtI18NProject-master/QtI18NProject.pro"
# subprocess.run(r'"D:\sqlite3\aaa\bin\lrelease.exe" "myqtui.pro"', shell=True)
subprocess.run(r'"E:\qt\qtEnv\sqlite3\aaa\bin\lrelease.exe" "myqtui.pro"', shell=True)

geni18nts.py注意:需要在相对路径下执行,如果你E:\qt\qtEnv\sqlite3\aaa\bin\lrelease.exe中没有lrelease.exe,则需要我的第二份代码。

有的话则需要改一下它的路径

import os
import subprocess

def convert_ts_to_qm(directory):
    # 遍历指定目录下的所有文件
    for root, dirs, files in os.walk(directory):
        for file in files:
            # 如果文件是 .ts 文件
            if file.endswith(".ts"):
                ts_file = os.path.join(root, file)
                qm_file = ts_file.replace(".ts", ".qm")
                
                # 生成对应的 .qm 文件
                command = [r"C:\Qt\Qt5.9.0\5.9\winrt_x86_msvc2017\bin\lrelease.exe", ts_file, "-qm", qm_file]  # 指定 lrelease 的完整路径
                try:
                    subprocess.run(command, check=True)
                    print(f"Generated {qm_file} successfully.")
                except subprocess.CalledProcessError as e:
                    print(f"Error generating {qm_file}: {e}")
                except FileNotFoundError:
                    print("lrelease not found. Please check the path.")

if __name__ == "__main__":
    # 指定包含 .ts 文件的路径
    directory = r"F:\translate"
    convert_ts_to_qm(directory)

生成qm文件

有了qm文件就可以执行翻译了

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值