Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

A Chinese Windows gets a Chinese interface, and the build log sits under the target choice

LNPANDY07f96b5983db
diff --git a/frontend-qt/nspb_qt/i18n.py b/frontend-qt/nspb_qt/i18n.py
index f3444d50..2684e26e 100644
--- a/frontend-qt/nspb_qt/i18n.py
+++ b/frontend-qt/nspb_qt/i18n.py
@@ -26,23 +26,32 @@ class Lang(Enum):
saved = os.environ.get('NSPB_LANG', '')
if saved in ('zh', 'en'):
return Lang(saved)
- try:
- tag = locale.getlocale()[0] or ''
- except (ValueError, TypeError):
- tag = ''
- tag = tag or os.environ.get('LANG', '') or os.environ.get('LC_ALL', '')
- if not tag and sys.platform.startswith('win'):
+ if sys.platform.startswith('win'):
+ # Ask Windows first, and always — not only when locale.getlocale() came back empty.
# The **user's** UI language, not the system's: a Chinese Windows install on
# English-labelled hardware (most handhelds ship en-US as the OS locale) has
- # LOCALE_SYSTEM_DEFAULT = en, and asking that showed an English interface to a
- # Chinese user — the report's "the exe is not fully Chinese". GetUserDefaultUILanguage
- # is what the Settings app's display language reflects.
+ # LOCALE_SYSTEM_DEFAULT = en. GetUserDefaultUILanguage is what the Settings app's
+ # display language reflects.
+ #
+ # This used to run only `if not tag`, and locale.getlocale() on Windows almost never
+ # returns empty: it returns the *locale name* ("Chinese (Simplified)_China"), which is
+ # not a BCP-47 tag, so `startswith('zh')` was False and the branch that knew better
+ # never ran. A Chinese user therefore got an English interface whatever the API said
+ # (the Windows report of "the exe is still not Chinese").
import ctypes
try:
lang = ctypes.windll.kernel32.GetUserDefaultUILanguage() & 0xFF # primary id
buf = ctypes.create_unicode_buffer(85)
if ctypes.windll.kernel32.GetLocaleInfoW(lang, 0x00000002, buf, 85): # LOCALE_SISO639LANGNAME
- tag = buf.value
+ return Lang.ZH if buf.value.lower().startswith('zh') else Lang.EN
except Exception:
- tag = ''
- return Lang.ZH if tag.lower().startswith('zh') else Lang.EN
+ pass # fall through to the environment
+ try:
+ tag = locale.getlocale()[0] or ''
+ except (ValueError, TypeError):
+ tag = ''
+ tag = tag or os.environ.get('LANG', '') or os.environ.get('LC_ALL', '')
+ # On Windows the locale name is spelled out ("Chinese (Simplified)_China"), so the prefix
+ # test alone misses every Chinese install there; the name contains "Chinese".
+ low = tag.lower()
+ return Lang.ZH if low.startswith('zh') or 'chinese' in low else Lang.EN
diff --git a/frontend-qt/tests/test_i18n_detect.py b/frontend-qt/tests/test_i18n_detect.py
new file mode 100644
index 00000000..d7618e1e
--- /dev/null
+++ b/frontend-qt/tests/test_i18n_detect.py
@@ -0,0 +1,76 @@
+"""Lang.initial() has to recognise a Chinese Windows.
+
+Two bugs stacked here, and either one alone showed a Chinese user an English interface:
+
+* `locale.getlocale()` on Windows returns the *locale name* ("Chinese (Simplified)_China"), not a
+ BCP-47 tag, so `startswith('zh')` was always False there;
+* the GetUserDefaultUILanguage fallback ran only `if not tag`, and that name is never empty — so
+ the call that knew better was unreachable.
+
+The Windows API is what the Settings app's display language reflects, and it is now asked first
+and unconditionally. These tests drive the environment, not the OS, so they run anywhere.
+"""
+from __future__ import annotations
+
+import os
+import sys
+import unittest
+from pathlib import Path
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parents[2]
+sys.path.insert(0, str(ROOT / 'frontend-qt'))
+
+from nspb_qt.i18n import Lang # noqa: E402
+
+
+
+def detect(locale_name: str, platform: str = 'darwin', env: dict | None = None) -> Lang:
+ """Lang.initial() with locale.getlocale() answering `locale_name` and only `env` set."""
+ with mock.patch.dict(os.environ, env or {}, clear=True), \
+ mock.patch('locale.getlocale', return_value=(locale_name, None)), \
+ mock.patch.object(sys, 'platform', platform):
+ return Lang.initial()
+
+
+class Detect(unittest.TestCase):
+ def test_a_saved_choice_wins(self):
+ self.assertEqual(detect('English_United States', env={'NSPB_LANG': 'zh'}), Lang.ZH)
+ self.assertEqual(detect('Chinese (Simplified)_China', env={'NSPB_LANG': 'en'}), Lang.EN)
+
+ def test_the_windows_locale_name_is_recognised(self):
+ for name in ('Chinese (Simplified)_China', 'Chinese (Traditional)_Taiwan', 'Chinese_China'):
+ self.assertEqual(detect(name, platform='win32'), Lang.ZH, name)
+ for name in ('English_United States', 'Japanese_Japan'):
+ self.assertEqual(detect(name, platform='win32'), Lang.EN, name)
+
+ def test_a_bcp47_tag_still_works(self):
+ for tag in ('zh_CN', 'zh-Hans', 'zh'):
+ self.assertEqual(detect(tag), Lang.ZH, tag)
+ self.assertEqual(detect('en_US'), Lang.EN)
+
+ def test_windows_is_asked_before_the_environment(self):
+ """The API decides on Windows even though locale.getlocale() returned a name — that was the bug."""
+ fake = mock.Mock()
+ fake.windll.kernel32.GetUserDefaultUILanguage.return_value = 0x04 # zh-CN primary id
+
+ def getlocale(lang, kind, buf, n):
+ buf.value = 'zh'
+ return 1
+ fake.windll.kernel32.GetLocaleInfoW.side_effect = getlocale
+ with mock.patch.dict(os.environ, {}, clear=True), \
+ mock.patch.dict(sys.modules, {'ctypes': fake}), \
+ mock.patch('locale.getlocale', return_value=('English_United States', None)), \
+ mock.patch.object(sys, 'platform', 'win32'):
+ self.assertEqual(Lang.initial(), Lang.ZH)
+
+ def test_an_empty_locale_falls_through_to_the_environment(self):
+ self.assertEqual(detect('', env={'LANG': 'zh_CN.UTF-8'}), Lang.ZH)
+ self.assertEqual(detect('', env={'LANG': 'en_US.UTF-8'}), Lang.EN)
+
+ def test_no_signal_at_all_is_english(self):
+ self.assertEqual(detect(''), Lang.EN)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index cdd5d62a..1ba479ca 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -1817,6 +1817,7 @@ struct BuilderView: View {
files.disabled(m.running)
target
if m.target != "mac-app" { install.disabled(m.running) }
+ logPanel
action
outcome
progress
@@ -2029,13 +2030,20 @@ struct BuilderView: View {
if m.stages.isEmpty && m.running {
row(m.t("library.preparing")) { ProgressView().controlSize(.small) }
}
- divider
- disclosure(m.t("build.log"), isOpen: logOpen) { logOpen.toggle() }
- if logOpen { logView.padding(.horizontal, 14).padding(.bottom, 14) }
}
}
}
+ /// The build log, right under the Mac / iPhone choice and folded away until it is asked for.
+ /// It used to sit at the bottom of the progress card, which is where a failed build's message
+ /// pointed ("see the log below") while the log itself was out of view on a short window (#52).
+ @ViewBuilder private var logPanel: some View {
+ group(m.t("section.log")) {
+ disclosure(m.t("build.log"), isOpen: logOpen) { logOpen.toggle() }
+ if logOpen { logView.padding(.horizontal, 14).padding(.bottom, 14) }
+ }
+ }
+
private var logView: some View {
ScrollViewReader { sp in
ScrollView {
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index cd38d8b1..cb3b82cb 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -92,6 +92,7 @@ enum L10n {
"build.saveTo": ("\u{4fdd}\u{5b58}\u{5230}", "Save to"),
"build.saveDefault": ("\u{6e38}\u{620f}\u{6587}\u{4ef6}\u{65c1}\u{8fb9}", "Next to the game files"),
"build.log": ("\u{6784}\u{5efa}\u{65e5}\u{5fd7}", "Build log"),
+ "section.log": ("\u{6784}\u{5efa}\u{65e5}\u{5fd7}", "Log"),
"library.folderUnset": ("\u{5c1a}\u{672a}\u{8bbe}\u{7f6e}", "Not set"),
"app.version": ("\u{7248}\u{672c} %@", "Version %@"),
"library.scrollBack": ("\u{5411}\u{524d}\u{6eda}\u{52a8}", "Scroll back"),