You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Swift命令行工具转.strings为.csv?SwiftGen API是否适用?

Can SwiftGen API convert Localizable.strings to .csv? What's the optimal solution?

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:

  1. Install SwiftGen (if you haven’t already):
    brew install swiftgen
    
  2. 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 escape filter ensures special characters (like double quotes or newlines) are properly escaped to keep your CSV valid.
  3. Run SwiftGen with your template:
    Point SwiftGen to your Localizable.strings file, 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:

  1. Create a Swift command-line tool (via Swift Package Manager):
    Add SwiftGenKit as a dependency in your Package.swift:
    dependencies: [
        .package(url: "https://github.com/SwiftGen/SwiftGenKit", from: "6.6.0")
    ]
    
  2. Write code to parse and generate CSV:
    Use StringsParser from SwiftGenKit to 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)")
    }
    
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:41:26