Skip to content

Commit

Permalink
LocalizationEnumBuild: Fix SwiftUI with regards to the markdown parser.
Browse files Browse the repository at this point in the history
The automatic markdown conversion to `AttributedString` only works with the `LocalizedStringKey` initializer.
  • Loading branch information
mickeyl committed Feb 7, 2022
1 parent 5aa687b commit ef40408
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 14 additions & 9 deletions Sources/Shark/LocalizationEnumBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private enum LocalizationValue: Comparable {
}
}

func declaration(withBody body: String = "", indentLevel: Int) throws -> String {
func declaration(withBody body: String = "", indentLevel: Int, framework: Framework) throws -> String {
var result = ""
switch self {
case .namespace(let name):
Expand All @@ -73,7 +73,12 @@ private enum LocalizationValue: Comparable {
if interpolatedTypes.isEmpty == false {
result += interpolatedTypes.functionDeclaration(withName: name, key: key, indentLevel: indentLevel)
} else {
result += #"\#(String.indent(indentLevel))public static var \#(name): String { return NSLocalizedString("\#(key)", bundle: bundle, comment: "") }"#
switch framework {
case .swiftui:
result += #"\#(String.indent(indentLevel))public static var \#(name): LocalizedStringKey { return .init(NSLocalizedString("\#(key)", bundle: bundle, comment: "")) }"#
default:
result += #"\#(String.indent(indentLevel))public static var \#(name): String { return NSLocalizedString("\#(key)", bundle: bundle, comment: "") }"#
}
}
}
return result
Expand Down Expand Up @@ -117,7 +122,7 @@ enum LocalizationBuilderError: LocalizedError {
}

enum LocalizationEnumBuilder {
static func localizationsEnumString(forFilesAtPaths paths: [String], separator: Character, topLevelName: String) throws -> String? {
static func localizationsEnumString(forFilesAtPaths paths: [String], topLevelName: String, options: Options) throws -> String? {
let termsDictionaries = try paths.compactMap({ path -> [String: String]? in
guard FileManager.default.fileExists(atPath: path) else { return nil }
guard let termsDictionary = NSDictionary(contentsOfFile: path) as? [String: String] else {
Expand All @@ -132,7 +137,7 @@ enum LocalizationEnumBuilder {

for termsDictionary in termsDictionaries {
for (name, value) in termsDictionary {
var parts = name.split(separator: separator)
var parts = name.split(separator: options.separator)

guard parts.isEmpty == false else { continue }

Expand All @@ -146,17 +151,17 @@ enum LocalizationEnumBuilder {

rootNode.sort()
rootNode.sanitize()
let result = try localizationEnumString(for: rootNode)
let result = try localizationEnumString(for: rootNode, framework: options.framework)
return result
}

private static func localizationEnumString(for node: Node<LocalizationValue>, indentLevel: Int = 0) throws -> String {
private static func localizationEnumString(for node: Node<LocalizationValue>, indentLevel: Int = 0, framework: Framework) throws -> String {
switch node.value {
case .namespace:
let childrenString = try node.children.map { try localizationEnumString(for: $0, indentLevel: indentLevel + 1) }
return try node.value.declaration(withBody: childrenString.joined(separator: "\n\n"), indentLevel: indentLevel)
let childrenString = try node.children.map { try localizationEnumString(for: $0, indentLevel: indentLevel + 1, framework: framework) }
return try node.value.declaration(withBody: childrenString.joined(separator: "\n\n"), indentLevel: indentLevel, framework: framework)
case .localization:
return try node.value.declaration(indentLevel: indentLevel)
return try node.value.declaration(indentLevel: indentLevel, framework: framework)
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions Sources/Shark/SharkEnumBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ enum SharkEnumBuilder {

let imagesString = try ImageEnumBuilder.imageEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "I", options: options)
let colorsString = try ColorEnumBuilder.colorEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "C", options: options)
let localizationsString = try LocalizationEnumBuilder.localizationsEnumString(
forFilesAtPaths: resourcePaths.localizationPaths,
separator: options.separator,
topLevelName: "L"
)
let localizationsString = try LocalizationEnumBuilder.localizationsEnumString(forFilesAtPaths: resourcePaths.localizationPaths, topLevelName: "L", options: options)
let fontsString = try FontEnumBuilder.fontsEnumString(forFilesAtPaths: resourcePaths.fontPaths, topLevelName: "F", options: options)
let dataAssetsString = try DataAssetEnumBuilder.dataAssetEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "D")
let storyboardString = try StoryboardBuilder.storyboardEnumString(forFilesAtPaths: resourcePaths.storyboardPaths, topLevelName: "S", options: options)
Expand Down

0 comments on commit ef40408

Please sign in to comment.