Skip to content

Commit 55944c4

Browse files
committed
improve for real usage
1 parent 21d6bb4 commit 55944c4

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

Sources/ConvertStringCatalogToAndroidXML/ConvertStringCatalogToAndroidXML.swift

+24-17
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ struct ConvertStringCatalogToAndroidXML: ParsableCommand {
88
@Option(help: "Specify the path to the xcstrings file")
99
public var xcstringsPath: String
1010

11-
@Option(help: "Output language")
12-
public var outputLanguage: String
11+
@Option(help: "Base language")
12+
public var baseLanguage: String
1313

1414
@Option(help: "Output path for the generated Android XML file")
1515
public var outputPath: String
@@ -19,20 +19,27 @@ struct ConvertStringCatalogToAndroidXML: ParsableCommand {
1919
print("Could not parse file at \(xcstringsPath)")
2020
throw ExitCode.failure
2121
}
22-
23-
let outputLanguage = StringLanguage(rawValue: outputLanguage)
24-
25-
let xmlDocument = catalog.converted(to: outputLanguage)
26-
27-
let url = URL(fileURLWithPath: outputPath + "/values-\(outputLanguage.rawValue)/")
28-
29-
try! FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
30-
31-
try! xmlDocument.prettyPrinted
32-
.write(
33-
toFile: url.path() + "/strings.xml",
34-
atomically: true,
35-
encoding: .utf8
36-
)
22+
23+
let baseLanguage = StringLanguage(rawValue: baseLanguage)
24+
25+
for outputLanguage in catalog.languages {
26+
let xmlDocument = catalog.converted(to: outputLanguage)
27+
28+
let url = (outputLanguage == baseLanguage)
29+
? URL(fileURLWithPath: outputPath + "/values/")
30+
: URL(fileURLWithPath: outputPath + "/values-\(outputLanguage.rawValue)/")
31+
32+
try! FileManager.default.createDirectory(
33+
at: url,
34+
withIntermediateDirectories: true
35+
)
36+
37+
try! xmlDocument.prettyPrinted
38+
.write(
39+
toFile: url.path() + "/strings.xml",
40+
atomically: true,
41+
encoding: .utf8
42+
)
43+
}
3744
}
3845
}

Sources/StringCatalogConverterLibrary/StringCatalog+Conversion.swift

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,38 @@ import Foundation
22
import StringCatalog
33

44
extension StringCatalog {
5-
public func converted(to language: StringLanguage) -> XMLDocument {
5+
static func cleanupKeyForAndroid(_ key: String) -> String {
6+
key
7+
.replacingOccurrences(of: ".", with: "_")
8+
.replacingOccurrences(of: "-", with: "_")
9+
}
10+
11+
static func cleanupValueForAndroid(_ value: String) -> String {
12+
value
13+
.replacingOccurrences(of: "'", with: "\\'")
14+
}
15+
16+
public func converted(
17+
to language: StringLanguage
18+
) -> XMLDocument {
619
let resources = XMLElement(name: "resources")
720

821
for string in strings.sorted(by: { lhs, rhs in
922
lhs.key < rhs.key
1023
}) {
24+
guard let value = string.value.localizations![language]?.stringUnit?.value else {
25+
continue
26+
}
27+
1128
let element = XMLElement(
12-
name: "string", stringValue: string.value.localizations![language]?.stringUnit?.value ?? "")
29+
name: "string", stringValue: Self.cleanupValueForAndroid(value))
1330

1431
// TODO: implement plurals
1532

1633
element.addAttribute(
1734
XMLNode.attribute(
1835
withName: "name",
19-
stringValue: string.key.replacingOccurrences(of: ".", with: "_")
36+
stringValue: Self.cleanupKeyForAndroid(string.key)
2037
) as! XMLNode
2138
)
2239

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
import StringCatalog
3+
4+
public extension StringCatalog {
5+
var languages: Set<StringLanguage> {
6+
Set(
7+
strings.values
8+
.map(\.localizations)
9+
.compactMap(\.?.keys)
10+
.flatMap{ $0 }
11+
)
12+
}
13+
}

Tests/StringCatalogConverterLibraryTests/Resources/SimpleTest/Output.de.xml

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<string name="error_request">Bei der Anfrage ist ein Fehler aufgetreten.</string>
99
<string name="error_universal">Fehler</string>
1010
<string name="error_universal_detail">Ein unbekannter Fehler ist aufgetreten.</string>
11-
<string name="messagesOfTheDay"></string>
1211
<string name="settings_bugreport_alert_action">Ok</string>
1312
<string name="settings_bugreport_alert_message">E-Mail Account muss eingerichtet sein</string>
1413
<string name="settings_bugreport_alert_title">Fehler</string>
@@ -20,5 +19,4 @@
2019
<string name="title_collection">Collection</string>
2120
<string name="title_counter">Counter</string>
2221
<string name="title_settings">Einstellungen</string>
23-
<string name="upcomingEvents"></string>
2422
</resources>

Tests/StringCatalogConverterLibraryTests/Resources/SimpleTest/Output.en.xml

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<string name="error_request">An error occurred during this request.</string>
99
<string name="error_universal">Error</string>
1010
<string name="error_universal_detail">An unknown error occurred.</string>
11-
<string name="messagesOfTheDay"></string>
1211
<string name="settings_bugreport_alert_action">Ok</string>
1312
<string name="settings_bugreport_alert_message">E-mail account must be set up</string>
1413
<string name="settings_bugreport_alert_title">Error</string>
@@ -20,5 +19,4 @@
2019
<string name="title_collection">Collection</string>
2120
<string name="title_counter">Counter</string>
2221
<string name="title_settings">Settings</string>
23-
<string name="upcomingEvents"></string>
2422
</resources>

0 commit comments

Comments
 (0)