Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #41 from SwiftGen/feature/strings-multiple-tables
Browse files Browse the repository at this point in the history
Multiple strings tables support
  • Loading branch information
djbe authored May 28, 2017
2 parents ae1d55f + 52dafe3 commit 91e3239
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Due to the removal of legacy code, there are a few breaking changes in this new
* Throw an error if a format string has mismatching types for the same placeholde position.
[David Jennes](https://github.com/djbe)
[#44](https://github.com/SwiftGen/SwiftGenKit/issues/44)
* Added support for multiple string tables.
[David Jennes](https://github.com/djbe)
[#41](https://github.com/SwiftGen/templates/issues/41)

### Internal Changes

Expand Down
22 changes: 12 additions & 10 deletions Sources/Parsers/StringsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import Foundation
import PathKit

public enum StringsFileParserError: Error, CustomStringConvertible {
case duplicateTable(name: String)
case failureOnLoading(path: String)
case invalidFormat
case invalidPlaceholder(previous: StringsFileParser.PlaceholderType, new: StringsFileParser.PlaceholderType)

public var description: String {
switch self {
case .duplicateTable(let name):
return "Table \"\(name)\" already loaded, cannot add it again"
case .failureOnLoading(let path):
return "Failed to load a file at \"\(path)\""
case .invalidFormat:
Expand All @@ -25,29 +28,28 @@ public enum StringsFileParserError: Error, CustomStringConvertible {
}

public final class StringsFileParser {
var entries = [Entry]()
var tables = [String: [Entry]]()

public init() {}

public func addEntry(_ entry: Entry) {
entries.append(entry)
}

// Localizable.strings files are generally UTF16, not UTF8!
public func parseFile(at path: Path) throws {
let name = path.lastComponentWithoutExtension

guard tables[name] == nil else {
throw StringsFileParserError.duplicateTable(name: name)
}
guard let data = try? path.read() else {
throw StringsFileParserError.failureOnLoading(path: path.string)
}

let plist = try PropertyListSerialization
.propertyList(from: data, format: nil)

let plist = try PropertyListSerialization.propertyList(from: data, format: nil)
guard let dict = plist as? [String: String] else {
throw StringsFileParserError.invalidFormat
}

for (key, translation) in dict {
addEntry(try Entry(key: key, translation: translation))
tables[name] = try dict.map { key, translation in
try Entry(key: key, translation: translation)
}
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/Stencil/StringsContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ private extension String {
*/
extension StringsFileParser {
public func stencilContext() -> [String: Any] {

let entryToStringMapper = { (entry: Entry, keyPath: [String]) -> [String: Any] in
let levelName = entry.keyStructure.last ?? ""

Expand All @@ -47,14 +46,15 @@ extension StringsFileParser {
return result
}

let structuredStrings = structure(
entries: entries,
usingMapper: entryToStringMapper
)
let tables: [[String: Any]] = [[
"name": "Localizable",
"levels": structuredStrings
]]
let tables = self.tables.map { name, entries in
return [
"name": name,
"levels": structure(
entries: entries,
usingMapper: entryToStringMapper
)
]
}

return [
"tables": tables
Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources
Submodule Resources updated 28 files
+1 −8 Contexts/Strings/empty.plist
+1 −1 Contexts/Strings/multiline.plist
+296 −0 Contexts/Strings/multiple.plist
+1 −1 Contexts/Strings/structuredonly.plist
+1 −1 Contexts/Strings/utf8.plist
+2 −2 Documentation/strings/flat-swift2.md
+2 −2 Documentation/strings/flat-swift3.md
+23 −48 Tests/Expected/Strings/flat-swift2-context-localizable-customname.swift
+22 −47 Tests/Expected/Strings/flat-swift2-context-localizable-no-comments.swift
+23 −48 Tests/Expected/Strings/flat-swift2-context-localizable.swift
+63 −0 Tests/Expected/Strings/flat-swift2-context-multiple.swift
+23 −48 Tests/Expected/Strings/flat-swift3-context-localizable-customname.swift
+22 −47 Tests/Expected/Strings/flat-swift3-context-localizable-no-comments.swift
+23 −48 Tests/Expected/Strings/flat-swift3-context-localizable.swift
+63 −0 Tests/Expected/Strings/flat-swift3-context-multiple.swift
+13 −13 Tests/Expected/Strings/structured-swift2-context-localizable-customname.swift
+13 −13 Tests/Expected/Strings/structured-swift2-context-localizable-no-comments.swift
+13 −13 Tests/Expected/Strings/structured-swift2-context-localizable.swift
+111 −0 Tests/Expected/Strings/structured-swift2-context-multiple.swift
+13 −13 Tests/Expected/Strings/structured-swift3-context-localizable-customname.swift
+13 −13 Tests/Expected/Strings/structured-swift3-context-localizable-no-comments.swift
+13 −13 Tests/Expected/Strings/structured-swift3-context-localizable.swift
+111 −0 Tests/Expected/Strings/structured-swift3-context-multiple.swift
+1 −1 Tests/TemplatesTests/StringsTests.swift
+32 −44 templates/strings/flat-swift2.stencil
+32 −44 templates/strings/flat-swift3.stencil
+16 −8 templates/strings/structured-swift2.stencil
+16 −8 templates/strings/structured-swift3.stencil
23 changes: 23 additions & 0 deletions Tests/SwiftGenKitTests/StringsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,27 @@ class StringsTests: XCTestCase {
let result = parser.stencilContext()
XCTDiffContexts(result, expected: "structuredonly.plist", sub: .strings)
}

func testMultipleFiles() throws {
let parser = StringsFileParser()
try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings))
try parser.parseFile(at: Fixtures.path(for: "LocMultiline.strings", sub: .strings))

let result = parser.stencilContext()
XCTDiffContexts(result, expected: "multiple.plist", sub: .strings)
}

func testMultipleFilesDuplicate() throws {
let parser = StringsFileParser()
try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings))

do {
try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings))
XCTFail("Code did parse file successfully while it was expected to fail for duplicate file")
} catch StringsFileParserError.duplicateTable {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
}
}
}

0 comments on commit 91e3239

Please sign in to comment.