About how to optimize and improve translation of commands

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

About how to optimize and improve translation of commands

Post by Evgeniy »

If we open Command.cpp from Fem worbench https://github.com/FreeCAD/FreeCAD/blob ... ommand.cpp
And what we look repeated strings in each ": Command" :

Code: Select all

sGroup          = QT_TR_NOOP("Fem");
...
sGroup          = QT_TR_NOOP("Fem");
...
sGroup          = QT_TR_NOOP("Fem");
run lupdate

Code: Select all

lupdate Command.cpp -ts Fem_ru.ts
And we get a lot of absolutely identical strings with the same translation.
<context>
<name>CmdFemConstraintBearing</name>
<message>
<location filename="Command.cpp" line="295"/>
<source>Fem</source>

<translation type="unfinished"></translation>
</message>
...
<context>
<name>CmdFemConstraintFixed</name>
<message>
<location filename="Command.cpp" line="424"/>
<source>Fem</source>

<translation type="unfinished"></translation>
</message>
...
<context>
<name>CmdFemConstraintGear</name>
<message>
<location filename="Command.cpp" line="553"/>
<source>Fem</source>

<translation type="unfinished"></translation>
</message>
This is bad.

Open Command.cpp again and replace all

Code: Select all

QT_TR_NOOP("Fem");
to

Code: Select all

QT_TRANSLATE_NOOP("WorkbenchName","Fem");
Save file as Commandcomp.cpp
And run lupdate again:

Code: Select all

lupdate Commandcomp.cpp -ts Femcomp_ru.ts
And we get only one string for translate name of workbench for all commands:

Code: Select all

<context>
    <name>WorkbenchName</name>
    <message>
        <location filename="Commandcomp.cpp" line="295"/>
        <location filename="Commandcomp.cpp" line="336"/>
        <location filename="Commandcomp.cpp" line="381"/>
        <location filename="Commandcomp.cpp" line="424"/>
        <location filename="Commandcomp.cpp" line="466"/>
        <location filename="Commandcomp.cpp" line="509"/>
        <location filename="Commandcomp.cpp" line="553"/>
        <location filename="Commandcomp.cpp" line="594"/>
        <location filename="Commandcomp.cpp" line="639"/>
        <location filename="Commandcomp.cpp" line="682"/>
        <location filename="Commandcomp.cpp" line="724"/>
        <location filename="Commandcomp.cpp" line="769"/>
        <location filename="Commandcomp.cpp" line="814"/>
        <location filename="Commandcomp.cpp" line="860"/>
        <location filename="Commandcomp.cpp" line="903"/>
        <location filename="Commandcomp.cpp" line="1039"/>
        <location filename="Commandcomp.cpp" line="1095"/>
        <location filename="Commandcomp.cpp" line="1251"/>
        <location filename="Commandcomp.cpp" line="1277"/>
        <location filename="Commandcomp.cpp" line="1303"/>
        <location filename="Commandcomp.cpp" line="1329"/>
        <location filename="Commandcomp.cpp" line="1357"/>
        <location filename="Commandcomp.cpp" line="1414"/>
        <location filename="Commandcomp.cpp" line="1440"/>
        <location filename="Commandcomp.cpp" line="1466"/>
        <location filename="Commandcomp.cpp" line="1606"/>
        <location filename="Commandcomp.cpp" line="1651"/>
        <source>Fem</source>
        <translation type="unfinished"></translation>
    </message>
</context>
Profit!
We remove repeated garbage from Crowdin, decrase size of *.ts files and saved translators from unnecessary work.

Size and strings numbers comparsion of ts files:

Fem_ru.ts (old)
24,8Kb
691 strings

Femcomp_ru.ts (by this method)
22,4Kb
590 strings
Qt Designer.zip
(4.34 KiB) Downloaded 49 times
Last edited by Evgeniy on Sat Sep 25, 2021 6:02 pm, edited 7 times in total.
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: Idea about how to optimize and improve translation of commands

Post by Evgeniy »

And perhaps this will help to solve one very BIG other problem that for some reason is do not solved...

https://github.com/FreeCAD/FreeCAD-tran ... /issues/61
User avatar
chennes
Veteran
Posts: 3904
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: About how to optimize and improve translation of commands

Post by chennes »

I don't actually understand why the change from QT_TR_NOOP (which is correct in this context) to QT_TRANSLATE_NOOP does this (and that usage is not correct, you'd typically set the first argument of QT_TRANSLATE_NOOP to the name of the class you want it to be treated as part of, which is done automatically by QT_TR_NOOP). Nevertheless, CrowdIn should be removing the duplicated entries and collapsing them to one, right? If we really, really didn't want to have those duplicates in the TS file, we could create a static variable at the top of the file that contained the translated string, I suppose.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: About how to optimize and improve translation of commands

Post by Evgeniy »

chennes wrote: Sat Sep 25, 2021 6:43 pm I don't actually understand why the change from QT_TR_NOOP (which is correct in this context) to QT_TRANSLATE_NOOP does this (and that usage is not correct
I think all logical correct.
QT_TR_NOOP - get context from class name. For example:

Code: Select all

CmdFemConstraintBearing::CmdFemConstraintBearing()
  : Command("FEM_ConstraintBearing")
{
    sAppModule      = "Fem";
    sGroup          = QT_TR_NOOP("Fem");
    sMenuText       = QT_TR_NOOP("Constraint bearing");
QT_TR_NOOP("Fem") is equivalent to QT_TRANSLATE_NOOP("CmdFemConstraintBearing","Fem")

in QT_TRANSLATE_NOOP("CmdFemConstraintBearing","Fem") we specify context explicitly

in QT_TR_NOOP("Fem") context is getted from CmdFemConstraintBearing::CmdFemConstraintBearing() - name of class.

And because of this, the same translation will appear in different contexts.
But if we set context manually for example "WorkbenchName" or "Workbench" with QT_TRANSLATE_NOOP, lupdate utility will assume that this is only one translation but it is located in several places. And it pack all repeats in one <message></message> block.

We just need to agree (create a rule and follow it) on which key context name we will use with sGroup values of commands and insert this context in all Command.cpp an Commands.py files in all places where is sGroup value exists.
Last edited by Evgeniy on Sat Sep 25, 2021 7:07 pm, edited 1 time in total.
User avatar
chennes
Veteran
Posts: 3904
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: About how to optimize and improve translation of commands

Post by chennes »

Ah, that makes sense, thanks for the explanation. You are correct, it does make sense in this case to use QT_TRANSLATE_NOOP instead (at least, in my opinion it does). Let me ping Yorik and Bernd and see if they want to weigh in on it.

bernd wrote:*
yorik wrote:*
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: About how to optimize and improve translation of commands

Post by Evgeniy »

chennes wrote: Sat Sep 25, 2021 7:06 pm Let me ping Yorik and Bernd and see if they want to weigh in on it.
Sure. I think we need to do something about it.
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: About how to optimize and improve translation of commands

Post by Evgeniy »

Im make simple request https://github.com/FreeCAD/FreeCAD/pull/5068
For optimize sketcher at first, when it have more 100 duplicates.

It is necessary for Yorik to express his opinion, because he knows all the subtleties of Crowdin well.
But, I did not understand about the checkboxes under message. I wrote "something" there.
Remarks is welcome.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: About how to optimize and improve translation of commands

Post by bernd »

chennes wrote: Sat Sep 25, 2021 7:06 pm Ah, that makes sense, thanks for the explanation. You are correct, it does make sense in this case to use QT_TRANSLATE_NOOP instead (at least, in my opinion it does). Let me ping Yorik and Bernd and see if they want to weigh in on it.

bernd wrote:*
yorik wrote:*
Great to see some progress on translation. Sorry guys I can not say much in this regard. I spent some time on transaltion problems for FEM a few years ago. I realised since FEM was at that time the only real module which hat Python and C++ it would not easy and fast to get the translation backt to working smooth.

I never came back to translation stuff, to much other work on FEM ...
User avatar
yorik
Founder
Posts: 13659
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: About how to optimize and improve translation of commands

Post by yorik »

Sorry forgot to reply here yesterday...

Yes, IMHO the use of translate() and QT_TRANSLATE_NOOP is always better than tr() and QT_TR_NOOP. The latter is just a simplification that too often is of no use at all, and only complicates the translators job. It is basically always better to specify the context manually.

I think we could put up some kind of rules, like:

1) by default the context is the module/workbench name
2) workbench names use "Workbench"
3) property descriptions use "App::Property"
4) command menutexts and tooltips use the command name (not the class name, but the FreeCAD command name defined with Gui.addCommand)
5) if you have specific reasons not to use the workbench name (ex. you specifically want to use a subcontext for some reason), then go for it!
wmayer
Founder
Posts: 20298
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: About how to optimize and improve translation of commands

Post by wmayer »

Evgeniy wrote: Sat Sep 25, 2021 7:01 pm We just need to agree (create a rule and follow it) on which key context name we will use with sGroup values of commands and insert this context in all Command.cpp an Commands.py files in all places where is sGroup value exists.
It's all correct what you write but there is one little issue: it won't work. At least not the way how it is now.

The method getGroupName() returns the member variable sGroup of a command and this method is used in several dialogs like this:

Code: Select all

QString text = qApp->translate(cmd->className(), cmd->getGroupName());
This means the context string is the Command class name itself and when you change the context string inside the class to "Workbench" then qApp->translate() will fail to find the translation. At the moment it works because the .ts files still contain the different contexts of the group name. But as soon as the .ts file are cleaned-up it will stop working.

So, what needs to be done? In order to get it working you must also explicitly set the context string to "Workbench" in all locations where getGroupName() is used for translation.

Furthermore, IMO there is no point to change

Code: Select all

sGroup = QT_TR_NOOP("Sketcher");
inside all Sketcher Command classes to

Code: Select all

sGroup = QT_TRANSLATE_NOOP("Workbench","Sketcher");
It's sufficient to simply write

Code: Select all

sGroup = "Sketcher";
and additionally put somewhere a (single) line like this:

Code: Select all

#if 0 // needed for Qt's lupdate utility
    qApp->translate("Workbench", "Sketcher");
#endif
Since this line already exists inside Workbench.cpp of the Sketcher module you don't have to do it twice.

And just for clarification: either we change the group context string everywhere (i.e. in all Command classes of all modules) or nowhere. It won't work to change it only inside one module and not inside all others.
Post Reply