From eda4c287e920bafd7fd38999b61683b4b72159b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc=20Serf=C5=91z=C5=91?= Date: Fri, 14 Jun 2024 15:48:48 +0200 Subject: [PATCH] chore(i18n): Localization skeleton --- CMakeLists.txt | 51 ++++++++++++++++++++++++++++++++++++++++++++-- po/midiconn.pot | 19 +++++++++++++++++ src/Version.hpp.in | 1 + src/main.cpp | 17 ++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 po/midiconn.pot diff --git a/CMakeLists.txt b/CMakeLists.txt index 79b3ce6..55b4d7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) # option() honors normal variables include(FetchContent) include(external/CMakeRC.cmake) +include(GNUInstallDirs) project(midiconn VERSION 0.2.2 LANGUAGES CXX) @@ -20,7 +21,9 @@ endif() if(NOT WIN32 AND MC_CHECK_FOR_UPDATES) find_package(CURL REQUIRED) endif() +find_package(Intl REQUIRED) +set(MC_LOCALE_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LOCALEDIR}") configure_file( src/Version.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/src/Version.hpp @@ -92,7 +95,8 @@ target_include_directories(midiconn PRIVATE external/imnodes external/portable-file-dialogs external/IconFontCppHeaders - "${FREETYPE_INCLUDE_DIRS}") + "${FREETYPE_INCLUDE_DIRS}" + "${Intl_INCLUDE_DIRS}") target_link_libraries(midiconn PRIVATE nlohmann_json::nlohmann_json rtmidi @@ -102,7 +106,8 @@ target_link_libraries(midiconn PRIVATE fmt::fmt freetype resources - midi) + midi + "${Intl_LIBRARIES}") if(NOT WIN32 AND MC_CHECK_FOR_UPDATES) target_link_libraries(midiconn PRIVATE curl) endif() @@ -157,3 +162,45 @@ endif() install(TARGETS midiconn RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") include(CPack) + +find_program(XGETTEXT xgettext) +find_package(Gettext) +if (XGETTEXT AND ${GETTEXT_FOUND}) + set(POT_FILE "${PROJECT_SOURCE_DIR}/po/midiconn.pot") + add_custom_command( + OUTPUT "${POT_FILE}" + COMMAND "${XGETTEXT}" "--join-existing" "--add-comments" "--output=${POT_FILE}" "--output-dir=${PROJECT_SOURCE_DIR}/po" ${sources} + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + DEPENDS ${sources} + ) + + file(GLOB PO_FILES "${PROJECT_SOURCE_DIR}/po/*.po") + if(PO_FILES) + foreach(PO_FILE ${PO_FILES}) + add_custom_command( + OUTPUT "${PO_FILE}" + COMMAND "${GETTEXT_MSGMERGE_EXECUTABLE}" "--update" "--backup=off" "${PO_FILE}" "${POT_FILE}" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + DEPENDS "${POT_FILE}" + ) + endforeach() + + add_custom_target(update-po DEPENDS ${PO_FILES}) + + foreach(PO_FILE ${PO_FILES}) + cmake_path(GET PO_FILE STEM LANG) + set(MO_FILE "${CMAKE_BINARY_DIR}/${LANG}/midiconn.mo") + add_custom_command( + OUTPUT "${MO_FILE}" + COMMAND "${GETTEXT_MSGFMT_EXECUTABLE}" "--output-file=${MO_FILE}" "${PO_FILE}" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + DEPENDS "${PO_FILE}" + ) + add_custom_target(generate-mo-${LANG} ALL DEPENDS ${MO_FILE}) + install( + FILES "${MO_FILE}" + DESTINATION "${MC_LOCALE_DIR}/${LANG}/LC_MESSAGES" + ) + endforeach() + endif() +endif() diff --git a/po/midiconn.pot b/po/midiconn.pot new file mode 100644 index 0000000..a779048 --- /dev/null +++ b/po/midiconn.pot @@ -0,0 +1,19 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-06-14 15:46+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + diff --git a/src/Version.hpp.in b/src/Version.hpp.in index 9f0ebac..8053a71 100644 --- a/src/Version.hpp.in +++ b/src/Version.hpp.in @@ -16,6 +16,7 @@ #define MC_BUILD_OS "@CMAKE_SYSTEM_NAME@-@CMAKE_SYSTEM_PROCESSOR@" #define MC_WEBSITE_URL "https://mfeproject.itch.io/midiconn" #cmakedefine MC_CHECK_FOR_UPDATES +#define MC_LOCALE_DIR "@MC_LOCALE_DIR@" namespace mc { diff --git a/src/main.cpp b/src/main.cpp index 0cab909..dbaaf00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include "KeyboardShortcutAggregator.hpp" #include "PlatformUtils.hpp" #include "Utils.hpp" +#include "Version.hpp" #if !SDL_VERSION_ATLEAST(2, 0, 17) #error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function @@ -38,6 +39,22 @@ MC_MAIN spdlog::set_default_logger(rotating_logger); spdlog::flush_every(3s); + if (setlocale(LC_ALL, "") == nullptr) + { + spdlog::error("Cannot set locale"); + return -1; + } + if (bindtextdomain(MIDI_APPLICATION_NAME_SNAKE, MC_LOCALE_DIR) == nullptr) + { + spdlog::error("Cannot bind text domain"); + return -1; + } + if (textdomain(MIDI_APPLICATION_NAME_SNAKE) == nullptr) + { + spdlog::error("Cannot set text domain"); + return -1; + } + mc::platform::set_process_dpi_aware(); const auto file_to_open = mc::wrap_exception([&]() {