如何通过Swift命令行工具转.strings为.csv?SwiftGen API是否适用?
Absolutely—you can use SwiftGen for this task, though the approach you pick depends on whether you want a lightweight command-line workflow or need more programmatic control. Let’s break down the options, starting with the optimal, most straightforward solution.
Optimal Solution: Use SwiftGen with a Custom Stencil Template
SwiftGen is best known for generating Swift code from localization files, but its template system (powered by Stencil) lets you output any format—including CSV. This is the top choice because it avoids writing custom code and leverages SwiftGen’s built-in string parsing capabilities out of the box.
Step-by-Step Implementation:
- Install SwiftGen (if you haven’t already):
brew install swiftgen - Create a custom Stencil template (save it as
localizable-to-csv.stencil):
This template loops through all localization entries and formats them into valid CSV, handling escaped quotes and basic formatting rules:"Key","Value" {% for entry in entries %} "{{ entry.key|escape }}","{{ entry.value|escape }}" {% endfor %}- The
escapefilter ensures special characters (like double quotes or newlines) are properly escaped to keep your CSV valid.
- The
- Run SwiftGen with your template:
Point SwiftGen to yourLocalizable.stringsfile, specify the custom template, and set your output CSV path:swiftgen strings --template-path localizable-to-csv.stencil --output localizable.csv ./path/to/Localizable.strings
For multi-language projects (e.g., en.lproj/Localizable.strings, zh-Hans.lproj/Localizable.strings), you can adjust the template to add a "Language" column, or run the command separately for each language and merge the CSVs afterward.
Using SwiftGen API for Programmatic Control
If you need extra flexibility—like merging multiple localization files, adding custom logic, or integrating this into a larger tool—you can use SwiftGen’s underlying SwiftGenKit library to parse .strings files and generate CSV programmatically.
Step-by-Step Implementation:
- Create a Swift command-line tool (via Swift Package Manager):
AddSwiftGenKitas a dependency in yourPackage.swift:dependencies: [ .package(url: "https://github.com/SwiftGen/SwiftGenKit", from: "6.6.0") ] - Write code to parse and generate CSV:
UseStringsParserfromSwiftGenKitto read localization entries, then format them into CSV:import SwiftGenKit func convertStringsToCSV(inputPath: String, outputPath: String) throws { let parser = StringsParser() try parser.parse(path: inputPath) // Initialize CSV header var csvContent = "Key,Value\n" // Process each localization entry for entry in parser.entries { // Escape double quotes to comply with CSV standards let escapedKey = entry.key.replacingOccurrences(of: "\"", with: "\"\"") let escapedValue = entry.value.replacingOccurrences(of: "\"", with: "\"\"") csvContent += "\"\(escapedKey)\",\"\(escapedValue)\"\n" } // Write the final CSV to file try csvContent.write(toFile: outputPath, atomically: true, encoding: .utf8) } // Example usage do { try convertStringsToCSV( inputPath: "./path/to/Localizable.strings", outputPath: "./localizable.csv" ) print("CSV generated successfully!") } catch { print("Error: \(error.localizedDescription)") } - Compile and run your tool to generate the CSV file.
Which Approach Should You Choose?
- Stencil Template: Go with this for most cases—it’s fast, requires no custom code compilation, and uses SwiftGen’s battle-tested parsing logic.
- SwiftGen API: Use this only when you need custom logic that can’t be easily expressed in a template (e.g., complex data transformations, integrating with other tools).
内容的提问来源于stack exchange,提问作者birdy

