Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.5.1")),
.package(url: "https://github.com/apple/swift-syntax.git", .upToNextMajor(from: "509.0.0")),
.package(url: "https://github.com/CoreOffice/XMLCoder.git", .upToNextMajor(from: "0.17.1")),
.package(url: "https://github.com/JohnSundell/Plot.git", .upToNextMajor(from: "0.14.0")),
.package(url: "https://github.com/alexisakers/HTMLString.git", .upToNextMajor(from: "6.0.0")),
.package(url: "https://github.com/stephencelis/SQLite.swift.git", .upToNextMajor(from: "0.15.4")),
],
Expand All @@ -66,7 +65,6 @@ let package = Package(
dependencies: [
"TranslationCatalog",
.product(name: "XMLCoder", package: "XMLCoder"),
.product(name: "Plot", package: "Plot"),
.product(name: "HTMLString", package: "HTMLString"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
Expand Down
22 changes: 13 additions & 9 deletions Sources/TranslationCatalogIO/ExpressionEncoder.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Plot
import TranslationCatalog

/// Utility for encoding catalog `Translation`s for export/transfer.
Expand All @@ -23,14 +22,19 @@ public struct ExpressionEncoder {
) throws -> Data {
switch format {
case .androidXML:
let sorted = expressions.sorted(by: { $0.key < $1.key })
let xml = XML.make(
with: sorted,
locale: locale,
fallback: fallback
)
let raw = xml.render(indentedBy: .spaces(2))
return raw.data(using: .utf8) ?? Data()
let sorted = expressions
.compactMap(locale: locale, fallback: fallback)
.sorted(by: { $0.key < $1.key })
let resources = sorted.map { exp -> Resource in
let multipleReplacements = exp.defaultValue.hasMultipleReplacements
return Resource(
name: exp.key,
value: exp.valueOrDefault(for: locale),
formatted: multipleReplacements ? false : nil
)
}
let strings = StringsXml(resources: resources)
return try strings.encoded()
case .appleStrings:
let sorted = expressions.sorted(by: { $0.key < $1.key })
var output: [String] = []
Expand Down
54 changes: 0 additions & 54 deletions Sources/TranslationCatalogIO/ExpressionRenderer.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Plot
import TranslationCatalog

extension TranslationCatalog.Expression {
Expand Down
110 changes: 0 additions & 110 deletions Sources/TranslationCatalogIO/Extensions/Plot+IO.swift

This file was deleted.

29 changes: 0 additions & 29 deletions Sources/TranslationCatalogIO/Extensions/XML+Expression.swift

This file was deleted.

21 changes: 15 additions & 6 deletions Sources/TranslationCatalogIO/Internal/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ import Foundation
import TranslationCatalog
import XMLCoder

struct Resource: Decodable, DynamicNodeDecoding {
struct Resource: Codable, DynamicNodeDecoding, DynamicNodeEncoding {
enum CodingKeys: String, CodingKey {
case name
case formatted
case value = ""
}

var name: String
var value: String
let name: String
let value: String
let formatted: Bool?

static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
switch key {
case CodingKeys.name:
case CodingKeys.name, CodingKeys.formatted:
.attribute
case CodingKeys.value:
default:
.element
}
}

static func nodeEncoding(for key: any CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.name, CodingKeys.formatted:
.attribute
default:
.elementOrAttribute
.element
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions Sources/TranslationCatalogIO/Internal/StringsXml.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import Foundation
import TranslationCatalog
import XMLCoder

struct StringsXml: Decodable, DynamicNodeDecoding {
struct StringsXml: Codable, DynamicNodeDecoding, DynamicNodeEncoding {
enum CodingKeys: String, CodingKey {
case resources = "string"
}

var resources: [Resource]
let resources: [Resource]

static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
.element
}

static func nodeEncoding(for key: any CodingKey) -> XMLEncoder.NodeEncoding {
.element
}

static func make(contentsOf url: URL) throws -> StringsXml {
let data = try Data(contentsOf: url)
Expand All @@ -21,6 +25,24 @@ struct StringsXml: Decodable, DynamicNodeDecoding {
static func make(with data: Data) throws -> StringsXml {
try XMLDecoder().decode(StringsXml.self, from: data)
}

func encoded() throws -> Data {
let encoder = XMLEncoder()
encoder.outputFormatting = [.sortedKeys]
let encoded = try encoder.encode(
self,
withRootKey: "resources",
header: XMLHeader(
version: 1.0,
encoding: "UTF-8"
)
)
var string = String(decoding: encoded, as: UTF8.self)
string = string.replacingOccurrences(of: "><resources>", with: ">\n<resources>")
string = string.replacingOccurrences(of: "><string", with: ">\n <string")
string = string.replacingOccurrences(of: "></resources>", with: ">\n</resources>")
return string.data(using: .utf8) ?? encoded
}
}

extension StringsXml {
Expand Down
1 change: 1 addition & 0 deletions Sources/TranslationCatalogIO/RenderFormat.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// File types which can be generated containing `Expression`/`Translation` info.
@available(*, deprecated)
public enum RenderFormat: String, CaseIterable {
case html
case markdown
Expand Down
Loading
Loading