From a3f9419f8953712747f53e4373a307edd4ed8b06 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 9 Nov 2025 14:12:25 +0000 Subject: [PATCH] add locale class --- include/hyprutils/i18n/I18nEngine.hpp | 18 ++++++++++- src/i18n/I18nEngine.cpp | 26 ++-------------- src/i18n/I18nLocale.cpp | 44 +++++++++++++++++++++++++++ tests/i18n.cpp | 2 +- 4 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 src/i18n/I18nLocale.cpp diff --git a/include/hyprutils/i18n/I18nEngine.hpp b/include/hyprutils/i18n/I18nEngine.hpp index 980a41b..f04b129 100644 --- a/include/hyprutils/i18n/I18nEngine.hpp +++ b/include/hyprutils/i18n/I18nEngine.hpp @@ -13,6 +13,22 @@ namespace Hyprutils::I18n { typedef std::unordered_map translationVarMap; typedef std::function translationFn; + class CI18nLocale { + public: + ~CI18nLocale() = default; + + std::string locale(); + std::string stem(); + std::string full(); + + private: + CI18nLocale(std::string fullLocale); + + std::string m_locale, m_rawFullLocale; + + friend class CI18nEngine; + }; + class CI18nEngine { public: CI18nEngine(); @@ -31,7 +47,7 @@ namespace Hyprutils::I18n { std::string localizeEntry(const std::string& locale, uint64_t key, const translationVarMap& map); - std::string getSystemLocale(); + CI18nLocale getSystemLocale(); private: Memory::CUniquePointer m_impl; diff --git a/src/i18n/I18nEngine.cpp b/src/i18n/I18nEngine.cpp index dde6242..ba82840 100644 --- a/src/i18n/I18nEngine.cpp +++ b/src/i18n/I18nEngine.cpp @@ -105,28 +105,6 @@ std::string CI18nEngine::localizeEntry(const std::string& locale, uint64_t key, return rawStr; } -std::string CI18nEngine::getSystemLocale() { - std::locale locale(""); - auto localeStr = locale.name(); - - // localeStr is very arbitrary... from my testing, it can be: - // en_US.UTF-8 - // LC_CTYPE=en_US - // POSIX - // * - // - // We only return e.g. en_US or pl_PL, or pl - - if (localeStr == "POSIX") - return "en_US"; - if (localeStr == "*") - return "en_US"; - - if (localeStr.contains('=')) - localeStr = localeStr.substr(localeStr.find('=') + 1); - - if (localeStr.contains('.')) - localeStr = localeStr.substr(0, localeStr.find('.')); - - return localeStr; +CI18nLocale CI18nEngine::getSystemLocale() { + return CI18nLocale(std::locale("").name()); } diff --git a/src/i18n/I18nLocale.cpp b/src/i18n/I18nLocale.cpp new file mode 100644 index 0000000..260b386 --- /dev/null +++ b/src/i18n/I18nLocale.cpp @@ -0,0 +1,44 @@ +#include + +using namespace Hyprutils::I18n; + +static std::string extractLocale(std::string locale) { + // localeStr is very arbitrary... from my testing, it can be: + // en_US.UTF-8 + // LC_CTYPE=en_US + // POSIX + // * + // + // We only return e.g. en_US or pl_PL, or pl + + if (locale == "POSIX") + return "en_US"; + if (locale == "*") + return "en_US"; + + if (locale.contains('=')) + locale = locale.substr(locale.find('=') + 1); + + if (locale.contains('.')) + locale = locale.substr(0, locale.find('.')); + + return locale; +} + +CI18nLocale::CI18nLocale(std::string fullLocale) : m_rawFullLocale(std::move(fullLocale)) { + m_locale = extractLocale(m_rawFullLocale); +} + +std::string CI18nLocale::locale() { + return m_locale; +} + +std::string CI18nLocale::stem() { + if (m_locale.contains('_')) + return m_locale.substr(0, m_locale.find('_')); + return m_locale; +} + +std::string CI18nLocale::full() { + return m_rawFullLocale; +} diff --git a/tests/i18n.cpp b/tests/i18n.cpp index af5de0e..0cb8703 100644 --- a/tests/i18n.cpp +++ b/tests/i18n.cpp @@ -15,7 +15,7 @@ int main(int argc, char** argv, char** envp) { CI18nEngine engine; - std::println("System locale: {}", engine.getSystemLocale()); + std::println("System locale: {}, stem: {}", engine.getSystemLocale().locale(), engine.getSystemLocale().stem()); engine.setFallbackLocale("en_US");