-
Notifications
You must be signed in to change notification settings - Fork 140
Internationalization in macros
Gaël Écorchard edited this page Jun 2, 2019
·
8 revisions
To internationalize a macro:
For Qt4 add the Python function
from PySide import QtGui
def tr(context, text):
try:
_encoding = QtWidgets.QApplication.UnicodeUTF8
return QtGui.QApplication.translate(context, text, None, _encoding)
except AttributeError:
return QtGui.QApplication.translate(context, text, None)
For Qt5 add the Python function
from PySide2 import QtWidgets
def tr(context, text):
return QtWidgets.QApplication.translate(context, text, None)
The function can be named differently but it should have at least two arguments for pyside2-lupdate
to work. context
must be the name of the macro.
Let's explain the steps to translate the macro DxfToSketch
into French as example.
- Use
tr()
for each of the macro string, e.g.tr('DxfToSketch', 'Install now?')
, where the first argument, the context, must be the macro name, and the second one, the string to translate. - Make a temporary symbolic link link or a copy of the macro into the
translations
directory. For exampleln -s Conversion/DxfToSketchLayers.FCMacro Conversion/translations/DxfToSketchLayers.py
. This must be done becausepyside2-lupdate
works only with*.py
files. -
pyside2-lupdate translations/DxfToSketchLayers.py -ts translations/DxfToSketchLayers.ts
. - Edit
translations/DxfToSketchLayers.ts
with Qt's Linguist. - Create
translations/DxfToSketchLayers.qm
with menuFile/Release
in Qt Linguist. - Create
DxfToSketchLayers.qrc
includingtranslations/DxfToSketchLayers.qm
as shown below. - Compile the resource file into Python with
pyside2-rcc -o DxfToSketchLayers_rc.py DxfToSketchLayers.qrc
orpyside2-rcc -py3 -o DxfToSketchLayers_rc.py DxfToSketchLayers.qrc
. - Add
import DxfToSketchLayers_rc
into the macro. - Add
gui.addLanguagePath(os.path.join(app.getUserMacroDir(False), 'translations'))
into the macro, with the associatedimport FreeCADGui as gui
.
DxfToSketchLayers.qrc
:
<RCC>
<qresource>
<file>translations/DxfToSketchLayers.qm</file>
</qresource>
</RCC>
UNFINISHED, DOESN'T WORK, HELP REQUIRED!