Skip to content
Closed
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
2 changes: 1 addition & 1 deletion R.swift.Library
55 changes: 41 additions & 14 deletions R.swift/Generators/StringsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ struct StringsGenerator: Generator {
// Maybe when we pick up this issue: https://github.com/mac-cain13/R.swift/issues/136
private static func computeParams(filename: String, strings: [LocalizableStrings]) -> [StringValues] {

var allParams: [String: [(Locale, String, [StringParam])]] = [:]
var allParams: [String: [(Locale, LocalizableStrings.Entry)]] = [:]
let baseKeys: Set<String>?
let bases = strings.filter { $0.locale.isBase }
if bases.isEmpty {
baseKeys = nil
}
else {
baseKeys = Set(bases.flatMap { $0.dictionary.keys })
baseKeys = Set(bases.flatMap { $0.keys })
}

// Warnings about duplicates and empties
for ls in strings {
let filenameLocale = ls.locale.withFilename(filename)
let groupedKeys = ls.dictionary.keys.groupBySwiftNames { $0 }
let groupedKeys = ls.keys.groupBySwiftNames { $0 }

for (sanitizedName, duplicates) in groupedKeys.duplicates {
warn("Skipping \(duplicates.count) strings in \(filenameLocale) because symbol '\(sanitizedName)' would be generated for all of these keys: \(duplicates.map { "'\($0)'" }.joinWithSeparator(", "))")
Expand All @@ -88,13 +88,17 @@ struct StringsGenerator: Generator {
}

// Save uniques
var byKey: [String: LocalizableStrings.Entry] = [:]
for entry in ls.entries {
byKey[entry.key] = entry
}
for key in groupedKeys.uniques {
if let (params, commentValue) = ls.dictionary[key] {
if let entry = byKey[key] {
if let _ = allParams[key] {
allParams[key]?.append((ls.locale, commentValue, params))
allParams[key]?.append((ls.locale, entry))
}
else {
allParams[key] = [(ls.locale, commentValue, params)]
allParams[key] = [(ls.locale, entry)]
}
}
}
Expand All @@ -105,7 +109,7 @@ struct StringsGenerator: Generator {
let filenameLocale = locale.withFilename(filename)
let sourceKeys = baseKeys ?? Set(allParams.keys)

let missing = sourceKeys.subtract(lss.flatMap { $0.dictionary.keys })
let missing = sourceKeys.subtract(lss.flatMap { $0.keys })

if missing.isEmpty {
continue
Expand Down Expand Up @@ -134,8 +138,8 @@ struct StringsGenerator: Generator {
var params: [StringParam] = []
var areCorrectFormatSpecifiers = true

for (locale, _, ps) in keyParams {
if ps.any({ $0.spec == FormatSpecifier.TopType }) {
for (locale, entry) in keyParams {
if entry.params.any({ $0.spec == FormatSpecifier.TopType }) {
let name = locale.withFilename(filename)
warn("Skipping string \(key) in \(name), not all format specifiers are consecutive")

Expand All @@ -145,8 +149,8 @@ struct StringsGenerator: Generator {

if !areCorrectFormatSpecifiers { continue }

for (_, _, ps) in keyParams {
if let unified = params.unify(ps) {
for (_, entry) in keyParams {
if let unified = params.unify(entry.params) {
params = unified
}
else {
Expand All @@ -158,8 +162,9 @@ struct StringsGenerator: Generator {

if !areCorrectFormatSpecifiers { continue }

let vals = keyParams.map { ($0.0, $0.1) }
let values = StringValues(key: key, params: params, tableName: filename, values: vals )
let vals = keyParams.map { locale, entry in (locale, entry.val) }
let comments = keyParams.map { locale, entry in (locale, entry.comment) }
let values = StringValues(key: key, params: params, tableName: filename, values: vals, entryComments: comments)
results.append(values)
}

Expand All @@ -182,13 +187,18 @@ struct StringsGenerator: Generator {
.flatMap { $0.localeDescription }
.map { "\"\($0)\"" }
.joinWithSeparator(", ")
let firstComment = values.entryComments
.first?
.1?
.escapedStringLiteral
let escapedComment = firstComment.map { "\"\($0)\"" } ?? "nil"

return Let(
comments: values.comments,
isStatic: true,
name: values.key,
typeDefinition: .Inferred(Type.StringResource),
value: "StringResource(key: \"\(escapedKey)\", tableName: \"\(values.tableName)\", locales: [\(locales)])"
value: "StringResource(key: \"\(escapedKey)\", tableName: \"\(values.tableName)\", locales: [\(locales)], comment: \(escapedComment))"
)
}

Expand Down Expand Up @@ -269,6 +279,7 @@ private struct StringValues {
let params: [StringParam]
let tableName: String
let values: [(Locale, String)]
let entryComments: [(Locale, String?)]

var localizedString: String {
let escapedKey = key.escapedStringLiteral
Expand Down Expand Up @@ -313,6 +324,22 @@ private struct StringValues {
let locales = values.flatMap { $0.0.localeDescription }
results.append("Locales: \(locales.joinWithSeparator(", "))")
}

let baseEntryComment = entryComments.filter { $0.0.isBase }.flatMap { $0.1 }.first
if let baseEntryComment = baseEntryComment {
if !results.isEmpty {
results.append("")
}

let lines = baseEntryComment.componentsSeparatedByString("\n")
if lines.count == 1 {
results.append("Comment: \(lines[0])")
} else {
results.append("Comment:")
let indented = lines.map { " \($0)" }
results.appendContentsOf(indented)
}
}

return results
}
Expand Down
Loading