LocaleHandler

Since: BlackBerry 10.0.0

#include <bb/cascades/LocaleHandler>

Enables dynamic translation and locale updates in C++ and QML.

When the language or locale of the device changes, this class is responsible for dynamically updating text with any necessary changes. If you're using QML, this class represents a more advanced approach to dynamic translation, since it enables you to perform other tasks related to localization, such as updating images with localized images.

In a QML-defined UI, if you're only required to update text properties, the Retranslate class represents a simpler way of providing dynamic updates.

Here's an example of how to connect the languageChanged() signal to a slot where text language can be updated. Calling tr() will update text with the translated text given that translation keys are available for the given language.

void MyClass::init() {
  myTranslator = new QTranslator();
  myLocaleHandler = new LocaleHandler(this);
  QObject::connect(myLocaleHandler, SIGNAL(systemLanguageChanged()),
                   this, SLOT(onSystemLanguageChanged()));
  QObject::connect(myLocaleHandler, SIGNAL(languageChanged()),
                   this, SLOT(onLanguageChanged()));
}

// Called when the system language is changed
void MyClass::onSystemLanguageChanged() {
  QLocale systemLocale;
  if (systemLocale.language() == QLocale::Swedish) {
      myTranslator->load("path/to/swedish/translator");
  } else if (systemLocale.language() == QLocale::English) {
      myTranslator->load("path/to/english/translator");
  } else {
      myTranslator->load("path/to/default/translator");
  }   
  QCoreApplication::instance()->installTranslator(myTranslator);
}

// Called when new translators are installed
void MyClass::onLanguageChanged() {
  someLabel->setText(tr("translate me"));
}

Here's how to add a LocaleHandler as an attachedObject to a label, in QML. The languageChanged() signal is emitted when a translator is installed using the QCoreApplication::installTranslator() function.

From the onLanguageChanged signal handler it is also possible to do more advanced locale specific updates like changing images that depend on locale.

Label {
  id: localeHandlerLabel
  objectName: "localeHandlerLabel"
  text: qsTr("LocaleHandler")

  attachedObjects: [
      LocaleHandler  {
          id: myLocaleHandler
          onLanguageChanged: {
              localeHandlerLabel.text = qsTr("LocaleHandler")
          }
      }
  ]
}


Overview

Inheritance

bb::cascades::BaseObject
bb::cascades::LocaleHandler

QML properties

Only has inherited QML properties

objectName: QStringInherited
parent: QObject [read-only]Inherited

Properties Index

Only has inherited properties

QStringobjectNameInherited
QObjectparent [read-only]Inherited

Public Functions Index

LocaleHandler (QObject *parent=0)
virtual ~LocaleHandler ()
voidsetObjectName (const QString &name)Inherited
virtual Q_INVOKABLE QStringtoDebugString () const Inherited

Protected Functions Index

Only has inherited protected functions

BaseObject (QObject *parent=0)Inherited

Signals Index

voidlanguageChanged ()
voidlocaleChanged ()
voidlocaleOrLanguageChanged ()
voidsystemLanguageChanged ()
voidcreationCompleted ()Inherited
voidobjectNameChanged (const QString &objectName)Inherited

Properties

(Only has inherited properties)

QString objectNameInherited

This property is overridden from QObject.

See also:

QObject::objectName().

Since:

BlackBerry 10.0.0

QObject parentInherited[read-only]

A read-only property that represents this object's parent.

The parent of an object is specified using QObject::setParent(QObject*). The purpose of the property is to expose the object's parent to QML.

This property is read-only to prevent modifications from QML, where typically the parent is declaratively set. In C++ code, the parent can be explicitly set using QObject::setParent(QObject*), or implicitly set by adding it to a visual container.

The default value of this property is 0.

Since:

BlackBerry 10.0.0

Public Functions

LocaleHandler (

Constructs a LocaleHandler.

Since:

BlackBerry 10.0.0

virtual~LocaleHandler ()

Destructor.

Since:

BlackBerry 10.0.0

void setObjectName (Inherited

Sets the objectName property.

Parameters
name

The new name for the object.

Since:

BlackBerry 10.0.0

virtual Q_INVOKABLE QString toDebugString ()Inherited

Returns a debug string representing this object.

Return:

A debug string for the object.

Since:

BlackBerry 10.0.0

Protected Functions

(Only has inherited protected functions)

BaseObject (Inherited

Constructs an instance of BaseObject's subclass.

Parameters
parent

An optional parent, defaults to 0.

Since:

BlackBerry 10.0.0

Signals

void languageChanged ()

Emitted when the language is changed and a new translator is available.

Applications should use this signal to retranslate strings in the UI.

Since:

BlackBerry 10.0.0

void localeChanged ()

Emitted when the region locale is changed.

Applications should use this signal to handle regional formatting like currency and dates.

Since:

BlackBerry 10.0.0

void localeOrLanguageChanged ()

Emitted when the language or locale is changed.

Since:

BlackBerry 10.0.0

void systemLanguageChanged ()

Emitted when the system language is changed.

Applications should use this signal to load new translators for the application.

Since:

BlackBerry 10.0.0

void creationCompleted ()Inherited

Emitted when this object is instantiated as a result of loading a QML document and creating the root node (only after the root component that caused this instantiation has completed construction), or when the object is being constructed from its builder class.

This signal indicates that the construction and initialization of the object has been completed, the properties are initialized, and any QML binding values have been assigned to the object.

This signal is not emitted when the object is constructed from C++ using the constructor. If the object is constructed using its builder class, the signal is emitted when the the builder class returns the fully constructed object.

This signal can be used when there is an activity that needs to be performed, such as a property state integrity verification after the object is instantiated from a QML document or a builder, but before control is returned to the application.

See also:

QmlDocument

Since:

BlackBerry 10.0.0

void objectNameChanged (Inherited

This signal is emitted when the objectName property is changed.

Since:

BlackBerry 10.0.0

Last modified: 2014-06-24



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus