Skip to content

Instantly share code, notes, and snippets.

@define-private-public
Last active October 10, 2022 18:30
Show Gist options
  • Save define-private-public/f01ff2f9bbb74eded144558a2e02966c to your computer and use it in GitHub Desktop.
Save define-private-public/f01ff2f9bbb74eded144558a2e02966c to your computer and use it in GitHub Desktop.
# `Linguist` is required
find_package(Qt6 COMPONENTS Linguist REQUIRED)
# ...
# Have your app specified
qt_add_executable(myapp ...
# ...
# Specify your translations
qt_add_translations(myapp
TS_FILES
i18n/myapp_de_DE.ts
i18n/myapp_ja_JP.ts
)
# Specify your translations
qt_add_translations(myapp
TS_FILES
i18n/myapp_de_DE.ts
i18n/myapp_ja_JP.ts
)
add_dependencies(myapp release_translations)
// User facing strings
QtObject {
// Render View
readonly property string app_window_title: qsTr('PSRayTracing (GUI Edition)')
readonly property string scene_label: qsTr('Scene:')
readonly property string stop: qsTr('Stop')
readonly property string render: qsTr('Render')
readonly property string total_render_time_fmt: qsTr('Total Render Time: %1')
// ...
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE">
<context>
<name>QObject</name>
<message>
<source>No</source>
<comment>denial</comment>
<translation type="obsolete">Nein</translation>
</message>
<message numerus="yes">
<location filename="main.cpp" line="36"/>
<source>%Ln Apple(s)</source>
<translation type="unfinished">
<numerusform>%Ln Apfel</numerusform>
<numerusform>%Ln Äpfel</numerusform>
</translation>
</message>
</context>
</TS>
// Find this line
const QString baseName = "myapp_" + QLocale(locale).name();
// Change it to this
const QString baseName = "myapp_" + QLocale().name();
QLocale::setDefault(QLocale::German);
auto lbl = new QLabel("Hi Mom!");
QString filePath = "data.csv";
auto lbl = new QLabel(tr("Hi Mom!"));
QString filePath = QStringLiteral("data.csv");
qDebug() << "Doing this with qDebug() is still okay though!"
[Controls]
Style=Basic
[Basic]
Font\PointSize=15
<RCC>
<qresource prefix="/">
<file>qtquickcontrols2.conf</file>
...
<!-- Japanese specific resources -->
<qresource prefix="/" lang="ja">
<file alias="qtquickcontrols2.conf">i18n/qtquickcontrols2_ja_JP.conf</file>
</qresource>
</RCC>
id: fields_layout
readonly property bool _small_width: UITheme.is_width_small(parent.width)
readonly property int column_count: (_small_width ? 1 : 2)
// ...
columns: fields_layout.column_count
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "myapp_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
app.installTranslator(&translator);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment