Skip to content

Commit

Permalink
[common] Integrate Procursus’s full localization support (yay!)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirb committed Aug 6, 2020
1 parent e5bcb70 commit 2f919bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion Common/Controllers/SubProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()!
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion control
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 2f919bd

Please sign in to comment.