Skip to content

Commit

Permalink
feature: translation du chines + store preference linke lang, keybind…
Browse files Browse the repository at this point in the history
… in a .ini file
  • Loading branch information
us3rT0m committed Jun 19, 2023
1 parent ad18f51 commit d571e52
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 35 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)

set(TS_FILES OnlyUP_Trainer_fr_FR.ts)

set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
positionmanager.h
positionmanager.cpp
config.h
config.cpp
resources.qrc
${TS_FILES}
)
Expand All @@ -35,7 +35,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation

qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
qt_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} ${TS_FILES})
else()
if(ANDROID)
add_library(OnlyUP_Trainer SHARED
Expand Down
12 changes: 9 additions & 3 deletions CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 10.0.1, 2023-06-17T16:48:54. -->
<!-- Written by QtCreator 10.0.1, 2023-06-20T01:17:37. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -81,6 +81,13 @@
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ClangdSettings">
<value type="bool" key="blockIndexing">false</value>
<value type="bool" key="useGlobalSettings">true</value>
</valuemap>
<valuemap type="QVariantMap" key="CppEditor.QuickFix">
<value type="bool" key="UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
Expand All @@ -90,7 +97,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 6.5.1 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 6.5.1 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt6.651.win64_mingw_kit</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">4</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">1</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
Expand Down Expand Up @@ -438,7 +445,6 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseTerminal">false</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/dev/software/c++/Qt/OnlyUP_Trainer/build-OnlyUP_Trainer-Desktop_Qt_6_5_1_MinGW_64_bit-Release</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
Empty file added OnlyUP_Trainer.pro
Empty file.
Binary file added OnlyUP_Trainer_en.qm
Binary file not shown.
92 changes: 92 additions & 0 deletions OnlyUP_Trainer_en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
<name>MainWindow</name>
<message>
<location filename="mainwindow.ui" line="32"/>
<location filename="mainwindow.ui" line="119"/>
<source>Only Up! Trainer v2.4</source>
<oldsource>Only Up! Trainer v2.3</oldsource>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="53"/>
<source>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If you enjoy my work, you can buy me a coffee ♥ : &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.buymeacoffee.com/t0mz3r&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#e5ac76;&quot;&gt;https://www.buymeacoffee.com/t0mz3r&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="94"/>
<source>Initialize Trainer</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="153"/>
<source>Teleport to Position</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="262"/>
<source>Save Position</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="290"/>
<source>Enter position name...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="310"/>
<source>Position :</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="452"/>
<source>Update Position</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="482"/>
<source>Key save</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="544"/>
<source>Key TP</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="565"/>
<source>Search a save</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="610"/>
<source>Drake Speed (Reset)</source>
<oldsource>Speed Drake (Reset)</oldsource>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="728"/>
<source>Pause drake</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="758"/>
<source>Reset drake</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="330"/>
<location filename="mainwindow.cpp" line="352"/>
<source>Choisir une touche</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.cpp" line="331"/>
<location filename="mainwindow.cpp" line="353"/>
<source>Touche :</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
3 changes: 0 additions & 3 deletions OnlyUP_Trainer_fr_FR.ts

This file was deleted.

Binary file added OnlyUP_Trainer_zh.qm
Binary file not shown.
95 changes: 95 additions & 0 deletions OnlyUP_Trainer_zh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>MainWindow</name>
<message>
<location filename="mainwindow.ui" line="32"/>
<location filename="mainwindow.ui" line="119"/>
<source>Only Up! Trainer v2.4</source>
<oldsource>Only Up! Trainer v2.3</oldsource>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="53"/>
<source>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If you enjoy my work, you can buy me a coffee ♥ : &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.buymeacoffee.com/t0mz3r&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#e5ac76;&quot;&gt;https://www.buymeacoffee.com/t0mz3r&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</source>
<translation type="unfinished">&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;如果您认可我的存档器,&lt;br/&gt;希望您可以给我一杯咖啡钱以示鼓励,万分感谢 ♥ : &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.buymeacoffee.com/t0mz3r&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#e5ac76;&quot;&gt;https://www.buymeacoffee.com/t0mz3r&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</translation>
</message>
<message>
<location filename="mainwindow.ui" line="94"/>
<source>Initialize Trainer</source>
<translation>初始化存档器</translation>
</message>
<message>
<location filename="mainwindow.ui" line="153"/>
<source>Teleport to Position</source>
<translation>传送至存档点</translation>
</message>
<message>
<location filename="mainwindow.ui" line="262"/>
<source>Save Position</source>
<translation>保存存档点</translation>
</message>
<message>
<location filename="mainwindow.ui" line="290"/>
<source>Enter position name...</source>
<translation>输入存档点名称…</translation>
</message>
<message>
<location filename="mainwindow.ui" line="310"/>
<source>Position :</source>
<translation>坐标 :</translation>
</message>
<message>
<location filename="mainwindow.ui" line="452"/>
<source>Update Position</source>
<translation>更新位置</translation>
</message>
<message>
<location filename="mainwindow.ui" line="482"/>
<source>Key save</source>
<translation>热键保存</translation>
</message>
<message>
<location filename="mainwindow.ui" line="544"/>
<source>Key TP</source>
<translation>热键传送</translation>
</message>
<message>
<location filename="mainwindow.ui" line="565"/>
<source>Search a save</source>
<translation>搜索存档点</translation>
</message>
<message>
<location filename="mainwindow.ui" line="610"/>
<source>Drake Speed (Reset)</source>
<translation>龙速(重置)</translation>
</message>
<message>
<source>Speed Drake (Reset)</source>
<translation type="vanished">龙速(重置)</translation>
</message>
<message>
<location filename="mainwindow.ui" line="728"/>
<source>Pause drake</source>
<translation>暂停</translation>
</message>
<message>
<location filename="mainwindow.ui" line="758"/>
<source>Reset drake</source>
<translation>初始化龙</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="330"/>
<location filename="mainwindow.cpp" line="352"/>
<source>Choisir une touche</source>
<translation>设置热键</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="331"/>
<location filename="mainwindow.cpp" line="353"/>
<source>Touche :</source>
<translation>热键 :</translation>
</message>
</context>
</TS>
40 changes: 40 additions & 0 deletions config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "config.h"
#include <QFile>
#include <QTextStream>

Config::Config(const QString &filename)
{
QFile file(filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
int pos = line.indexOf('=');
if (pos == -1) continue;
QString key = line.left(pos).trimmed();
QString value = line.mid(pos + 1).trimmed();
data[key] = value;
}
}
}

QString Config::get(const QString &key) const
{
return data.value(key, QString());
}

void Config::set(const QString &key, const QString &value)
{
data[key] = value;
}

void Config::save(const QString &filename) const
{
QFile file(filename);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
for (auto i = data.begin(); i != data.end(); ++i) {
out << i.key() << "=" << i.value() << "\n";
}
}
}
20 changes: 20 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CONFIG_H
#define CONFIG_H

#include <QString>
#include <QMap>

class Config
{
public:
Config(const QString &filename);

QString get(const QString &key) const;
void set(const QString &key, const QString &value);
void save(const QString &filename) const;

private:
QMap<QString, QString> data;
};

#endif // CONFIG_H
1 change: 1 addition & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lang = zh
29 changes: 21 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mainwindow.h"
#include "config.h"

#include <QApplication>
#include <QLocale>
Expand All @@ -7,6 +8,9 @@
#include <QMessageBox>
#include <QDebug>

QTranslator translator;
Config config("config.ini");

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Expand All @@ -16,15 +20,24 @@ int main(int argc, char *argv[])
qSetMessagePattern("%{time yyyy-MM-dd hh:mm:ss.zzz} %{type} %{threadid} %{function}(): %{message}");
#endif

QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "OnlyUP_Trainer_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
QString lang = config.get("lang");

QLocale locale;
if (lang == "en") {
locale = QLocale(QLocale::English, QLocale::UnitedKingdom);
} else if (lang == "fr") {
locale = QLocale(QLocale::French, QLocale::France);
} else if (lang == "zh") {
locale = QLocale(QLocale::Chinese, QLocale::China);
}

const QString baseName = "OnlyUP_Trainer_" + locale.name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
} else {
qDebug() << "Failed to load translation:" << baseName;
}

MainWindow w;
w.show();
return a.exec();
Expand Down

0 comments on commit d571e52

Please sign in to comment.