Skip to content

Commit

Permalink
improve for real usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutzifer committed Oct 29, 2024
1 parent 21d6bb4 commit 55944c4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct ConvertStringCatalogToAndroidXML: ParsableCommand {
@Option(help: "Specify the path to the xcstrings file")
public var xcstringsPath: String

@Option(help: "Output language")
public var outputLanguage: String
@Option(help: "Base language")
public var baseLanguage: String

@Option(help: "Output path for the generated Android XML file")
public var outputPath: String
Expand All @@ -19,20 +19,27 @@ struct ConvertStringCatalogToAndroidXML: ParsableCommand {
print("Could not parse file at \(xcstringsPath)")
throw ExitCode.failure
}

let outputLanguage = StringLanguage(rawValue: outputLanguage)

let xmlDocument = catalog.converted(to: outputLanguage)

let url = URL(fileURLWithPath: outputPath + "/values-\(outputLanguage.rawValue)/")

try! FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)

try! xmlDocument.prettyPrinted
.write(
toFile: url.path() + "/strings.xml",
atomically: true,
encoding: .utf8
)

let baseLanguage = StringLanguage(rawValue: baseLanguage)

for outputLanguage in catalog.languages {
let xmlDocument = catalog.converted(to: outputLanguage)

let url = (outputLanguage == baseLanguage)
? URL(fileURLWithPath: outputPath + "/values/")
: URL(fileURLWithPath: outputPath + "/values-\(outputLanguage.rawValue)/")

try! FileManager.default.createDirectory(
at: url,
withIntermediateDirectories: true
)

try! xmlDocument.prettyPrinted
.write(
toFile: url.path() + "/strings.xml",
atomically: true,
encoding: .utf8
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@ import Foundation
import StringCatalog

extension StringCatalog {
public func converted(to language: StringLanguage) -> XMLDocument {
static func cleanupKeyForAndroid(_ key: String) -> String {
key
.replacingOccurrences(of: ".", with: "_")
.replacingOccurrences(of: "-", with: "_")
}

static func cleanupValueForAndroid(_ value: String) -> String {
value
.replacingOccurrences(of: "'", with: "\\'")
}

public func converted(
to language: StringLanguage
) -> XMLDocument {
let resources = XMLElement(name: "resources")

for string in strings.sorted(by: { lhs, rhs in
lhs.key < rhs.key
}) {
guard let value = string.value.localizations![language]?.stringUnit?.value else {
continue
}

let element = XMLElement(
name: "string", stringValue: string.value.localizations![language]?.stringUnit?.value ?? "")
name: "string", stringValue: Self.cleanupValueForAndroid(value))

// TODO: implement plurals

element.addAttribute(
XMLNode.attribute(
withName: "name",
stringValue: string.key.replacingOccurrences(of: ".", with: "_")
stringValue: Self.cleanupKeyForAndroid(string.key)
) as! XMLNode
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation
import StringCatalog

public extension StringCatalog {
var languages: Set<StringLanguage> {
Set(
strings.values
.map(\.localizations)
.compactMap(\.?.keys)
.flatMap{ $0 }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<string name="error_request">Bei der Anfrage ist ein Fehler aufgetreten.</string>
<string name="error_universal">Fehler</string>
<string name="error_universal_detail">Ein unbekannter Fehler ist aufgetreten.</string>
<string name="messagesOfTheDay"></string>
<string name="settings_bugreport_alert_action">Ok</string>
<string name="settings_bugreport_alert_message">E-Mail Account muss eingerichtet sein</string>
<string name="settings_bugreport_alert_title">Fehler</string>
Expand All @@ -20,5 +19,4 @@
<string name="title_collection">Collection</string>
<string name="title_counter">Counter</string>
<string name="title_settings">Einstellungen</string>
<string name="upcomingEvents"></string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<string name="error_request">An error occurred during this request.</string>
<string name="error_universal">Error</string>
<string name="error_universal_detail">An unknown error occurred.</string>
<string name="messagesOfTheDay"></string>
<string name="settings_bugreport_alert_action">Ok</string>
<string name="settings_bugreport_alert_message">E-mail account must be set up</string>
<string name="settings_bugreport_alert_title">Error</string>
Expand All @@ -20,5 +19,4 @@
<string name="title_collection">Collection</string>
<string name="title_counter">Counter</string>
<string name="title_settings">Settings</string>
<string name="upcomingEvents"></string>
</resources>

0 comments on commit 55944c4

Please sign in to comment.