-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringsFromSwift.swift
More file actions
155 lines (137 loc) · 6.34 KB
/
StringsFromSwift.swift
File metadata and controls
155 lines (137 loc) · 6.34 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// ByvLocalizableStringsGenerator.swift
// ByvLocalizableStringsGenerator
//
// Created by Adrian Apodaca on 2/5/17.
// Copyright © 2017 byvapps. All rights reserved.
//
import Foundation
func stringsFromSwiftFiles() {
//guard let files = dir?.findFilesRecursively(byName: "example.swift") else {
guard let files = dir?.findFilesRecursively(byExtension: "swift") else {
abort()
}
for file in files {
// get AST output from sourcekitten library
let output = shell(
launchPath: "/usr/bin/env",
arguments: [
"sourcekitten",
"structure",
"--file",
"\(file.path)"
]
)
let data = output.data(using: .utf8)
var contents: String? = nil
do {
let json = try JSONSerialization.jsonObject(
with: data!,
options: JSONSerialization.ReadingOptions.allowFragments
)
_ = Recursive(json: json) {
value, path in
guard let name = value["key.name"] as? String else {
return
}
//check .localize()
if name.contains("\".localize") {
// this part gets the actual string that needs translating
var parts = name.components(separatedBy: "\".localize")
for i in 0...parts.count - 2 {
let part = parts[i]
if let str = part.components(separatedBy: "\"").last {
let string = str
let comment = ""
if string.count > 0 {
var translation = Translation(comment: comment, files: [file.name])
if let storedTranslation = translations[string] {
var files = storedTranslation.files
files.append(file.name)
translation.files = files
}
translations[string] = translation
}
}
}
} else if name.contains(".locText") ||
name.contains(".locTitle") ||
name.contains(".locPlaceholder") ||
name.contains(".styledLocText") ||
name.contains(".styledLocTitle") ||
name.contains(".styledLocPlaceholder") ||
name.contains("NSLocalizedString") {
var string = ""
var stringOffset = -1
var stringLength = -1
var commentOffset = -1
var commentLength = -1
var comment = ""
if let subArray = value["key.substructure"] as? [[String: Any]] {
for sub in subArray {
guard let offset = sub["key.bodyoffset"] as? NSNumber else {return}
guard let length = sub["key.bodylength"] as? NSNumber else {return}
if let name = sub["key.name"] as? String {
if name == "comment" {
commentOffset = offset.intValue
commentLength = length.intValue
} else if name == "format" {
stringOffset = offset.intValue
stringLength = length.intValue
}
} else {
// Can only be string
stringOffset = offset.intValue
stringLength = length.intValue
}
}
} else {
// Only text -> string => .locText("")
if let offset = value["key.bodyoffset"] as? NSNumber ,
let length = value["key.bodylength"] as? NSNumber {
stringOffset = offset.intValue
stringLength = length.intValue
}
}
if contents == nil {
contents = file.contents()
}
guard let contents = contents else {return}
// string
if stringOffset > 0 && stringLength > 0 {
let quotedString = contents.substring(
with: (stringOffset)
..< (stringOffset + stringLength)
)
string = quotedString.trimmingCharacters(in: CharacterSet(["\""]))
if quotedString == string {
// No quoted string, so it should be a variable
string = ""
}
}
// comment
if commentOffset > 0 && commentLength > 0 {
comment = contents.substring(
with: (commentOffset)
..< (commentOffset + commentLength)
)
comment = comment.trimmingCharacters(in: CharacterSet(["\""]))
}
if string.count > 0 {
var translation = Translation(comment: comment, files: [file.name])
if let storedTranslation = translations[string] {
var files = storedTranslation.files
files.append(file.name)
translation.files = files
}
translations[string] = translation
}
}
}
}
catch let error as NSError {
print(error.localizedDescription)
}
}
return
}