From 2f919bda91d0b0accfdf7367be207eabf64141af Mon Sep 17 00:00:00 2001 From: Adam Demasi Date: Thu, 6 Aug 2020 13:25:24 +0930 Subject: [PATCH] =?UTF-8?q?[common]=20Integrate=20Procursus=E2=80=99s=20fu?= =?UTF-8?q?ll=20localization=20support=20(yay!)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Controllers/SubProcess.swift | 22 +++++++++++++++++++++- control | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Common/Controllers/SubProcess.swift b/Common/Controllers/SubProcess.swift index 6b6621e..f527eee 100644 --- a/Common/Controllers/SubProcess.swift +++ b/Common/Controllers/SubProcess.swift @@ -70,6 +70,7 @@ class SubProcess: NSObject { windowSize.ws_row = SubProcess.defaultHeight fileDescriptor = Int32() + let localeCode = self.localeCode let pid = forkpty(&fileDescriptor!, nil, nil, &windowSize) switch pid { @@ -89,7 +90,7 @@ class SubProcess: NSObject { let env = ([ "TERM=xterm-color", - "LANG=en_US.UTF-8", + "LANG=\(localeCode)", "TERM_PROGRAM=NewTerm", "LC_TERMINAL=NewTerm" ] as NSArray).cStringArray()! @@ -176,6 +177,25 @@ class SubProcess: NSObject { fileHandle!.write(data) } + private var localeCode: String { + // Try and find a locale suitable for the user. Use en_US.UTF-8 as fallback. + // TODO: There has to be a better way to get a gettext locale out of the Apple locale. For + // instance, a phone set to Simplified Chinese but a region of Australia will only have the + // language zh_AU… which isn’t a thing. But gettext only has languages in country pairs, no + // safe generic fallbacks exist, like zh-Hans in this case. + for language in Locale.preferredLanguages { + let locale = Locale(identifier: language) + if let languageCode = locale.languageCode, let regionCode = locale.regionCode { + let identifier = "\(languageCode)_\(regionCode).UTF-8" + let url = URL(fileURLWithPath: "/usr/share/locale").appendingPathComponent(identifier) + if (try? url.checkResourceIsReachable()) == true { + return identifier + } + } + } + return "en_US.UTF-8" + } + deinit { if childPID != nil { NSLog("warning: illegal state — SubProcess deallocated while still running") diff --git a/control b/control index 6a54c12..b3e4a5d 100644 --- a/control +++ b/control @@ -1,6 +1,6 @@ Package: ws.hbang.newterm2 Name: NewTerm (iOS 10 – 13) -Depends: firmware (>= 10.0), firmware (>= 12.2) | org.swift.libswift, firmware (>= 13.0) | ws.hbang.newtermfonts, ws.hbang.common (>= 1.14), bash, coreutils, ncurses, system-cmds, com.linusyang.localeutf8 +Depends: firmware (>= 10.0), firmware (>= 12.2) | org.swift.libswift, firmware (>= 13.0) | ws.hbang.newtermfonts, ws.hbang.common (>= 1.14), bash, coreutils, ncurses, system-cmds, gettext-localizations | com.linusyang.localeutf8 Conflicts: ws.hbang.newterm, com.lint.ntarrows Replaces: ws.hbang.newterm, com.lint.ntarrows Version: 2.4