You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: technical/translation.md
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,67 @@ Some general guidelines when constructing strings for translation:
16
16
* 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.
17
17
* 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).
18
18
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:
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
+
19
80
## Translating plurals
20
81
21
82
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