Skip to content

Instantly share code, notes, and snippets.

@billypchan
Last active July 18, 2023 12:04
Show Gist options
  • Save billypchan/426e279ee565839cf9559ebc43c4af31 to your computer and use it in GitHub Desktop.
Save billypchan/426e279ee565839cf9559ebc43c4af31 to your computer and use it in GitHub Desktop.
decode Localizable.xcstrings
//
// main.swift
// LocalizeCatalogCodable
//
// Created by Bill, Yiu Por Chan on 18.07.23.
//
import Foundation
struct Localizable: Codable {
let sourceLanguage: String
let strings: [String: StringItem]
let version: String
struct StringItem: Codable {
let localizations: [String: Localization]?
struct Localization: Codable {
let stringUnit: StringUnit
struct StringUnit: Codable {
let state: String
let value: String
}
}
}
}
func createStructFromFile(filePath: String) -> Localizable{
let fileURL = URL(fileURLWithPath: filePath)
let localizable: Localizable
do {
let jsonData = try Data(contentsOf: fileURL)
let decoder = JSONDecoder()
localizable = try decoder.decode(Localizable.self, from: jsonData)
// Access the decoded struct properties here
} catch {
print("Error decoding JSON: \(error)")
exit(1)
}
return localizable
}
//example
let localizable = createStructFromFile(filePath: "/Users/bill/Documents/Localizable.xcstrings")
print(localizableAll.count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment