From f4e7e33fba5fb6eaec76b233b6f0b9260196e506 Mon Sep 17 00:00:00 2001 From: reionwong Date: Mon, 19 Oct 2026 23:45:35 +0800 Subject: [PATCH] feat(i18n): show the new language when the session language changes --- src/window/settingswindow.cpp | 11 +++++++++++ src/window/settingswindow.h | 2 ++ src/window/sidebar.cpp | 8 ++++++++ src/window/sidebar.h | 2 ++ tests/ui/foundation_test.cpp | 14 ++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/src/window/settingswindow.cpp b/src/window/settingswindow.cpp index 0ca6cc5..fae55ac 100644 --- a/src/window/settingswindow.cpp +++ b/src/window/settingswindow.cpp @@ -242,6 +242,7 @@ SettingsWindow::SettingsWindow() }; // A press anywhere outside the search field takes the focus off it. m_window->onPointerPressed = [this](PointF position, MouseButton) { m_sidebar->releaseSearch(position); }; + m_languageChanged = Application::instance()->languageChanged.connect([this] { retranslate(); }); for (const PageInfo& page : pages()) { const bool sidebarPage = !page.category.empty(); @@ -371,6 +372,16 @@ void SettingsWindow::showHistoryEntry(int index) updateHeader(); } +void SettingsWindow::retranslate() +{ + m_window->setTitle(tr("Settings")); + m_sidebar->retranslate(); + if (m_historyIndex >= 0) + showHistoryEntry(m_historyIndex); + else + updateHeader(); +} + void SettingsWindow::updateHeader() { m_back->setEnabled(canGoBack()); diff --git a/src/window/settingswindow.h b/src/window/settingswindow.h index 1ce6e60..57c2b59 100644 --- a/src/window/settingswindow.h +++ b/src/window/settingswindow.h @@ -90,6 +90,7 @@ private: void updateHeader(); void updateBackground(); bool isReady() const; + void retranslate(); std::unique_ptr m_window; SettingsRoot* m_root; @@ -108,6 +109,7 @@ private: std::string m_pendingWifiJoin; std::unique_ptr m_revealTimer; bool m_revealed = false; + cutefish::base::Subscription m_languageChanged; }; } // namespace settings diff --git a/src/window/sidebar.cpp b/src/window/sidebar.cpp index d6ec649..578ace4 100644 --- a/src/window/sidebar.cpp +++ b/src/window/sidebar.cpp @@ -316,6 +316,14 @@ void Sidebar::setEntries(std::vector entries) updateResults(); } +void Sidebar::retranslate() +{ + m_title->setText(tr("Settings")); + m_search->setPlaceholder(tr("Search")); + m_noResults->setText(tr("No results")); + setEntries(m_entries); +} + void Sidebar::setSubPageAvailable(const std::string& id, bool available) { auto it = std::find(m_unavailableSubPages.begin(), m_unavailableSubPages.end(), id); diff --git a/src/window/sidebar.h b/src/window/sidebar.h index 644922c..ad83418 100644 --- a/src/window/sidebar.h +++ b/src/window/sidebar.h @@ -32,6 +32,8 @@ public: // The sidebar pages shown, in order. void setEntries(std::vector entries); + // Takes its text again after the session language changed. + void retranslate(); const std::vector& entries() const { return m_entries; } // Sub pages whose entry is unavailable stay out of search results. void setSubPageAvailable(const std::string& id, bool available); diff --git a/tests/ui/foundation_test.cpp b/tests/ui/foundation_test.cpp index d86f800..ecdb83a 100644 --- a/tests/ui/foundation_test.cpp +++ b/tests/ui/foundation_test.cpp @@ -5,6 +5,7 @@ #include +#include "ui/application.h" #include "ui/keys.h" #include "ui/layout.h" #include "ui/test_input.h" @@ -105,6 +106,19 @@ TEST_F(Fixture, HistoryGoesBackAndForward) EXPECT_FALSE(settingsWindow->canGoForward()); } +TEST_F(Fixture, LanguageChangeMakesThePageAgain) +{ + settingsWindow->switchToPage("dock"); + settingsWindow->pushPage("hotcorners"); + const Page* before = settingsWindow->currentPage(); + Application::instance()->languageChanged.emit(); + window().grab(); + EXPECT_NE(settingsWindow->currentPage(), before); + EXPECT_NE(dumpLine(window(), "page:hotcorners"), ""); + EXPECT_TRUE(settingsWindow->canGoBack()); + EXPECT_FALSE(settingsWindow->canGoForward()); +} + TEST_F(Fixture, BackAndForwardKeys) { settingsWindow->switchToPage("about");