Skip to content

Commit 4f44353

Browse files
authored
Add more detail to translation
1 parent 7c02143 commit 4f44353

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

technical/translation.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,67 @@ Some general guidelines when constructing strings for translation:
1616
* The NOOP-versions of the Qt translation functions extract the string for translation purposes, but *do not* replace the string in place with its translated equivalent. They should generally only be used in classes derived from Gui::Command for things like the menu entry and tooltip. The Command class handles actually loading the translated string.
1717
* In a non-QObject-derived class, use `Q_DECLARE_TR_FUNCTIONS(MyClass)` to give your class access to the `tr()` function. See also [the Qt documentation](https://doc.qt.io/qt-5/i18n-source-translation.html).
1818

19+
# Qt Translation Functions
20+
21+
## C++
22+
23+
The main translation function for C++ code is `tr()`, automatically defined in any class derived from `QObject`. It automatically sets the "context" of the translation to the name of the class, providing disambiguation between the same string in different parts of FreeCAD, where the meaning might be different. It generates a `QString` out of a string literal:
24+
```
25+
void SpecialWidget::DoThings()
26+
{
27+
this->labelThing.setText(tr("This will get translated")); // Context is "SpecialWidget"
28+
}
29+
```
30+
31+
If your class is not derived from `QObject`, include the Q_DECLARE_TR_FUNCTIONS macro when defining your class to gain the `tr()` function:
32+
```
33+
#include <QCoreApplication>
34+
35+
class NotAWidget
36+
{
37+
Q_DECLARE_TR_FUNCTIONS(NotAWidget);
38+
39+
public:
40+
NotAWidget();
41+
};
42+
```
43+
44+
Technically `tr()` can take an additional string: a "comment" that is displayed to translators, but is not itself translated. This is rarely used.
45+
46+
To do string replacement use `QString::arg()`, for example:
47+
```
48+
label.setText(tr("Item %1 of %2 confabulated. Please wait...").arg(i).arg(total));
49+
```
50+
The string "Item %1 of %2 confabulated. Please wait..." will be presented to translators, but CrowdIn will prevent them from removing the placeholders accidentally.
51+
52+
Note that in many places in the FreeCAD source code you will see uses of `QT_TR_NOOP` - this is *only* used in cases where the string is later passed through a separate call to `tr()`. If you aren't sure, check with a Maintainer, and when in doubt, `tr()` is probably what you need.
53+
54+
## Python
55+
56+
In your Python code, import FreeCAD, then create a function called `translate` with the following code:
57+
```
58+
translate = FreeCAD.Qt.translate
59+
```
60+
It then gets used similarly to `tr()` in C++, but with a manually-specified context:
61+
```
62+
print(translate("MyMod", "This will get translated"))
63+
```
64+
Note that "translate" is not a true function: it is really a tag that the lupdate utility uses to identify strings for translation. You *cannot* rename it, or use it with non-literal strings, etc. You also cannot use f-strings, or Python string concatenation:
65+
```
66+
print(translate(modNameInAVariable, someOtherVariable)) # NO!
67+
print(translate("MyMod", f"This won't {work}")) # NO!
68+
print(translate("MyMod", "This" " also " "won't work")) # NO!
69+
```
70+
To do argument replacement use the `format` function of Python's string class:
71+
```
72+
print(translate("MyMod", "There are {} widgets present, out of {} total").format(present, total))
73+
```
74+
(Note that the string sent to translators is "There are {} widgets present, out of {} total" -- the format function is called on the string that is *returned* from the translate function).
75+
76+
## UI Files
77+
78+
For the most part, all strings in UI files are automatically subject to translation. In some circumstances you may want to *disable* that translation. Using Qt Designer, uncheck the "translatable" checkbox on the widget that you want to disable translation for.
79+
1980
## Translating plurals
2081

2182
Many languages have complex pluralization rules. To support this, you can write strings that are designed for pluralization, including in English, by specifying a parameter that indicates how many of a thing there are. The translation system will dynamically select the correct translation (assuming translators wrote one), even for English. For example

0 commit comments

Comments
 (0)