diff --git a/Base/QTCore/qMRMLTranslator.cxx b/Base/QTCore/qMRMLTranslator.cxx new file mode 100644 index 00000000000..8f2b058b1f7 --- /dev/null +++ b/Base/QTCore/qMRMLTranslator.cxx @@ -0,0 +1,8 @@ +#include + +#include "qMRMLTranslator.h" + +std::string qMRMLTranslator::Translate(const char *context, const char *sourceText, const char *disambiguation, int n) +{ + return QCoreApplication::translate(context, sourceText, disambiguation, n).toStdString(); +} diff --git a/Base/QTCore/qMRMLTranslator.h b/Base/QTCore/qMRMLTranslator.h new file mode 100644 index 00000000000..d4c18840ca7 --- /dev/null +++ b/Base/QTCore/qMRMLTranslator.h @@ -0,0 +1,16 @@ +#ifndef qMRMLTranslator_h +#define qMRMLTranslator_h + +#include "vtkMRMLTranslator.h" + +#include "qSlicerBaseQTCoreExport.h" + +class Q_SLICER_BASE_QTCORE_EXPORT qMRMLTranslator: public vtkMRMLTranslator +{ +public: + + /// Translation function for logic classes + std::string Translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) override; +}; + +#endif diff --git a/Base/QTCore/qSlicerCoreApplication.cxx b/Base/QTCore/qSlicerCoreApplication.cxx index 22df082e842..b4f61c7181f 100644 --- a/Base/QTCore/qSlicerCoreApplication.cxx +++ b/Base/QTCore/qSlicerCoreApplication.cxx @@ -85,6 +85,7 @@ #include "qSlicerModuleFactoryManager.h" #include "qSlicerModuleManager.h" #include "qSlicerUtils.h" +#include "qMRMLTranslator.h" // SlicerLogic includes #include "vtkDataIOManagerLogic.h" @@ -353,6 +354,12 @@ void qSlicerCoreApplicationPrivate::init() vtkEventBroker::GetInstance()->SetRequestModifiedCallback(modifiedRequestCallback); } + // Create the translation object used for string translation in logic classes + if (!this->AppLogic->GetTranslatorInstance()) + { + this->AppLogic->SetTranslatorInstance(new qMRMLTranslator()); + } + // Ensure that temporary folder is writable { // QTemporaryFile is deleted automatically when leaving this scope diff --git a/Libs/MRML/Logic/vtkMRMLApplicationLogic.cxx b/Libs/MRML/Logic/vtkMRMLApplicationLogic.cxx index 71b266d84f6..2f1715691b7 100644 --- a/Libs/MRML/Logic/vtkMRMLApplicationLogic.cxx +++ b/Libs/MRML/Logic/vtkMRMLApplicationLogic.cxx @@ -1254,3 +1254,32 @@ std::string vtkMRMLApplicationLogic::GetFontsDirectory() std::string fullPath = vtksys::SystemTools::JoinPath(filesVector); return fullPath; } + +//---------------------------------------------------------------------------- +std::string vtkMRMLApplicationLogic::Translate(const char *context, const char *sourceText, const char *disambiguation, int n) +{ + if (vtkMRMLApplicationLogic::TranslatorInstance) + { + return vtkMRMLApplicationLogic::TranslatorInstance->Translate(context, sourceText, disambiguation, n); + } + else + { + return sourceText; + } +} + +//---------------------------------------------------------------------------- +vtkMRMLTranslator* vtkMRMLApplicationLogic::GetTranslatorInstance() +{ + return vtkMRMLApplicationLogic::TranslatorInstance; +} + +//---------------------------------------------------------------------------- +void vtkMRMLApplicationLogic::SetTranslatorInstance(vtkMRMLTranslator* translatorInstance) +{ + if (vtkMRMLApplicationLogic::TranslatorInstance == translatorInstance) + { + return; + } + vtkMRMLApplicationLogic::TranslatorInstance = translatorInstance; +} diff --git a/Libs/MRML/Logic/vtkMRMLApplicationLogic.h b/Libs/MRML/Logic/vtkMRMLApplicationLogic.h index aceabda7e87..b3b99926f2a 100644 --- a/Libs/MRML/Logic/vtkMRMLApplicationLogic.h +++ b/Libs/MRML/Logic/vtkMRMLApplicationLogic.h @@ -27,6 +27,8 @@ #include "vtkMRMLLogicExport.h" #include "vtkMRMLSliceCompositeNode.h" +#include "vtkMRMLTranslator.h" + class vtkMRMLColorLogic; class vtkMRMLModelDisplayNode; class vtkMRMLSliceNode; @@ -289,6 +291,15 @@ class VTK_MRML_LOGIC_EXPORT vtkMRMLApplicationLogic /// Font file path is set to the one specified in FontFileName property in this object. void UseCustomFontFile(vtkTextProperty* textProperty); + /// Translation function for logic classes + std::string Translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1); + + /// @{ + /// Set the object used for string translation in logic classes + static void SetTranslatorInstance(vtkMRMLTranslator* translatorInstance); + static vtkMRMLTranslator* GetTranslatorInstance(); + /// @} + protected: vtkMRMLApplicationLogic(); @@ -316,6 +327,8 @@ class VTK_MRML_LOGIC_EXPORT vtkMRMLApplicationLogic class vtkInternal; vtkInternal* Internal; + static vtkMRMLTranslator* TranslatorInstance; + }; diff --git a/Libs/MRML/Logic/vtkMRMLTranslator.cxx b/Libs/MRML/Logic/vtkMRMLTranslator.cxx new file mode 100644 index 00000000000..94be5de5692 --- /dev/null +++ b/Libs/MRML/Logic/vtkMRMLTranslator.cxx @@ -0,0 +1,6 @@ +#include "vtkMRMLTranslator.h" + +std::string vtkMRMLTranslator::Translate(const char *vtkNotUsed(context), const char *sourceText, const char *vtkNotUsed(disambiguation), int vtkNotUsed(n)) +{ + return sourceText; +} diff --git a/Libs/MRML/Logic/vtkMRMLTranslator.h b/Libs/MRML/Logic/vtkMRMLTranslator.h new file mode 100644 index 00000000000..2153a042eb2 --- /dev/null +++ b/Libs/MRML/Logic/vtkMRMLTranslator.h @@ -0,0 +1,18 @@ +#ifndef vtkMRMLTranslator_h +#define vtkMRMLTranslator_h + +#include "vtkMRMLLogicExport.h" + +// STD includes + +#include + +class VTK_MRML_LOGIC_EXPORT vtkMRMLTranslator +{ +public: + + /// Default noop translation function for logic classes + virtual std::string Translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1); +}; + +#endif