Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QTranslator Class

TheQTranslator class provides internationalization support for text output.More...

Header:#include <QTranslator>
Inherits:QObject

Public Functions

QTranslator(QObject * parent = 0)
~QTranslator()
virtual boolisEmpty() const
boolload(const QString & filename, const QString & directory = QString(), const QString & search_delimiters = QString(), const QString & suffix = QString())
boolload(const QLocale & locale, const QString & filename, const QString & prefix = QString(), const QString & directory = QString(), const QString & suffix = QString())
boolload(const uchar * data, int len)
virtual QStringtranslate(const char * context, const char * sourceText, const char * disambiguation = 0) const
QStringtranslate(const char * context, const char * sourceText, const char * disambiguation, int n) const
  • 29 public functions inherited fromQObject

Additional Inherited Members

  • 1 property inherited fromQObject
  • 1 public slot inherited fromQObject
  • 1 signal inherited fromQObject
  • 7 static public members inherited fromQObject
  • 8 protected functions inherited fromQObject

Detailed Description

TheQTranslator class provides internationalization support for text output.

An object of this class contains a set of translations from a source language to a target language.QTranslator provides functions to look up translations in a translation file. Translation files are created usingQt Linguist.

The most common use ofQTranslator is to: load a translation file, install it usingQApplication::installTranslator(), and use it viaQObject::tr(). Here's themain() function from theHello tr() example:

int main(int argc,char*argv[]){QApplication app(argc, argv);QTranslator translator;    translator.load("hellotr_la");    app.installTranslator(&translator);QPushButton hello(QPushButton::tr("Hello world!"));    hello.resize(100,30);    hello.show();return app.exec();}

Note that the translator must be createdbefore the application's widgets.

Most applications will never need to do anything else with this class. The other functions provided by this class are useful for applications that work on translator files.

Looking up Translations

It is possible to look up a translation usingtranslate() (astr() andQApplication::translate() do). Thetranslate() function takes up to three parameters:

  • Thecontext - usually the class name for thetr() caller.
  • Thesource text - usually the argument totr().
  • Thedisambiguation - an optional string that helps disambiguate different uses of the same text in the same context.

For example, the "Cancel" in a dialog might have "Anuluj" when the program runs in Polish (in this case the source text would be "Cancel"). The context would (normally) be the dialog's class name; there would normally be no comment, and the translated text would be "Anuluj".

But it's not always so simple. The Spanish version of a printer dialog with settings for two-sided printing and binding would probably require both "Activado" and "Activada" as translations for "Enabled". In this case the source text would be "Enabled" in both cases, and the context would be the dialog's class name, but the two items would have disambiguations such as "two-sided printing" for one and "binding" for the other. The disambiguation enables the translator to choose the appropriate gender for the Spanish version, and enables Qt to distinguish between translations.

Using Multiple Translations

Multiple translation files can be installed in an application. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched for translations first and the earliest translation file is searched last. The search stops as soon as a translation containing a matching string is found.

This mechanism makes it possible for a specific translation to be "selected" or given priority over the others; simply uninstall the translator from the application by passing it to theQApplication::removeTranslator() function and reinstall it withQApplication::installTranslator(). It will then be the first translation to be searched for matching strings.

See alsoQApplication::installTranslator(),QApplication::removeTranslator(),QObject::tr(),QApplication::translate(),I18N Example,Hello tr() Example,Arrow Pad Example, andTroll Print Example.

Member Function Documentation

QTranslator::QTranslator(QObject * parent = 0)

Constructs an empty message file object with parentparent that is not connected to any file.

QTranslator::~QTranslator()

Destroys the object and frees any allocated resources.

[virtual]bool QTranslator::isEmpty() const

Returns true if this translator is empty, otherwise returns false. This function works with stripped and unstripped translation files.

bool QTranslator::load(constQString & filename, constQString & directory = QString(), constQString & search_delimiters = QString(), constQString & suffix = QString())

Loadsfilename +suffix (".qm" if thesuffix is not specified), which may be an absolute file name or relative todirectory. Returns true if the translation is successfully loaded; otherwise returns false.

Ifdirectory is not specified, the directory of the application's executable is used (i.e., asapplicationDirPath()).

The previous contents of this translator object are discarded.

If the file name does not exist, other file names are tried in the following order:

  1. File name withoutsuffix appended.
  2. File name with text after a character insearch_delimiters stripped ("_." is the default forsearch_delimiters if it is an empty string) andsuffix.
  3. File name stripped withoutsuffix appended.
  4. File name stripped further, etc.

For example, an application running in the fr_CA locale (French-speaking Canada) might call load("foo.fr_ca", "/opt/foolib"). load() would then try to open the first existing readable file from this list:

  1. /opt/foolib/foo.fr_ca.qm
  2. /opt/foolib/foo.fr_ca
  3. /opt/foolib/foo.fr.qm
  4. /opt/foolib/foo.fr
  5. /opt/foolib/foo.qm
  6. /opt/foolib/foo

bool QTranslator::load(constQLocale & locale, constQString & filename, constQString & prefix = QString(), constQString & directory = QString(), constQString & suffix = QString())

Loadsfilename +prefix +ui language name +suffix (".qm" if thesuffix is not specified), which may be an absolute file name or relative todirectory. Returns true if the translation is successfully loaded; otherwise returns false.

The previous contents of this translator object are discarded.

If the file name does not exist, other file names are tried in the following order:

  1. File name withoutsuffix appended.
  2. File name with ui language part after a "_" character stripped andsuffix.
  3. File name with ui language part stripped withoutsuffix appended.
  4. File name with ui language part stripped further, etc.

For example, an application running in the locale with the followingui languages - "es", "fr-CA", "de" might call load(QLocale::system(), "foo", ".", "/opt/foolib", ".qm").load() would replace '-' (dash) with '_' (underscore) in the ui language and then try to open the first existing readable file from this list:

  1. /opt/foolib/foo.es.qm
  2. /opt/foolib/foo.es
  3. /opt/foolib/foo.fr_CA.qm
  4. /opt/foolib/foo.fr_CA
  5. /opt/foolib/foo.de.qm
  6. /opt/foolib/foo.de
  7. /opt/foolib/foo.fr.qm
  8. /opt/foolib/foo.fr
  9. /opt/foolib/foo.qm
  10. /opt/foolib/foo.
  11. /opt/foolib/foo

On operating systems where file system is case sensitive,QTranslator also tries to load a lower-cased version of the locale name.

This function was introduced in Qt 4.8.

bool QTranslator::load(constuchar * data,int len)

This function overloadsload().

Loads the QM file datadata of lengthlen into the translator.

The data is not copied. The caller must be able to guarantee thatdata will not be deleted or modified.

[virtual]QString QTranslator::translate(constchar * context, constchar * sourceText, constchar * disambiguation = 0) const

Returns the translation for the key (context,sourceText,disambiguation). If none is found, also tries (context,sourceText, ""). If that still fails, returns an empty string.

If you need to programatically insert translations in to aQTranslator, this function can be reimplemented.

See alsoload().

QString QTranslator::translate(constchar * context, constchar * sourceText, constchar * disambiguation,int n) const

This function overloadstranslate().

Returns the translation for the key (context,sourceText,disambiguation). If none is found, also tries (context,sourceText, ""). If that still fails, returns an empty string.

Ifn is not -1, it is used to choose an appropriate form for the translation (e.g. "%n file found" vs. "%n files found").

See alsoload().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp