Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions Sources/TranslationCatalogIO/KeyHierarchy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,33 @@ public struct KeyHierarchy {
self.nodes = nodes
}

public static func make(with expressions: [TranslationCatalog.Expression]) throws -> KeyHierarchy {
public static func make(with localizationKeys: [LocalizationKey]) throws -> KeyHierarchy {
var hierarchy = KeyHierarchy()

try expressions
try localizationKeys.forEach { key in
let id = key.key
.components(separatedBy: String(key.strategy.separator))
.map {
[$0]
}

try hierarchy.processKey(key, path: id)
}

return hierarchy
}

public static func make(with expressions: [TranslationCatalog.Expression]) throws -> KeyHierarchy {
let localizationKeys = try expressions
.map {
try LocalizationKey(
key: $0.key,
defaultValue: $0.defaultValue.encodingDarwinStrings(),
comment: $0.context
)
}
.forEach { key in
let id = key.key
.components(separatedBy: String(key.strategy.separator))
.map {
[$0]
}

try hierarchy.processKey(key, path: id)
}

return hierarchy
return try make(with: localizationKeys)
}

mutating func processKey(_ key: LocalizationKey, path: KeyNodePath) throws {
Expand All @@ -116,9 +121,10 @@ public struct KeyHierarchy {
}

guard !components.isEmpty else {
if let token = Self.reservedTypeTokens.first(where: { $0.caseInsensitiveCompare(component) == .orderedSame }) {
throw HierarchyError.reservedTypeToken(token)
}
// Since this is the last token, it will become a `enum case`; No need to block here?!
// if let token = Self.reservedTypeTokens.first(where: { $0.caseInsensitiveCompare(component) == .orderedSame }) {
// throw HierarchyError.reservedTypeToken(token)
// }

if Int(component) != nil {
throw HierarchyError.reservedType(Int.self)
Expand Down
58 changes: 20 additions & 38 deletions Tests/TranslationCatalogTests/KeyHierarchy2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,50 @@ import XCTest

final class KeyHierarchy2Tests: XCTestCase {

let expressions: [TranslationCatalog.Expression] = [
Expression(
id: UUID(uuidString: "0681BB4E-A63F-4E3C-AA15-5E152844B7EC")!,
let keys: [LocalizationKey] = [
LocalizationKey(
key: "PAYMENT_METHOD_ADD_ACTION",
value: "Add payment method",
languageCode: .english
defaultValue: "Add payment method"
),
Expression(
id: UUID(uuidString: "B7FA5A95-2FE5-4887-8619-566CDA83F23E")!,
LocalizationKey(
key: "PAYMENT_METHOD_ADD_CARD_FAILURE_MESSAGE",
value: "Your payment method could not be added at this time.",
languageCode: .english
defaultValue: "Your payment method could not be added at this time."
),
Expression(
id: UUID(uuidString: "76C37D84-8AF3-4011-88B1-BCC80B2D6C6A")!,
LocalizationKey(
key: "PAYMENT_METHOD_ADD_CARD_SUCCESS_MESSAGE",
value: "Your payment method has been added.",
languageCode: .english
defaultValue: "Your payment method has been added."
),
Expression(
id: UUID(uuidString: "52B6650F-F041-4CA3-ACE6-38E7930D026D")!,
LocalizationKey(
key: "PAYMENT_METHOD_CONFIRM_DELETE",
value: "Are you sure you want to delete this payment method?",
languageCode: .english
defaultValue: "Are you sure you want to delete this payment method?"
),
Expression(
id: UUID(uuidString: "943D6EF9-A37A-4BB6-8C93-05FECAD1DA8C")!,
LocalizationKey(
key: "PAYMENT_METHOD_EXPIRATION_ABBREVIATION",
value: "Exp",
languageCode: .english
defaultValue: "Exp"
),
Expression(
id: UUID(uuidString: "95C73B79-8111-43E7-85AC-EF4C74586594")!,
LocalizationKey(
key: "PAYMENT_METHOD_NAVIGATION_TITLE",
value: "Manage Payments",
languageCode: .english
defaultValue: "Manage Payments"
),
Expression(
id: UUID(uuidString: "E5792937-3C63-4C10-8B58-0E405E76AB56")!,
LocalizationKey(
key: "PAYMENT_METHOD_REMOVE_CARD_FAILURE_MESSAGE",
value: "Your payment method could not be deleted at this time.",
languageCode: .english
defaultValue: "Your payment method could not be deleted at this time."
),
Expression(
id: UUID(uuidString: "185CA880-88DB-4CA6-98A4-FD544B908900")!,
LocalizationKey(
key: "PAYMENT_METHOD_REMOVE_CARD_SUCCESS_MESSAGE",
value: "Your payment method has been deleted.",
languageCode: .english
defaultValue: "Your payment method has been deleted."
),
Expression(
id: UUID(uuidString: "9E141C73-8D8C-40B6-AF56-AFD94C230752")!,
LocalizationKey(
key: "PAYMENT_METHOD_SECTION_TITLE",
value: "Payment Methods",
languageCode: .english
defaultValue: "Payment Methods"
),
]

var hierarchy: KeyHierarchy!

override func setUpWithError() throws {
try super.setUpWithError()
hierarchy = try KeyHierarchy.make(with: expressions)
hierarchy = try KeyHierarchy.make(with: keys)
}

func testHierarchyGeneration() throws {
Expand Down
52 changes: 18 additions & 34 deletions Tests/TranslationCatalogTests/KeyHierarchyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,46 @@ import XCTest

final class KeyHierarchyTests: XCTestCase {

let expressions: [TranslationCatalog.Expression] = [
Expression(
id: UUID(uuidString: "0681BB4E-A63F-4E3C-AA15-5E152844B7EC")!,
let keys: [LocalizationKey] = [
LocalizationKey(
key: "GREETING",
value: "Hello World!",
languageCode: .english
defaultValue: "Hello World!"
),
Expression(
id: UUID(uuidString: "B7FA5A95-2FE5-4887-8619-566CDA83F23E")!,
LocalizationKey(
key: "APPLICATION_NAME",
value: "Lingua",
languageCode: .english
defaultValue: "Lingua"
),
Expression(
id: UUID(uuidString: "76C37D84-8AF3-4011-88B1-BCC80B2D6C6A")!,
LocalizationKey(
key: "HIDDEN_MESSAGE",
value: "",
languageCode: .english
defaultValue: ""
),
Expression(
id: UUID(uuidString: "52B6650F-F041-4CA3-ACE6-38E7930D026D")!,
LocalizationKey(
key: "PLATFORM_ANDROID",
value: "Android",
languageCode: .english
defaultValue: "Android"
),
Expression(
id: UUID(uuidString: "943D6EF9-A37A-4BB6-8C93-05FECAD1DA8C")!,
LocalizationKey(
key: "PLATFORM_APPLE",
value: "Apple",
languageCode: .english
defaultValue: "Apple"
),
Expression(
id: UUID(uuidString: "95C73B79-8111-43E7-85AC-EF4C74586594")!,
LocalizationKey(
key: "PLATFORM_APPLE_MAC",
value: "macOS",
languageCode: .english
defaultValue: "macOS"
),
Expression(
id: UUID(uuidString: "E5792937-3C63-4C10-8B58-0E405E76AB56")!,
LocalizationKey(
key: "PLATFORM_WEB",
value: "Web",
languageCode: .english
defaultValue: "Web"
),
Expression(
id: UUID(uuidString: "185CA880-88DB-4CA6-98A4-FD544B908900")!,
LocalizationKey(
key: "ZULU_TIME_DEFINITION",
value: "definition",
languageCode: .english
defaultValue: "definition"
),
]

var hierarchy: KeyHierarchy!

override func setUpWithError() throws {
try super.setUpWithError()
hierarchy = try KeyHierarchy.make(with: expressions)
hierarchy = try KeyHierarchy.make(with: keys)
}

func testHierarchyGeneration() throws {
Expand Down