Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Use hyphens for the whole namespace, but normalized names for everyth…
Browse files Browse the repository at this point in the history
…ing else.
  • Loading branch information
bclymer committed Jun 19, 2019
1 parent 00c30d1 commit fc95294
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CocoaLoco.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CocoaLoco'
s.version = '1.0.8'
s.version = '1.0.9'
s.summary = 'A Swift command line tool to help you make sense of large quantities of localizable strings.'
s.description = <<-DESC
A Swift command line tool to help you make sense of large quantities of localizable strings.
Expand Down
2 changes: 1 addition & 1 deletion Sources/CocoaLocoCore/CocoaLocoCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct CocoaLocoCore {
let initialName: String = "\(namePrefix)\(defaultName)"
let namespace = LocalizationNamespace.parseValue(jsonResult,
namespace: nil,
normalizedName: initialName,
name: initialName,
prefix: namePrefix)
let swiftOutputFile = SwiftOutputFile(namespace: namespace)
let baseStringsDictFile = StringsDictOutputFile(namespace: namespace)
Expand Down
21 changes: 16 additions & 5 deletions Sources/CocoaLocoCore/LocalizationNamespace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,42 @@ import Foundation

struct LocalizationNamespace: CodeGeneratable {

let name: String
let normalizedName: String
let namespaces: [LocalizationNamespace]
let strings: [LocalizedString]
let plurals: [Plural]

init(name: String,
namespaces: [LocalizationNamespace],
strings: [LocalizedString],
plurals: [Plural]) {
self.name = name
self.normalizedName = normalizeName(rawName: name)
self.namespaces = namespaces
self.strings = strings
self.plurals = plurals
}

static func parseValue(_ dict: [String: Any], namespace: String?, normalizedName: String, prefix: String) -> LocalizationNamespace {
static func parseValue(_ dict: [String: Any], namespace: String?, name: String, prefix: String) -> LocalizationNamespace {
var namespaces = [LocalizationNamespace]()
var strings = [LocalizedString]()
var plurals = [Plural]()

dict.forEach { key, value in
let normalizedName = normalizeName(rawName: key)
if let plural = Plural.asPlural(value, key: key, namespace: namespace, prefix: prefix) {
plurals.append(plural)
} else if let string = LocalizedString.asLocalizedString(key: key, namespace: namespace, prefix: prefix, value: value) {
strings.append(string)
} else if let dictValue = value as? [String: Any] {
let nextNamespace = joinedNamespace(part1: namespace, part2: normalizedName)
namespaces.append(parseValue(dictValue, namespace: nextNamespace, normalizedName: normalizedName, prefix: prefix))
let nextNamespace = joinedNamespace(part1: namespace, part2: key)
namespaces.append(parseValue(dictValue, namespace: nextNamespace, name: key, prefix: prefix))
} else {
fatalError("RIP")
}
}

return LocalizationNamespace(normalizedName: normalizedName, namespaces: namespaces, strings: strings, plurals: plurals)
return LocalizationNamespace(name: name, namespaces: namespaces, strings: strings, plurals: plurals)
}

func toSwiftCode(indent: Int, visibility: Visibility) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CocoaLocoCore/LocalizedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct LocalizedString: CodeGeneratable {
self.arguments = arguments
self.normalizedName = normalizeName(rawName: key)
self.swiftKey = joinedNamespace(part1: namespace, part2: key)
self.swiftFullFunc = joinedNamespace(part1: namespace, part2: normalizedName)
self.swiftFullFunc = joinedNamespace(part1: normalizeName(rawName: namespace ?? ""), part2: normalizedName)
}

func toSwiftCode(visibility: Visibility, swiftEnum: LocalizationNamespace) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CocoaLocoCore/NormalizedName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func normalizeName(rawName: String) -> String {
/// - part2: The child namespace string.
/// - Returns: The full namespace combining the two provided parts.
func joinedNamespace(part1: String?, part2: String) -> String {
if let part1 = part1 {
if let part1 = part1, !part1.isEmpty {
return "\(part1).\(part2)"
} else {
return part2
Expand Down
2 changes: 1 addition & 1 deletion Sources/CocoaLocoCore/SwiftOutputFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SwiftOutputFile {
@objcMembers
@available(swift, obsoleted: 1.0, message: "Use \(prefix)LocalizableStrings instead")
\(visibility.rawValue) class \(prefix)ObjCLocalizableStrings: Foundation.NSObject {
\(namespace.toObjcCode(visibility: visibility, baseName: namespace.normalizedName))
\(namespace.toObjcCode(visibility: visibility, baseName: namespace.name))
}
"""
Expand Down
5 changes: 5 additions & 0 deletions Tests/CocoaLocoTests/JoinedNamespaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ class JoinedNamespaceTests: XCTestCase {
XCTAssertEqual(result, "Part1.Tester")
}

func testParentEmpty() {
let result = joinedNamespace(part1: "", part2: "Tester")
XCTAssertEqual(result, "Tester")
}

}
12 changes: 6 additions & 6 deletions Tests/CocoaLocoTests/LocalizationNamespaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LocalizationNamespaceTests: XCTestCase {
"other": "%lu clips"
]
]
let result = LocalizationNamespace.parseValue(dict, namespace: "namespace", normalizedName: "name", prefix: "")
let result = LocalizationNamespace.parseValue(dict, namespace: "namespace", name: "name", prefix: "")
XCTAssertEqual(result.plurals.count, 1)
XCTAssertEqual(result.namespaces.count, 0)
XCTAssertEqual(result.strings.count, 0)
Expand All @@ -33,7 +33,7 @@ class LocalizationNamespaceTests: XCTestCase {
"value": "yay this is nice"
]
]
let result = LocalizationNamespace.parseValue(dict, namespace: "namespace", normalizedName: "name", prefix: "")
let result = LocalizationNamespace.parseValue(dict, namespace: "namespace", name: "name", prefix: "")
XCTAssertEqual(result.plurals.count, 0)
XCTAssertEqual(result.namespaces.count, 0)
XCTAssertEqual(result.strings.count, 1)
Expand All @@ -49,7 +49,7 @@ class LocalizationNamespaceTests: XCTestCase {
]
]
]
let result = LocalizationNamespace.parseValue(dict, namespace: "namespace", normalizedName: "name", prefix: "")
let result = LocalizationNamespace.parseValue(dict, namespace: "namespace", name: "name", prefix: "")
XCTAssertEqual(result.plurals.count, 0)
XCTAssertEqual(result.namespaces.count, 1)
XCTAssertEqual(result.namespaces.first?.normalizedName, "someNamespace")
Expand All @@ -63,7 +63,7 @@ class LocalizationNamespaceTests: XCTestCase {
"value": "yay this is nice"
]
]
let result = LocalizationNamespace.parseValue(dict, namespace: nil, normalizedName: "name", prefix: "")
let result = LocalizationNamespace.parseValue(dict, namespace: nil, name: "name", prefix: "")
XCTAssertEqual(result.strings.first?.swiftKey, "someString")
XCTAssertEqual(result.strings.first?.swiftFullFunc, "someString")
}
Expand Down Expand Up @@ -144,7 +144,7 @@ internal enum testName {
}

private let exampleNestedNamespace = [
LocalizationNamespace(normalizedName: "nested", namespaces: [], strings: [], plurals: [])
LocalizationNamespace(name: "nested", namespaces: [], strings: [], plurals: [])
]
private let examplePlurals = [
Plural(key: "one", namespace: "oneName", prefix: "", comment: nil, other: "%lu clips", one: "1 clip", zero: nil, two: nil, few: nil, many: nil)!,
Expand All @@ -154,7 +154,7 @@ private let exampleStrings = [
LocalizedString(key: "one", namespace: "oneName", value: "test1", prefix: "", comment: nil, arguments: []),
LocalizedString(key: "two", namespace: "twoName", value: "test2", prefix: "", comment: nil, arguments: [])
]
private let exampleNamespace = LocalizationNamespace(normalizedName: "testName",
private let exampleNamespace = LocalizationNamespace(name: "testName",
namespaces: exampleNestedNamespace,
strings: exampleStrings,
plurals: examplePlurals)
22 changes: 19 additions & 3 deletions Tests/CocoaLocoTests/LocalizedStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ private static let _name = Foundation.NSLocalizedString("Namespace.name", bundle
XCTAssertEqual(string.toSwiftCode(visibility: .internal, swiftEnum: testNamespace), """
internal static func name() -> String { return bigName._name }
private static let _name = Foundation.NSLocalizedString("Namespace.name", bundle: __bundle, value: "Test", comment: "MyComment")
""")
}

func testSwiftNamespaceNormalized() {
let string = LocalizedString(key: "weird-name_with_stuff", namespace: "weird.namespace_with.stuff", value: "Test", prefix: "", comment: nil, arguments: [])
XCTAssertEqual(string.toSwiftCode(visibility: .internal, swiftEnum: testNamespace), """
internal static func weirdName_with_stuff() -> String { return bigName._weirdName_with_stuff }
private static let _weirdName_with_stuff = Foundation.NSLocalizedString("weird.namespace_with.stuff.weird-name_with_stuff", bundle: __bundle, value: "Test", comment: "")
""")
}

Expand Down Expand Up @@ -115,6 +123,14 @@ private static let _name = Foundation.NSLocalizedString("Namespace.name", bundle
XCTAssertEqual(string.toSwiftCode(visibility: .public, swiftEnum: testNamespace), """
public static func name() -> String { return bigName._name }
private static let _name = Foundation.NSLocalizedString("Namespace.name", tableName: "TableNameLocalizable", bundle: __bundle, value: "Test", comment: "MyComment")
""")
}

func testSwiftWithHyphensInKey() {
let string = LocalizedString(key: "name-blah-test", namespace: "Namespace-test-yay", value: "Test", prefix: "TableName", comment: "MyComment", arguments: [])
XCTAssertEqual(string.toSwiftCode(visibility: .public, swiftEnum: testNamespace), """
public static func nameBlahTest() -> String { return bigName._nameBlahTest }
private static let _nameBlahTest = Foundation.NSLocalizedString("Namespace-test-yay.name-blah-test", tableName: "TableNameLocalizable", bundle: __bundle, value: "Test", comment: "MyComment")
""")
}

Expand Down Expand Up @@ -146,12 +162,12 @@ public static func Namespace_Name() -> String { return bigName.Namespace.name()
}

func testObjcNamespaceNormalized() {
let string = LocalizedString(key: "name", namespace: "weird.namespace_with.stuff", value: "Test", prefix: "", comment: nil, arguments: [])
let string = LocalizedString(key: "name-hyphen", namespace: "weird.namespace_with.stuff-hyphen", value: "Test", prefix: "", comment: nil, arguments: [])
XCTAssertEqual(string.toObjcCode(visibility: .internal, baseName: "bigName"), """
internal static func Weird_Namespace_with_Stuff_Name() -> String { return bigName.weird.namespace_with.stuff.name() }
internal static func Weird_Namespace_with_StuffHyphen_NameHyphen() -> String { return bigName.weird.namespace_with.stuffHyphen.nameHyphen() }
""")
}

}

private let testNamespace = LocalizationNamespace(normalizedName: "bigName", namespaces: [], strings: [], plurals: [])
private let testNamespace = LocalizationNamespace(name: "big-name", namespaces: [], strings: [], plurals: [])
2 changes: 1 addition & 1 deletion Tests/CocoaLocoTests/StringsDictOutputFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class StringsDictOutputFileTests: XCTestCase {
private let examplePlurals = [
Plural(key: "one", namespace: "oneName", prefix: "", comment: nil, other: "%lu clips", one: "1 clip", zero: nil, two: nil, few: nil, many: nil)!
]
private let exampleNamespace = LocalizationNamespace(normalizedName: "testName",
private let exampleNamespace = LocalizationNamespace(name: "testName",
namespaces: [],
strings: [],
plurals: examplePlurals)
2 changes: 1 addition & 1 deletion Tests/CocoaLocoTests/SwiftOutputFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private class BundleReference {}

}

private let exampleNamespace = LocalizationNamespace(normalizedName: "testName",
private let exampleNamespace = LocalizationNamespace(name: "testName",
namespaces: [],
strings: [],
plurals: [])

0 comments on commit fc95294

Please sign in to comment.