kisolre wrote: ↑Sun Sep 12, 2021 8:57 am
No idea how
Could you create such script?
Это была опечатка переводчика... Я имел ввиду, что могу подправить код
Code ready.
The path to the *.ts files
must be without spaces
Put *.ts to User_folder\ts folder
You can place *.ts file in subfolder. For example \ts\Bulgarian\
locale ("any_file_
bg.qm" for Bulgarian) getted from xml text of each *.ts file.
Code: Select all
#! python
# -*- coding: utf-8 -*-
# <nowiki>(c) 2021 <Evgeniy> LGPL
import sys, os, zipfile, re
import urllib.request
import subprocess
# 1. Get path to $USER/AppData/Roaming/FreeCAD/ folder
path = App.getUserAppDataDir()
trans_path = os.path.join(path,"translations")
ts_path = os.path.join(path,"ts")
# Create translation and FreeCAD_zip folder if it not exists
os.makedirs(trans_path, exist_ok = True)
os.makedirs(ts_path, exist_ok = True)
print("Copy youre *.ts files to folder: "+ts_path)
print("And run this script")
print("You can use subfolders in \ts folder! But path does not contain space chars!")
# 2. Check lrealase.exe utility in folder
lrel_path = os.path.join(path,"lrelase.exe")
if not os.path.exists(trans_path.replace("translations","lrelease.exe")):
print(lrel_path+" is not found... process stopped")
print("please copy lrelease.exe utilty to folder: "+trans_path.replace("translations","")+" and try again!")
sys.exit(-1)
def get_locale_from_file(path):
file = open(path, "r", encoding="utf8")
text = file.read()
locale = re.search(r'" language="[^"]+"',text).group()
locale = locale.replace('"','').replace(" language=","")
file.close()
return locale
# 3. Make list of files
print("\nScan "+ts_path+" folder...")
file_list = []
supported_locales = Gui.supportedLocales().values()
os.chdir(path)
# Walk into FreeCAD unzipped folder and lrelease all files
for dirpath, subdirs, files in os.walk(ts_path):
for file in files:
if file.endswith(".ts"):
file_path = os.path.join(dirpath, file)
locale = get_locale_from_file(file_path)
print(file_path+" ["+locale+"]")
file_list.append((file,file_path,locale))
print(str(len(file_list))+" files will be converted\n")
# 4. Translate all files with lrelease
progress=0
for file_info in file_list:
file_name,file_path,locale=file_info
out_locale = locale #"de"
#os.system("lrelease "+file_path)
FreeCADGui.updateGui()
subprocess.call("lrelease "+file_path, shell=True, cwd=path)
qm_file_path=file_path.replace(".ts",".qm")
if not os.path.exists(qm_file_path):
print("ERROR: of creation " + qm_file_path + " file, it does not exist, aborting")
sys.exit(-1)
else:
# Rename and move file
qm_file_name=file_name.replace(".ts","_"+out_locale+".qm")
qm_new_path=os.path.join(trans_path,qm_file_name)
if os.path.exists(qm_new_path):
FreeCADGui.updateGui()
os.remove(qm_new_path)
os.rename(qm_file_path,qm_new_path)
perc = 100/len(file_list)*progress
print(" {0:6.2f}% ".format(perc) + file_path.replace(path,"") +" -> "+qm_file_name)
FreeCADGui.updateGui()
progress+=1
# 5. Complete message
print("Integration of *.ts files complited!")
Console result:
Code: Select all
0.00% ts\AddonManager.ts -> AddonManager_bg.qm
3.57% ts\Arch.ts -> Arch_bg.qm
7.14% ts\Assembly.ts -> Assembly_bg.qm
10.71% ts\Complete.ts -> Complete_bg.qm
14.29% ts\draft.ts -> draft_bg.qm
17.86% ts\Drawing.ts -> Drawing_bg.qm
21.43% ts\Fem.ts -> Fem_bg.qm
25.00% ts\FreeCAD.ts -> FreeCAD_bg.qm
28.57% ts\Image.ts -> Image_bg.qm
32.14% ts\StartPage.ts -> StartPage_bg.qm
35.71% ts\TechDraw.ts -> TechDraw_bg.qm
39.29% ts\Bulgarian\Mesh.ts -> Mesh_bg.qm
42.86% ts\Bulgarian\MeshPart.ts -> MeshPart_bg.qm
46.43% ts\Bulgarian\OpenSCAD.ts -> OpenSCAD_bg.qm
50.00% ts\Bulgarian\Part.ts -> Part_bg.qm
53.57% ts\Bulgarian\PartDesign.ts -> PartDesign_bg.qm
57.14% ts\Bulgarian\Raytracing.ts -> Raytracing_bg.qm
60.71% ts\Bulgarian\ReverseEngineering.ts -> ReverseEngineering_bg.qm
64.29% ts\Bulgarian\Robot.ts -> Robot_bg.qm
67.86% ts\Bulgarian\Ship.ts -> Ship_bg.qm
71.43% ts\Bulgarian\Sketcher.ts -> Sketcher_bg.qm
75.00% ts\Bulgarian\Test.ts -> Test_bg.qm
78.57% ts\Bulgarian\Tux.ts -> Tux_bg.qm
82.14% ts\Bulgarian\Web.ts -> Web_bg.qm
85.71% ts\Other_folder\Path.ts -> Path_bg.qm
89.29% ts\Other_folder\Plot.ts -> Plot_bg.qm
92.86% ts\Other_folder\Points.ts -> Points_bg.qm
96.43% ts\Other_folder\Spreadsheet.ts -> Spreadsheet_bg.qm
Integration of *.ts files complited!