-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add translation function for logic classes #7188
ENH: Add translation function for logic classes #7188
Conversation
mhdiop
commented
Aug 23, 2023
•
edited
edited
- Mark all user-visible strings as translatable in C++ SlicerLanguagePacks#12
- Translate user-visible text coming from logic classes #6647
- Some strings cannot be translated to other languages #6177
This comment was marked as outdated.
This comment was marked as outdated.
As outlined in the build log1, the linking is currently failing on Linux:
Footnotes |
Libs/MRML/Logic/vtkMRMLTranslator.h
Outdated
#ifndef vtkMRMLTranslator_h | ||
#define vtkMRMLTranslator_h | ||
|
||
class vtkMRMLTranslator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class vtkMRMLTranslator | |
class VTK_MRML_LOGIC_EXPORT vtkMRMLTranslator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this comment, the update is done. May I know why we need to add VTK_MRML_LOGIC_EXPORT
before the class name ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding VTK_MRML_LOGIC_EXPORT
designates the class to be visible on the public API of the shared library. VTK_MRML_LOGIC_EXPORT
is a compiler/linker-specific macro defined in an automatically generated .h file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I better understand now !
Base/QTCore/qMRMLTranslator.h
Outdated
|
||
#include "vtkMRMLTranslator.h" | ||
|
||
class qMRMLTranslator: public vtkMRMLTranslator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class qMRMLTranslator: public vtkMRMLTranslator | |
class Q_SLICER_BASE_QTCORE_EXPORT qMRMLTranslator: public vtkMRMLTranslator |
e6f2233
to
711c669
Compare
|
||
#include "qSlicerBaseQTCoreExport.h" | ||
|
||
class Q_SLICER_BASE_QTCORE_EXPORT qMRMLTranslator: public vtkMRMLTranslator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this class is derived from a VTK base class, it has to be a VTK class, i.e., vtkMRMLTranslator
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular class should probably be called qSlicerQtVTKTranslationBridge
?
Note that since its role is to establish a "bridge" so that VTK logic classes can indirectly leverage Qt translation capabilities through polymorphism, it doesn't have to strictly start with the prefix vtk
.
That said, may be the name vtkSlicerQtVTKTranslationBridge
would be better ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vtk
prefix is an important hint about how the class is allowed to be constructed and used. vtk...
classes are based on vtkObject
and rely on internal reference counting. So, for clarity and consistency we either use a proper VTK class (with appropriate naming, internal reference counting, private constructor, etc.) or we don't make the translator a VTK class at all. We could make it a Qt class (derived from QObject, following Qt conventions) then, or just make it a simple C++ class (we establishing all rules).
Since initialization and destruction of singleton classes is a difficult topic, I would use already established singleton patterns in Slicer or VTK. The current design seems to be some custom implementation, which may not work correctly in a large application, composed of many shared libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re: memory
Inheriting from vtkObject
make sense and will allow to use vtkNew
(that way we are consistent and we don't need to make use of std::shared_ptr
)
re: established singleton patterns in Slicer or VTK
I am not using our established pattern made available through vtkSingleton.h is needed here.
Simply instantiating and setting using the following should be sufficient:
vtkNew<vtkMRMLTranslator> translator;
this->AppLogic->SetTranslatorInstance(translator);
// Create the translation object used for string translation in logic classes | ||
if (!this->AppLogic->GetTranslatorInstance()) | ||
{ | ||
this->AppLogic->SetTranslatorInstance(new qMRMLTranslator()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qMRMLTranslator is not a Qt class, and it is not taking care of destroying itself if it is created by new. Instead, it is a VTK class, which can be created for example by vtkNew:
vtkNew<vtkMRMLTranslator> translator;
this->AppLogic->SetTranslatorInstance(translator);
@@ -316,6 +327,8 @@ class VTK_MRML_LOGIC_EXPORT vtkMRMLApplicationLogic | |||
class vtkInternal; | |||
vtkInternal* Internal; | |||
|
|||
static vtkMRMLTranslator* TranslatorInstance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is this going to be deleted?
How can you ensure that it is deleted at the right time (not too early - when something may still call it; but not too late - after the VTK leak detection mechanism already counted all the non-deleted VTK objects)?
How would we implement this correctly on Windows, Linux, and macOS? We should not reinvent this from scratch but follow a singleton pattern already used in Slicer or VTK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this particular case, I don't think we need to use vtkSingleton.h helper class.
Since the lifecycle of this class is driven by vtkMRMLApplicationLogic
(itself instantiated and deleted by the main application), the reference to vtkMRMLTranslator
will be released upon destruction of vtkMRMLApplicationLogic
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for making a simple vtk class that is owned by the application logic.
Superseded by #7210 |