Skip to content
Andras Lasso edited this page Jan 20, 2022 · 7 revisions

Overview

The goal of internationalization (I18N) is to provide infrastructure for creating localized version of the software, with translations to different languages and support for region-specific units and display formats.

This effort did not get much attention until CZI-EOSS funding has been received in September 2021.

Previous and related efforts

In 2013, it was decided that internationalization efforts were going to put on hold, because underlying libraries did not work well with international characters and non-US-English regional settings. Specifically, ITK/VTK/Teem/.. readers/writers failed due to changing numerical formats (see for example #3029), and they could not read/write files with international characters in the path.

Napari image viewer (open-source Python-based image viewer) recently introduced internationalization (see details). They use crowdin for crowdsourcing translations (see for example jupyterlab's project page).

FreeCAD C++ & Python, using Qt translation infrastructure and crowdin. Script for uploading/downloading translation from crowdin: https://github.com/FreeCAD/FreeCAD/blob/master/src/Tools/updatecrowdin.py.

Open questions

  • When a label can be used in multiple places (e.g., "closed curve" can be displayed in a menu or in a sentence somewhere) then it may need to be capitalized using title case or sentence case. Need to figure out a mechanism (maybe a helper function) that allows plugging in a language-dependent logic that could do a reasonable job. See this discussion for some context.

More information

Content already ported to this page:

Existing implementation

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

CMake option named Slicer_BUILD_I18N_SUPPORT must be enabled to support internationalization.

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,

label->setText(tr("Measurement"));

Translation file template

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

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="zh_CN">
<context>
<name>qSlicerMainWindow</name>
<message>
    <source>&amp;File</source>
    <translation>文件</translation>
</message>
<message>
    <source>&amp;Edit</source>
    <translation>编辑</translation>
</message>
<message>
    <source>Feedback</source>
    <translation>反馈</translation>
</message>
<message>
    <source>Import Scene</source>
    <translation>导入场景</translation>
</message>
<message>
    <source>Add Data</source>
    <translation>添加数据</translation>
</message>
</context>
</TS>

In the above example, all the texts are within the scope of qSlicerMainWindow class. So, qSlicerMainWindow 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 conveniently 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.

lupdate c:\myproject\Slicer\ -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.

lrelease test.ts

Modifications to the source code

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

#include <QTranslator>

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

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

If Slicer 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.

Scripted Module

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

parent.title = "Endoscopy"

to

parent.title = slicer.app.translate(self.__class__.__name__, "Endoscopy")

then "Endoscopy" can be shown in other languages. Similarly, other displayable strings can be handled in the same way. In endoscopy.py, classes Endoscopy and EndoscopyWidget are defined. String "Endoscopy" is used in Class Endoscopy. Strings like Path, Input Fiducial and camera are used in EndoscopyWidget. The following lines should be appended in the TS file:

<context>
        <name>Endoscopy</name>
        <message>
        <source>Endoscopy</source>
        <translation>内窥镜</translation>
        </message>
</context>
<context>
        <name>EndoscopyWidget</name>
        <message>
        <source>Path</source>
        <translation>路径</translation>
        </message>
        <message>
        <source>Camera</source>
        <translation>相机(Camera)</translation>
        </message>
        <message>
        <source>Input Fiducials</source>
        <translation>输入参考点(Input Fiducials)</translation>
        </message>
</context>

Translations for reference