Skip to content

Latest commit

 

History

History
77 lines (56 loc) · 1.87 KB

File metadata and controls

77 lines (56 loc) · 1.87 KB

XCSWriter

XCSWriter handles the persistence side of the package. It can serialize a typed Catalog, apply targeted StringPatch updates to existing JSON bytes, and write the result to disk.

Public API

XCSWriterError

  • invalidJSON
  • invalidCatalogShape(path:)
  • writeFailed(path:)

Serialization

public static func serialize(
    _ catalog: Catalog,
    prettyPrinted: Bool = true,
    sortedKeys: Bool = true
) throws -> Data

Patch Application

public static func apply(
    patches: [StringPatch],
    to originalData: Data,
    prettyPrinted: Bool = true,
    sortedKeys: Bool = true
) throws -> Data

File Writing

public static func write(_ data: Data, to fileURL: URL) throws
public static func write(
    _ catalog: Catalog,
    to fileURL: URL,
    prettyPrinted: Bool = true,
    sortedKeys: Bool = true
) throws

Basic Usage

Serialize a typed catalog:

import XCSWriter

let data = try XCSWriter.serialize(catalog)
try XCSWriter.write(data, to: outputURL)

Apply patches returned by the translator:

let updatedData = try XCSWriter.apply(patches: result.patches, to: originalData)
try XCSWriter.write(updatedData, to: outputURL)

Write a typed catalog directly:

try XCSWriter.write(catalog, to: outputURL)

Behavior Notes

  • prettyPrinted and sortedKeys default to true for stable, readable output.
  • Patch application preserves existing sibling fields and only updates the targeted path.
  • Missing entry, localization, and variation containers are synthesized automatically during patch application.
  • apply expects the original data to be valid JSON with a catalog-like root object containing strings.

If you are starting from a parsed catalog and want to persist translation updates, the common pairing is XCSTranslator plus XCSWriter.apply.