Slicer4:Internationalization of Slicer

From Slicer Wiki
Jump to: navigation, search
Home < Slicer4:Internationalization of Slicer

Back to Slicer 4 Developers


This pages contains notes on source code additions and modifications of different modules within Slicer for the internationalization purpose.

QT-based Internationalization

Internationalization is well supported in QT. A lot of modules of Slicer are developed on QT. This has set a good ground for the internationalization of Slicer.

  • basic idea

In Qt, tr() is one of the static public members of QObject class, which returns a translated version of input sourceText.

   QString QObject::tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 ) [static]

tr() will search the translation in a so-called translation file. If nothing is found, then the sourceText instead of the translated version will be displayed directly. For example,

  • translation file template

The following is an example of the translation file, from English to Chinese.

   <?xml version="1.0" encoding="utf-8"?>
   <TS version="2.0" language="zh_CN">
Import Scene
Add Data

In the above example, all the texts are within the scope of qSlicerMainWindow class. So, 'qSlicerMainWindo' occurs in section 'name'.

  • Prepare translation file

The translation file can be prepared manually based on the above template and file name extension should be like .ts, for example, lang_hz.ts, lang_de.hz, etc. Translation file can also be created conviently by a tool called lupdate. lupdate is a command line tool that finds the translatable strings (tr())in the specified source, header and Qt Designer interface files, and produces or updates .ts translation files. TS files are text files, which can be viewed or edited by any text editor tools. The following command searchs the folder and subfolders for tr() occurences in files (.ui, .h, .cxx) and create translation file test.ts.

      Usage: lupdate c:\myproject\slicer4\ -ts   test.ts

lrelease is a command line tool that produces QM files out of TS files. The QM file format is a compact binary format that is used by the localized application. It provides extremely fast lookups for translations.

      Usage: lrelease test.ts
  • Modifications to the source codes:

In file \Slicer4\Base\QTCore\qSlicerCoreApplication.h, add the following line:

   #include <QTranslator>

In file \Slicer4\Applications\SlicerQT\main.cxx (around line#230), add the following lines:

   static QTranslator* translator;
   if (translator != NULL)
       delete translator;
       translator = NULL;
   QString langCode= argv[1];
   translator = new QTranslator;
   QString qmFilename = "lang_" + langCode; 
   if (translator->load(qmFilename))

If slicer4 launches with the first argument being 'zh', lang_zh.qm will be installed. The extension .qm doesn't need to be specified, for installTranslator only loads QM file.

  • To be continued

Scripted Module

Some modules are implemented in Python. Endoscopy is one of the typical examples. In, if we change the code in line#10

   parent.titel = “Endoscopy”


   parent.title =, "Endoscopy")

then "Endoscopy" can be shown in other languages.Similarily, other displayable strings can be handled in the same way. In, classes 'Endoscopy' and 'EndoscopyWidget' are defined. String "Endoscopy" is used in Class 'Endoscopy'. String 'Path' is used in 'EndoscopyWidget'. The following lines should be appended in the TS file:


To be continued