Skip to content

Commit

Permalink
Support translation
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Dec 8, 2021
1 parent 6ee88e2 commit e3ada1e
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 8 deletions.
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ foreach (QT_COMPONENT ${APD_QT_COMPONENTS})
set(APD_QT_LIBRARIES ${APD_QT_LIBRARIES} Qt5::${QT_COMPONENT})
endforeach()
find_package(Qt5 COMPONENTS ${APD_QT_COMPONENTS} REQUIRED)
find_package(Qt5LinguistTools REQUIRED)

# spdlog
#
Expand Down Expand Up @@ -307,12 +308,39 @@ if (APD_BUILD_GIT_HASH)
set(APD_COMPILE_DEFINITIONS ${APD_COMPILE_DEFINITIONS} APD_BUILD_GIT_HASH="${APD_BUILD_GIT_HASH}")
endif()

##################################################
# Qt configurations
#

# Resource

qt5_add_resources(APD_CODE_FILES "Source/Resource/Resource.qrc")

#
# Translation
#

set(APD_TRANSLATION_LANGS zh_CN)

set(APD_TS_FILES ${APD_TRANSLATION_LANGS})
list(TRANSFORM APD_TS_FILES PREPEND "./Source/Resource/Translation/apd_")
list(TRANSFORM APD_TS_FILES APPEND ".ts")
set_source_files_properties(${APD_TS_FILES} PROPERTIES OUTPUT_LOCATION "${APD_BINARY_OUT_DIR}/translations")

qt5_create_translation(
APD_QM_FILES
"./Source/"
${APD_TS_FILES}
OPTIONS -locations none
)

##################################################

add_executable(
${PROJECT_NAME} ${ADD_EXECUTABLE_ARG}

${APD_CODE_FILES}
${APD_QM_FILES}
)

target_compile_definitions(
Expand Down
12 changes: 12 additions & 0 deletions Source/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ bool ApdApplication::Prepare(int argc, char *argv[])

LOG(Info, "Args: {{ trace: {} }}", _launchOptions.enableTrace);

SetTranslator();

setFont(QFont{"Segoe UI", 9});
setWindowIcon(QIcon{Config::QrcIconSvg});
setQuitOnLastWindowClosed(false);
Expand All @@ -163,6 +165,16 @@ int ApdApplication::Run()
return exec();
}

void ApdApplication::SetTranslator(const QLocale &locale)
{
LOG(Info, "SetTranslator() locale: {}", locale.name());

QDir translationFolder = QCoreApplication::applicationDirPath();
translationFolder.cd("translations");
_translator.load(locale, "apd", "_", translationFolder.absolutePath());
installTranslator(&_translator);
}

void ApdApplication::QuitSafety()
{
QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);
Expand Down
7 changes: 7 additions & 0 deletions Source/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <cxxopts.hpp>
#include <SingleApplication>

#include <QTranslator>

#include "Gui/TrayIcon.h"
#include "Gui/MainWindow.h"
#include "Gui/DownloadWindow.h"
Expand All @@ -30,6 +32,8 @@

class ApdApplication : public SingleApplication
{
Q_OBJECT

private:
struct LaunchOptions {
bool enableTrace{false};
Expand Down Expand Up @@ -60,11 +64,14 @@ class ApdApplication : public SingleApplication
return _isFirstTimeUse;
}

void SetTranslator(const QLocale &locale = {});

static void QuitSafety();

private:
static inline LaunchOptions _launchOptions;
static inline bool _isFirstTimeUse{false};
QTranslator _translator;
std::unique_ptr<Gui::TrayIcon> _trayIcon;
std::unique_ptr<Gui::MainWindow> _mainWindow;
std::unique_ptr<Gui::DownloadWindow> _downloadWindow;
Expand Down
4 changes: 2 additions & 2 deletions Source/Gui/SettingsWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@
</font>
</property>
<property name="text">
<string>AirPodsDesktop</string>
<string notr="true">AirPodsDesktop</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lbVersion">
<property name="text">
<string>v</string>
<string notr="true">v</string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
Expand Down
21 changes: 15 additions & 6 deletions Source/Gui/TrayIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,31 @@ void TrayIcon::Repaint()

toolTipContent += state.displayName;

static auto strLeft{tr("Left")}, strRight{tr("Right")}, strCase{tr("Case")},
strCharging{tr("charging")};

static auto textCharging = QString{" (%1)"}.arg(strCharging),
textPlaceHolder = QString{"\n%1: %2%%3"};

// clang-format off
if (state.pods.left.battery.Available()) {
const auto batteryValue = state.pods.left.battery.Value();

toolTipContent += QString{tr("\nLeft: %1%%2")}
toolTipContent += textPlaceHolder
.arg(strLeft)
.arg(batteryValue)
.arg(state.pods.left.isCharging ? tr(" (charging)") : "");
.arg(state.pods.left.isCharging ? textCharging : QString{});

minBattery = batteryValue;
}

if (state.pods.right.battery.Available()) {
const auto batteryValue = state.pods.right.battery.Value();

toolTipContent += QString{tr("\nRight: %1%%2")}
toolTipContent += textPlaceHolder
.arg(strRight)
.arg(batteryValue)
.arg(state.pods.right.isCharging ? tr(" (charging)") : "");
.arg(state.pods.right.isCharging ? textCharging : QString{});

if (minBattery.Available() && batteryValue < minBattery.Value() ||
!minBattery.Available()) {
Expand All @@ -145,9 +153,10 @@ void TrayIcon::Repaint()
}

if (state.caseBox.battery.Available()) {
toolTipContent += QString{tr("\nCase: %1%%2")}
toolTipContent += textPlaceHolder
.arg(strCase)
.arg(state.caseBox.battery.Value())
.arg(state.caseBox.isCharging ? tr(" (charging)") : "");
.arg(state.caseBox.isCharging ? textCharging : QString{});
}
// clang-format on
break;
Expand Down
Loading

0 comments on commit e3ada1e

Please sign in to comment.