RhythmGame 1.4.3
Loading...
Searching...
No Matches
4. Add a translation

Previous | Tutorial | Next: Song selection

Goal: Mark menu text for translation and load a translation file.

Install docs/examples/theme-tutorial/04-translations/ and choose it for Main Menu. It includes Polish translations for the greeting, most buttons and the color setting. You can try them without installing translation tools.

Download 04-translations or open the source folder.

Mark text in QML

The earlier examples use plain English strings. To make them translatable, wrap each string shown to the player in qsTr(). The Song select button now contains:

text: qsTr("Song select")

qsTr() looks up the translation for the selected language. If none is available, it returns the original text.

For the greeting, keep the whole phrase inside qsTr() and put %1 where the profile name belongs. .arg() replaces %1 after translation, so the name itself isn't translated. A translator can move %1 to the right place for their language.

Label {
Layout.fillWidth: true
text: qsTr("Hello, %1").arg(screen.generalVars.name)
color: screen.themeVars.accentColor
font.pixelSize: 32
wrapMode: Text.Wrap
}

Include the translation files

The manifest declares en and pl as available languages. An empty English path uses the source text. The Polish path points to a compiled .qm file.

{
"$schema": "https://raw.githubusercontent.com/Bobini1/RhythmGame/master/schemas/theme.json",
"scripts": {
"main": "main.qml"
},
"settings": {
"main": "main-settings.json"
},
"translations": {
"en": "",
"pl": "translations/tutorial_pl.qm"
}
}

The editable .ts file contains the source strings and translations.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="pl_PL" sourcelanguage="en">
<context>
<name>main</name>
<message>
<source>Hello, %1</source>
<translation>Witaj, %1</translation>
</message>
<message>
<source>Song select</source>
<translation>Wybór utworu</translation>
</message>
<message>
<source>Settings</source>
<translation>Ustawienia</translation>
</message>
<message>
<source>Quit</source>
<translation>Wyjdź</translation>
</message>
</context>
</TS>

Open Settings > General > Language, choose Polish, and return to the menu. Song select, Settings and Quit should read "Wybór utworu", "Ustawienia" and "Wyjdź". Multiplayer has no translation yet, so it keeps its English label. Choose English again to return to the source text. The language preference belongs to the profile, so other screens with Polish translations will change too.

Settings labels use translations stored directly in JSON. They don't pass through lupdate or the QML translation file.

{
"$schema": "https://raw.githubusercontent.com/Bobini1/RhythmGame/master/schemas/themeSettings.json",
"items": [
{
"id": "accentColor",
"name": {
"en": "Accent color",
"pl": "Kolor wyróżnienia"
},
"description": {
"en": "The color of the greeting on the main menu.",
"pl": "Kolor powitania w menu głównym."
},
"type": "color",
"default": "#a8dcc9"
}
]
}

Edit and compile a translation

To change the translated strings, install Qt's Linguist tools. With their directory on your command search path, run the following from inside 04-translations. lupdate collects the strings, Linguist lets you edit the translations, and lrelease builds the file loaded by the game.

lupdate main.qml -ts translations/tutorial_pl.ts
linguist translations/tutorial_pl.ts
lrelease translations/tutorial_pl.ts -qm translations/tutorial_pl.qm

Keep both files in your source folder and include the .qm file when sharing the theme. Restart the game after replacing it. Add any new QML files to the lupdate command so their strings are collected too.

The translation guide explains language tags such as fr-CA and zh-Hant-TW. Qt's source translation guide explains contexts and placeholders in more detail.