Skip to content

Commit

Permalink
feat(locale): support english and chinese (#62)
Browse files Browse the repository at this point in the history
* feat: support en locale

* build: generate .mo file

* feat: change locale file location

* fix: package locale files location

* build: install msgfmt by vcpkg

* chore: fix typo

* chore: add .pot into gitignore
  • Loading branch information
Serein207 committed Sep 26, 2024
1 parent 25ba0df commit 8195691
Show file tree
Hide file tree
Showing 19 changed files with 825 additions and 111 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ jobs:
Import-Module (Join-Path $vsPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll")
Enter-VsDevShell -VsInstallPath $vsPath -SkipAutomaticLocation -DevCmdArguments '-arch=${{ matrix.msvc_arch }} -host_arch=x64'
$additionalConfigureArgs += '-DPKG_CONFIG_EXECUTABLE:FILEPATH=./build/vcpkg_installed/x64-windows/tools/pkgconf/pkgconf.exe'
$additionalConfigureArgs += '-DMSGFMT_EXECUTABLE:FILEPATH=./build/vcpkg_installed/x64-windows/tools/gettext/bin/msgfmt.exe'
}
if ($vcpkgTargetTriplet) {
$additionalConfigureArgs += "-DVCPKG_TARGET_TRIPLET:STRING=$vcpkgTargetTriplet"
Expand Down
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ CMakeUserPresets.json
# CMake build directory
/build
/out
/.vscode/settings.json

action-cli
# IDE settings
.vscode/settings.json
.idea
.vs

# Windows build output
action-cli
*.rc

# i18n files
*.mo
*.pot
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ endif()
project(sast-evento VERSION ${VERSION_SEMANTIC} LANGUAGES CXX)

set(SLINT_STYLE "material" CACHE STRING "Slint style" FORCE)
set(SLINT_FEATURE_GETTEXT ON CACHE BOOL "Slint gettext feature" FORCE)

if (WIN32)
# Target Windows 7
Expand Down Expand Up @@ -53,6 +54,7 @@ find_package(spdlog REQUIRED)
find_package(Boost REQUIRED COMPONENTS system url beast process)
find_package(OpenSSL 3.3.0 REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(Intl REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus)

Expand All @@ -62,6 +64,9 @@ if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/3rdpart/sast-link-cxx-sdk/CMakeLists
message(FATAL_ERROR "Git submodule not found. Run `git submodule update --init` from the source tree to fetch the submodule contents.")
endif()

# i18n
set(SOURCE_LOCALE_DIR "${CMAKE_SOURCE_DIR}/ui/locale")

# source code
add_subdirectory(src)
# sast-link-sdk
Expand All @@ -76,6 +81,8 @@ set(BUILD_SHARED_LIBS ON)
file(WRITE ${CMAKE_BINARY_DIR}/sast-evento-version.txt ${VERSION_SEMANTIC})
install(FILES ${CMAKE_BINARY_DIR}/sast-evento-version.txt DESTINATION .)
install(FILES ${CMAKE_SOURCE_DIR}/LICENSE DESTINATION .)
install(FILES ${SOURCE_LOCALE_DIR}/en/LC_MESSAGES/sast-evento.mo DESTINATION ./locale/en/LC_MESSAGES)
install(FILES ${SOURCE_LOCALE_DIR}/zh/LC_MESSAGES/sast-evento.mo DESTINATION ./locale/zh/LC_MESSAGES)
install(TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down
11 changes: 11 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ target_compile_definitions(${PROJECT_NAME}
$<$<CONFIG:Debug>:EVENTO_DEBUG>
$<$<CONFIG:Release>:EVENTO_RELEASE>
${PLATFORM}
LOCALE_DIR="${CMAKE_SOURCE_DIR}/ui/locale"
)

target_link_libraries(${PROJECT_NAME}
Expand All @@ -67,6 +68,7 @@ target_link_libraries(${PROJECT_NAME}
keychain
Slint::Slint
version::version
Intl::Intl
${URING_LIBRARIES}
)

Expand All @@ -75,4 +77,13 @@ if (WIN32)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:${PROJECT_NAME}> $<TARGET_FILE_DIR:${PROJECT_NAME}> COMMAND_EXPAND_LISTS)
endif()

find_program(MSGFMT_EXECUTABLE msgfmt REQUIRED)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${SOURCE_LOCALE_DIR}/en/LC_MESSAGES
COMMAND ${MSGFMT_EXECUTABLE} ${SOURCE_LOCALE_DIR}/en.po -o ${SOURCE_LOCALE_DIR}/en/LC_MESSAGES/sast-evento.mo
COMMAND ${CMAKE_COMMAND} -E make_directory ${SOURCE_LOCALE_DIR}/zh/LC_MESSAGES
COMMAND ${MSGFMT_EXECUTABLE} ${SOURCE_LOCALE_DIR}/zh.po -o ${SOURCE_LOCALE_DIR}/zh/LC_MESSAGES/sast-evento.mo
)

add_subdirectory(Tray)
12 changes: 1 addition & 11 deletions src/Controller/View/SettingPage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ SettingPage::SettingPage(slint::ComponentHandle<UiEntryName> uiEntry, UiBridge&
void SettingPage::onCreate() {
auto& self = *this;

const auto [languageIdx, minimalToTray, noticeBegin, noticeEnd, themeIdx] = evento::settings;

self->set_language_index(languageIdx);
const auto [minimalToTray, noticeBegin, noticeEnd, themeIdx] = evento::settings;
self->set_minimal_to_tray(minimalToTray);
self->set_notice_begin(noticeBegin);
self->set_notice_end(noticeEnd);
Expand All @@ -23,19 +21,12 @@ void SettingPage::onCreate() {

config.insert_or_assign("setting",
toml::table{
{"language", languageIdx},
{"minimal-to-tray", minimalToTray},
{"notice-begin", noticeBegin},
{"notice-end", noticeEnd},
{"theme", themeIdx},
});

self->on_language_changed([&self = *this]() {
auto& setting = config["setting"].ref<toml::table>();
setting.insert_or_assign("language", self->get_language_index());
evento::settings.language = self->get_language_index();
});

self->on_minimal_to_tray_changed([&self = *this]() {
auto& setting = config["setting"].ref<toml::table>();
setting.insert_or_assign("minimal-to-tray", self->get_minimal_to_tray());
Expand Down Expand Up @@ -69,7 +60,6 @@ void SettingPage::onCreate() {
void SettingPage::onShow() {
auto& self = *this;

self->set_language_index(evento::settings.language);
self->set_minimal_to_tray(evento::settings.minimalToTray);
self->set_notice_begin(evento::settings.noticeBegin);
self->set_notice_end(evento::settings.noticeEnd);
Expand Down
19 changes: 10 additions & 9 deletions src/Infrastructure/Utils/Config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <boost/dll.hpp>
#include <chrono>
#include <ctime>
#include <filesystem>
Expand All @@ -14,7 +15,6 @@ user-id = <string>
expire = <date-time>
[setting]
language = <int>
minimal-to-tray = <bool>
notice-begin = <bool>
notice-end = <bool>
Expand All @@ -36,15 +36,21 @@ const std::filesystem::path configDir =

inline toml::table config;

inline struct {
int language;
const std::filesystem::path localePath =
#ifdef EVENTO_DEBUG
LOCALE_DIR;
#else
std::filesystem::path{(boost::dll::program_location().parent_path() / "locale").string()};
#endif // EVENTO_DEBUG

inline struct Settings {
bool minimalToTray;
bool noticeBegin;
bool noticeEnd;
int theme;
} settings;

inline struct {
inline struct Account {
std::string userId;
toml::date_time expire;
} account;
Expand All @@ -55,10 +61,6 @@ static void loadSetting() {
}
auto& setting = config["setting"].ref<toml::table>();

auto languageIdx = setting["language"].value_or(0);
if (languageIdx > 2) {
languageIdx = 0;
}
auto themeIdx = setting["theme"].value_or(0);
if (themeIdx > 2) {
themeIdx = 0;
Expand All @@ -68,7 +70,6 @@ static void loadSetting() {
auto minimalToTray = setting["minimal-to-tray"].value_or(false);

evento::settings = {
.language = languageIdx,
.minimalToTray = minimalToTray,
.noticeBegin = noticeBegin,
.noticeEnd = noticeEnd,
Expand Down
5 changes: 5 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <Infrastructure/Utils/Logger.hh>
#include <Version.h>
#include <filesystem>
#include <libintl.h>
#include <locale>
#include <spdlog/spdlog.h>

int main(int argc, char** argv) {
Expand All @@ -13,6 +15,9 @@ int main(int argc, char** argv) {
evento::initConfig();
spdlog::info("SAST Evento version: v" VERSION_FULL);

bindtextdomain("sast-evento", evento::localePath.string().c_str());
std::locale::global(std::locale(""));

evento::UiBridge uiBridge(App::create());

evento::SocketClient socketClient({
Expand Down
6 changes: 3 additions & 3 deletions ui/app.slint
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,17 @@ export component App inherits Window {
nav-views: [
{
icon:Token.image.icon.home,
content:"活动广场",
content: @tr("活动广场"),
target:ViewName.DiscoveryPage,
},
{
icon:Token.image.icon.department,
content:"部门活动",
content: @tr("部门活动"),
target:ViewName.SearchPage,
},
{
icon:Token.image.icon.schedule,
content:"活动日程",
content: @tr("活动日程"),
target:ViewName.MyEventPage,
},
];
Expand Down
Loading

0 comments on commit 8195691

Please sign in to comment.