Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
commit aa59ef28ea56315eb421ba044ddaf0c4066bfff8 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Mon Nov 7 10:26:28 2022 +0100 Add trailing carrier at the end of generated extension files commit e985950aa1e39d81d4938e15f8724c0f7723b003 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Fri Nov 4 16:37:34 2022 +0100 Replace installation script by script that install completion file Setup a Makefile to install resgen commit d574384c151259610a4c2f837b14068bb7716e44 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Fri Nov 4 14:33:39 2022 +0100 Refactor Improve testability Add tests on SwiftUI generated code Add tests on command commit d9e76632c3037da0ed9e1dd37056685416579da9 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Thu Nov 3 15:43:47 2022 +0100 Fixing bad merge on FontOptions commit 76b5ebfcd1cde7a7d4c83f516a4fc937841e7715 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Thu Nov 3 15:37:28 2022 +0100 Squashed commit of the following: commit 085f1ffc3347d3c48af91ffb00a1a9b381ce47d1 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Thu Nov 3 15:00:27 2022 +0100 Refactor SwiftUI extension generation and generation SwiftUI extension for images commit 4f7d7e18b138343a07cbb0bb47213818678ced6b Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Mon Oct 31 16:21:32 2022 +0100 Génération de composant SwiftUI: Color et Image commit 0797667b2510f6fd45b9845f2d29c0c1e31da877 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Mon Oct 31 16:21:12 2022 +0100 Génération de composant SwiftUI: Color et Image commit 417a2630925841dd486ae1d684d28ab7dca240e0 Author: Thibaut Schmitt <t.schmitt@openium.fr> Date: Wed Oct 19 17:13:03 2022 +0200 Update Info.plist UIAppFonts key when generating fonts (if plist path if defined)
165 lines
6.7 KiB
Swift
165 lines
6.7 KiB
Swift
//
|
|
// StringsFileGenerator.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 04/01/2022.
|
|
//
|
|
|
|
import Foundation
|
|
import ToolCore
|
|
|
|
class StringsFileGenerator {
|
|
|
|
// MARK: - Strings Files
|
|
|
|
static func writeStringsFiles(sections: [Section], langs: [String], defaultLang: String, tags: [String], outputPath: String, inputFilenameWithoutExt: String) {
|
|
var stringsFilesContent = [String: String]()
|
|
for lang in langs {
|
|
stringsFilesContent[lang] = Self.generateStringsFileContent(lang: lang,
|
|
defaultLang: defaultLang,
|
|
tags: tags,
|
|
sections: sections)
|
|
}
|
|
|
|
// Write strings file content
|
|
langs.forEach { lang in
|
|
guard let fileContent = stringsFilesContent[lang] else { return }
|
|
|
|
let stringsFilePath = "\(outputPath)/\(lang).lproj/\(inputFilenameWithoutExt).strings"
|
|
let stringsFilePathURL = URL(fileURLWithPath: stringsFilePath)
|
|
do {
|
|
try fileContent.write(to: stringsFilePathURL, atomically: false, encoding: .utf8)
|
|
} catch (let error) {
|
|
let error = StringiumError.writeFile(error.localizedDescription, stringsFilePath)
|
|
print(error.localizedDescription)
|
|
Stringium.exit(withError: error)
|
|
}
|
|
}
|
|
}
|
|
|
|
static func generateStringsFileContent(lang: String, defaultLang: String, tags inputTags: [String], sections: [Section]) -> String {
|
|
var stringsFileContent = """
|
|
/**
|
|
* Apple Strings File
|
|
* Generated by ResgenSwift \(ResgenSwiftVersion)
|
|
* Language: \(lang)
|
|
*/\n
|
|
"""
|
|
|
|
sections.forEach { section in
|
|
// Check that at least one string will be generated
|
|
guard section.hasOneOrMoreMatchingTags(tags: inputTags) else {
|
|
return // Go to next section
|
|
}
|
|
|
|
stringsFileContent += "\n/********** \(section.name) **********/\n\n"
|
|
section.definitions.forEach { definition in
|
|
var skipDefinition = false // Set to true if not matching tag
|
|
let translationOpt: String? = {
|
|
// If no matching tag => skip
|
|
if definition.hasOneOrMoreMatchingTags(inputTags: inputTags) == false {
|
|
skipDefinition = true
|
|
return nil
|
|
}
|
|
|
|
// If tags contains `noTranslationTag` => get default lang
|
|
if definition.tags.contains(Stringium.noTranslationTag) {
|
|
return definition.translations[defaultLang]
|
|
}
|
|
|
|
// Else: get specific lang
|
|
return definition.translations[lang]
|
|
}()
|
|
|
|
if let translation = translationOpt {
|
|
stringsFileContent += "\"\(definition.name)\" = \"\(translation)\";\n\n"
|
|
} else if skipDefinition == false {
|
|
let error = StringiumError.langNotDefined(lang, definition.name, definition.reference != nil)
|
|
print(error.localizedDescription)
|
|
Stringium.exit(withError: error)
|
|
}
|
|
}
|
|
}
|
|
|
|
return stringsFileContent
|
|
}
|
|
|
|
// MARK: - Extension file
|
|
|
|
static func writeExtensionFiles(sections: [Section], defaultLang lang: String, tags: [String], staticVar: Bool, inputFilename: String, extensionName: String, extensionFilePath: String) {
|
|
// Get extension content
|
|
let extensionFileContent = Self.getExtensionContent(sections: sections,
|
|
defaultLang: lang,
|
|
tags: tags,
|
|
staticVar: staticVar,
|
|
inputFilename: inputFilename,
|
|
extensionName: extensionName)
|
|
|
|
// Write content
|
|
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
|
do {
|
|
try extensionFileContent.write(to: extensionFilePathURL, atomically: false, encoding: .utf8)
|
|
} catch (let error) {
|
|
let error = StringiumError.writeFile(extensionFilePath, error.localizedDescription)
|
|
print(error.localizedDescription)
|
|
Stringium.exit(withError: error)
|
|
}
|
|
}
|
|
|
|
// MARK: - Extension content
|
|
|
|
static func getExtensionContent(sections: [Section], defaultLang lang: String, tags: [String], staticVar: Bool, inputFilename: String, extensionName: String) -> String {
|
|
[
|
|
Self.getHeader(stringsFilename: inputFilename, extensionClassname: extensionName),
|
|
Self.getProperties(sections: sections, defaultLang: lang, tags: tags, staticVar: staticVar),
|
|
Self.getFooter()
|
|
]
|
|
.joined(separator: "\n")
|
|
}
|
|
|
|
// MARK: - Extension part
|
|
|
|
private static func getHeader(stringsFilename: String, extensionClassname: String) -> String {
|
|
"""
|
|
// Generated by ResgenSwift.Strings.\(Stringium.toolName) \(ResgenSwiftVersion)
|
|
|
|
import UIKit
|
|
|
|
fileprivate let kStringsFileName = "\(stringsFilename)"
|
|
|
|
extension \(extensionClassname) {
|
|
"""
|
|
}
|
|
|
|
private static func getProperties(sections: [Section], defaultLang lang: String, tags: [String], staticVar: Bool) -> String {
|
|
sections.compactMap { section in
|
|
// Check that at least one string will be generated
|
|
guard section.hasOneOrMoreMatchingTags(tags: tags) else {
|
|
return nil // Go to next section
|
|
}
|
|
|
|
var res = "\n // MARK: - \(section.name)\n"
|
|
res += section.definitions.compactMap { definition in
|
|
guard definition.hasOneOrMoreMatchingTags(inputTags: tags) == true else {
|
|
return nil // Go to next definition
|
|
}
|
|
|
|
if staticVar {
|
|
return "\n\(definition.getNSLocalizedStringStaticProperty(forLang: lang))"
|
|
}
|
|
return "\n\(definition.getNSLocalizedStringProperty(forLang: lang))"
|
|
}
|
|
.joined(separator: "\n")
|
|
return res
|
|
}
|
|
.joined(separator: "\n")
|
|
}
|
|
|
|
private static func getFooter() -> String {
|
|
"""
|
|
}
|
|
|
|
"""
|
|
}
|
|
}
|