Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
156 lines
6.2 KiB
Swift
156 lines
6.2 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: true, encoding: .utf8)
|
|
} catch (let error) {
|
|
let error = StringiumError.writeFile(error.localizedDescription, stringsFilePath)
|
|
print(error.localizedDescription)
|
|
Stringium.exit(withError: error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private 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) {
|
|
let extensionHeader = Self.getHeader(stringsFilename: inputFilename, extensionClassname: extensionName)
|
|
let extensionFooter = Self.getFooter()
|
|
|
|
let extensionContent: String = {
|
|
var content = ""
|
|
sections.forEach { section in
|
|
// Check that at least one string will be generated
|
|
guard section.hasOneOrMoreMatchingTags(tags: tags) else {
|
|
return // Go to next section
|
|
}
|
|
|
|
content += "\n\t// MARK: - \(section.name)"
|
|
section.definitions.forEach { definition in
|
|
guard definition.hasOneOrMoreMatchingTags(inputTags: tags) == true else {
|
|
return // Go to next definition
|
|
}
|
|
|
|
if staticVar {
|
|
content += "\n\n\(definition.getNSLocalizedStringStaticProperty(forLang: lang))"
|
|
} else {
|
|
content += "\n\n\(definition.getNSLocalizedStringProperty(forLang: lang))"
|
|
}
|
|
}
|
|
content += "\n"
|
|
}
|
|
return content
|
|
}()
|
|
|
|
// Create file if not exists
|
|
let fileManager = FileManager()
|
|
if fileManager.fileExists(atPath: extensionFilePath) == false {
|
|
Shell.shell("touch", "\(extensionFilePath)")
|
|
}
|
|
|
|
// Create extension content
|
|
let extensionFileContent = [extensionHeader, extensionContent, extensionFooter].joined(separator: "\n")
|
|
|
|
// Write content
|
|
let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
|
|
do {
|
|
try extensionFileContent.write(to: extensionFilePathURL, atomically: true, encoding: .utf8)
|
|
} catch (let error) {
|
|
let error = StringiumError.writeFile(extensionFilePath, error.localizedDescription)
|
|
print(error.localizedDescription)
|
|
Stringium.exit(withError: error)
|
|
}
|
|
}
|
|
|
|
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 getFooter() -> String {
|
|
"""
|
|
}
|
|
"""
|
|
}
|
|
}
|