Ai
8 Star 9 Fork 3

Gitee 极速下载/IINA

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/lhc70000/iina
克隆/下载
check_localizable.swift 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
Collider LI 提交于 2018-09-10 00:02 +08:00 . Add fix for other languages
#!/usr/bin/xcrun swift
import Cocoa
extension String {
var escaped: String {
return self.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n")
}
}
struct StandardErrorOutputStream: TextOutputStream {
let stderr = FileHandle.standardError
func write(_ string: String) {
if let data = string.data(using: .utf8) {
stderr.write(data)
}
}
}
var stderr = StandardErrorOutputStream()
func error(_ message: String) -> Never {
print(message, to: &stderr)
exit(1)
}
func append(_ text: String, to fileURL: URL) {
guard let fileHandle = try? FileHandle(forWritingTo: fileURL) else {
error("Cannot open file \(fileURL).")
}
fileHandle.seekToEndOfFile()
fileHandle.write(text.data(using: .utf8)!)
fileHandle.closeFile()
}
let fm = FileManager.default
let callPath = CommandLine.arguments[0]
let selfURL: URL
if callPath.hasPrefix("/") {
selfURL = URL(fileURLWithPath: callPath)
} else {
selfURL = URL(fileURLWithPath: fm.currentDirectoryPath).appendingPathComponent(callPath)
}
let projRootURL = selfURL.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("iina", isDirectory: true)
// base Localizable.strings file
let baseFileURL = projRootURL.appendingPathComponent("Base.lproj/Localizable.strings")
// lproj folders
guard let lProjURLs = try? fm
.contentsOfDirectory(at: projRootURL, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants])
.filter { $0.hasDirectoryPath && $0.lastPathComponent.hasSuffix(".lproj") && $0.lastPathComponent != "Base.lproj" }, lProjURLs.count > 1 else {
error("Cannot find localization directories.")
}
print("Checking Localizable.strings for \(lProjURLs.count) localizations……")
// Localizable.strings files
let stringFileURLs = lProjURLs.map { $0.appendingPathComponent("Localizable.strings") }
// make sure all files exist
stringFileURLs.forEach { url in
guard fm.fileExists(atPath: url.path) else {
error("Localizable.strings doesn't exist: \(url.path)")
}
}
// read base file
guard let baseDict = NSDictionary(contentsOf: baseFileURL) as? [String: String] else {
error("Base Localizable.strings doesn't exist.")
}
let baseKeys = baseDict.keys
var missingKeys: [(String, URL)] = []
// check l10n files
for stringFileURL in stringFileURLs {
// read l10n file
guard let l10nDict = NSDictionary(contentsOf: stringFileURL) as? [String: String] else {
error("File \(stringFileURL.path) doesn't exist.")
}
for baseKey in baseKeys {
if l10nDict[baseKey] == nil {
missingKeys.append((baseKey, stringFileURL))
}
}
}
if CommandLine.arguments.count > 1 && CommandLine.arguments[1] == "--fix" {
// fix
print("Fixing errors……")
for (key, file) in missingKeys {
append("""
/* FIXME: Using English localization instead */
"\(key)" = "\(baseDict[key]!.escaped)";
""", to: file)
print("Fixed \(key) in \(file.path)")
}
} else {
// output error
guard missingKeys.isEmpty else {
error(missingKeys.map { "Missing key \"\($0.0)\" in \($0.1.path)." }.joined(separator: "\n"))
}
}
print("Checking Finished.")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/mirrors/IINA.git
git@gitee.com:mirrors/IINA.git
mirrors
IINA
IINA
develop

搜索帮助