Skip to content

Commit d06c961

Browse files
vaxerskifufexan
andauthored
i18n: init translation engine (#39)
--------- Co-authored-by: Mihai Fufezan <mihai@fufexan.net>
1 parent 75bd0c0 commit d06c961

7 files changed

Lines changed: 73 additions & 20 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pkg_check_modules(
2626
hyprtoolkit
2727
pixman-1
2828
libdrm
29-
hyprutils
29+
hyprutils>=0.10.2
3030
hyprwire
3131
icu-uc
3232
hyprlang

flake.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/ConfigManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ CConfigManager::CConfigManager() : m_inotifyFd(inotify_init()) {
1212

1313
m_config->addConfigValue("general:grab_focus", Hyprlang::INT{1});
1414

15+
m_config->addConfigValue("locale:override", Hyprlang::STRING{""});
16+
1517
m_config->addConfigValue("cache:enabled", Hyprlang::INT{1});
1618

1719
m_config->addConfigValue("finders:default_finder", Hyprlang::STRING{"desktop"});

src/i18n/Engine.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "Engine.hpp"
2+
#include "../config/ConfigManager.hpp"
3+
4+
static Hyprutils::I18n::CI18nEngine engine;
5+
6+
//
7+
void I18n::initEngine() {
8+
engine.setFallbackLocale("en_US");
9+
10+
// en_US (English)
11+
engine.registerEntry("en_US", TXT_KEY_SEARCH_SOMETHING, "Search...");
12+
13+
// pl_PL (Polish)
14+
engine.registerEntry("pl_PL", TXT_KEY_SEARCH_SOMETHING, "Szukaj...");
15+
16+
// ja_JP (Japanese)
17+
engine.registerEntry("ja_JP", TXT_KEY_SEARCH_SOMETHING, "検索...");
18+
19+
// zh (Simplified Chinese)
20+
engine.registerEntry("zh", TXT_KEY_SEARCH_SOMETHING, "搜索...");
21+
22+
// zh_Hant (Traditional Chinese)
23+
engine.registerEntry("zh_Hant", TXT_KEY_SEARCH_SOMETHING, "搜尋...");
24+
}
25+
26+
std::string I18n::localize(eTextKeys key, const Hyprutils::I18n::translationVarMap& vars) {
27+
static auto POVERRIDELOCALE = Hyprlang::CSimpleConfigValue<Hyprlang::STRING>(g_configManager->m_config.get(), "locale:override");
28+
29+
const auto LOCALE = std::string_view{*POVERRIDELOCALE}.empty() ? engine.getSystemLocale().locale() : std::string{*POVERRIDELOCALE};
30+
31+
return engine.localizeEntry(LOCALE, key, vars);
32+
}

src/i18n/Engine.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <string>
5+
#include <hyprutils/i18n/I18nEngine.hpp>
6+
7+
namespace I18n {
8+
//NOLINTNEXTLINE
9+
enum eTextKeys : uint64_t {
10+
TXT_KEY_SEARCH_SOMETHING = 0,
11+
};
12+
13+
void initEngine();
14+
std::string localize(eTextKeys key, const Hyprutils::I18n::translationVarMap& vars);
15+
};

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "socket/ServerSocket.hpp"
99
#include "query/QueryProcessor.hpp"
1010
#include "config/ConfigManager.hpp"
11+
#include "i18n/Engine.hpp"
1112

1213
#include <iostream>
1314

@@ -112,6 +113,8 @@ int main(int argc, char** argv, char** envp) {
112113

113114
socket.reset();
114115

116+
I18n::initEngine();
117+
115118
if (!explicitOptions.empty()) {
116119
g_ipcFinder->setData(explicitOptions);
117120
g_queryProcessor->overrideQueryProvider(g_ipcFinder);

src/ui/UI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../socket/ServerSocket.hpp"
77
#include "../helpers/Log.hpp"
88
#include "../config/ConfigManager.hpp"
9+
#include "../i18n/Engine.hpp"
910

1011
#include <hyprutils/string/String.hpp>
1112
#include <xkbcommon/xkbcommon-keysyms.h>
@@ -33,7 +34,7 @@ CUI::CUI(bool open) : m_openByDefault(open) {
3334
m_layout->setMargin(4);
3435

3536
m_inputBox = Hyprtoolkit::CTextboxBuilder::begin()
36-
->placeholder("Search something...")
37+
->placeholder(I18n::localize(I18n::TXT_KEY_SEARCH_SOMETHING, {}))
3738
->onTextEdited([](SP<Hyprtoolkit::CTextboxElement>, const std::string& query) { g_queryProcessor->scheduleQueryUpdate(query); })
3839
->size({Hyprtoolkit::CDynamicSize::HT_SIZE_PERCENT, Hyprtoolkit::CDynamicSize::HT_SIZE_ABSOLUTE, {1.F, 28.F}})
3940
->multiline(false)

0 commit comments

Comments
 (0)