Translating an external workbench/it

In the following notes, should be your addon's or workbench's name, for example,  or, or whatever. Capitalization matters here: is not the same as  for example. The context makes it so that all translation of your code will be gathered under the same name, to be more easily identified by translators. That is, they will know exactly to which addon or workbench a particular string belongs.

Note: Here is an all-in-one script that automates the complete procedure mentioned below (you are still advised to read the procedure to know what the script should do, though): https://github.com/yorikvanhavre/BIM_Workbench/blob/master/utils/updateTranslations.py

General

 * Add a folder. You can name it to something else, but this will be easier as it is the same throughout FreeCAD. In this folder, you will place the  files (the "source" translation files) and  files (compiled translation files).
 * Only the text that is shown to the user in the FreeCAD UI should be translated. Text that is only shown in the Python console should not be translated.
 * Text that prints to the is shown in the "Report view", and therefore should be translated. The "Report view" is different from the Python console.

In every Python .py file

 * In every file where you need to translate text, you need a function defined. You can use the fully-qualified name from Qt, but it's a little cleaner to use:




 * All text that must be translated must be passed through the function:




 * becomes:




 * Be aware that is not just a normal function: it also serves as a "tag" for the  text-processing utility, so must be named exactly "translate". The  program is a simple text processor, it does not execute your code. You must pass string literals directly to the  function: you cannot pass variables, constants, etc. For example:




 * This can be used anywhere: in, in , in Qt dialogs, etc. The functions do not automatically add the newline character , so this must be added at the end if desired. This character doesn't need translation either, so it can be outside the translating function:




 * If you are using files made with QtDesigner, nothing special needs to be done with them.
 * When creating new objects, do not translate the object's "Name". Rather, translate the object's "Label". The difference is that a "Name" is unique; it stays the same throughout the life of the object; on the other hand, a "Label" can be changed by the user as desired.
 * When creating properties for your objects, don't translate the property name. But place the description inside :




 * Don't use your own in this specific case. Keep.


 * Do not translate the text of document transactions made with

In InitGui.py

 * Add the following line near the top of the file:




 * The macro doesn't do anything, but it marks texts to be picked up by the  utility later on. We only use it in special cases where FreeCAD itself takes care of everything.


 * To translate menu and toolbar names use the word as the context:




 * Add the path to your folder in the Initialized function:




 * The file has no file attribute, so it is not easy to find the translations folder's relative location. An easy way to work around this is to make it import another file from the same folder, and in that file do:



Inside each FreeCAD command class

 * Add the following line near the top of the file:




 * Translate the and  of the command like this:




 * where is the name of the command, defined by:



Gather all the strings from your module

 * You will need the, , and  tools installed on your system. In Linux distributions they usually come in packages named  or . On some systems  is named  or  or  or similar. Same for the other tools. You may use the Qt4 or Qt5 version at your choice.
 * If you have files, you need to run  first:




 * This is recursive and will find files inside your whole directory structure.


 * If you have files, you need to run  too:




 * If you ran both operations, you now need to unify these two files into one:




 * Check the contents of the three files to make sure that they contain the strings, then you can delete both  and.
 * You can do it all in one bash script like this:



Send the .ts file to a translation platform
It is time to have your file translated. You can choose to set up an account on a public translation platform such as Crowdin or Transifex, or you can benefit from our existing FreeCAD-addons account at Crowdin, which has many users already, and therefore more chance to have your file translated quickly and by people who know FreeCAD.

If you wish to host your file on the FreeCAD Crowdin account, get in touch with Yorik on the FreeCAD forum.

some platforms like Crowdin can integrate with GitHub and do all the process from points 2, 3 and 4 automatically. For that, you can't use the FreeCAD Crowdin account; you will need to set up your own account.

Merge the translations
Once your file has been translated, even if partially, you can download the translations from the site:


 * You will usually download a file containing one  per language
 * Place all the translated files, together with your base  file, in the  folder

Compile the translations
Now run the program on each file that you have:

You can automate the process:

You should find one file for each translated  file. The files is what will be used by Qt and FreeCAD at runtime.

That's all you need. Note that certain parts of your workbench cannot be translated on-the-fly if you decide to switch languages. If this is the case, you will need to restart FreeCAD for the new language to take effect.

Testing translations

 * 1) Switch FreeCAD to a language you have translated (for example German)
 * 2) Load translation into FreeCAD, ex.
 * 3) Test something, for example

Result: This should give you the German translation. If this works, then the basic setup is OK. Then we can look at something else. For example, command names should always use a special context that is the name of the command as registered to FreeCAD.

Important notes

 * Make sure you are using a *context* and *string* that actually are in the ts/qm file of course.

Convenience script
Yorik maintains a convenience script for the BIM workbench, that can gather, upload and download ts files. You can just copy and adapt that script for your workbench:

https://github.com/yorikvanhavre/BIM_Workbench/blob/master/utils/updateTranslations.py

Technical details and advanced usage
In the above examples there are two separate functions being used: and. You may also run across and, which automatically provide the "context" argument based on their calling location. These two pairs of functions are fundamentally different.

and accomplish two separate tasks: at runtime they perform the actual translation from the string passed into them to the final translated string. This is true whether they are provided a literal string, or a variable, or a constant: the lookup is dynamic and real-time during the run of the code. However, they provide an additional non-runtime function: they are recognized by the utility. If (and only if) they contain a string literal, that literal is extracted by the utility. ONLY string literals are extracted by -- if a variable is passed it is ignored by the  utility. Qt will attempt to provide a translation at runtime, but this will only work if some other piece of code called one of the translation functions with the literal string that needs to be translated, so that can extract it. Note that the code with the string literal need not actually ever execute, it must simply exist as a line of code in a file somewhere: performs no analysis or code execution, it is simply performing a string search and extraction.

In contrast, and  do nothing at all at runtime: they are literal "no-ops", and are completely ignored by running code. Their only use is to mark a literal string for extraction by : it never makes sense to place a variable within a call to one of these functions, it will have no effect. They are used in circumstances where or  will be called with a variable containing the text to translate. For example, any code that is used to create a Command or a Property will use a NOOP-type function around the command menu text or tooltip, or the property docstring: at runtime when FreeCAD displays these items to the user it calls : the literal strings must have been extracted by at the point of creation, for example:

In this usage, at runtime the dictionary returned by this function is literally:

There is no reference to any type of translation information. When FreeCAD actually displays this information to the user, the pseudo-code is:

In this case, cannot extract any string from the call to  because it refers to a variable. So ignores that call, but at runtime Qt searches for the string that's passed to it. As long as someplace in the code there is a call to one of the translation functions with a matching literal string (in this case, in the function), this translation call will succeed.

To verify that the expected strings are being extracted, you can manually run the command:

The file will contain the set of strings that are uploaded to CrowdIn for translation.

Important references

 * Why and how to translate functions (forum thread)

Pagine correlate

 * Ambienti esterni
 * Localizzazione