-
Notifications
You must be signed in to change notification settings - Fork 10
/
LocalizableFileSorter.swift
38 lines (33 loc) · 1.37 KB
/
LocalizableFileSorter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env swift
// Copyright (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Foundation
let strDataPath = "./"
func handleFiles(_ handler: @escaping ((url: URL, fileName: String)) -> ()) {
let rawURLs = FileManager.default.enumerator(
at: URL(fileURLWithPath: strDataPath),
includingPropertiesForKeys: nil
)?.compactMap { $0 as? URL }
rawURLs?.forEach { url in
guard let fileName = url.pathComponents.last,
fileName.lowercased() == "localizable.strings" else { return }
handler((url, fileName))
}
}
handleFiles { url, fileName in
guard let rawStr = try? String(contentsOf: url, encoding: .utf8) else { return }
let locale = Locale(identifier: "zh@collation=stroke")
do {
try rawStr.components(separatedBy: .newlines).filter { !$0.isEmpty }.sorted {
$0.compare($1, locale: locale) == .orderedAscending
}.joined(separator: "\n").description.appending("\n")
.write(to: url, atomically: false, encoding: .utf8)
} catch {
print("!! Error writing to \(fileName)")
}
}