Fix Int format crash + unify String localization (#1273)

* Use %d over %zi to prevent a possible crash

* Clean up / unify localization behavior

Before, we were doing double localization (Feeding a `LocalizedString` into a `.localizedStringWithFormat`).
This commit is contained in:
Bas Broek
2017-12-17 22:10:57 +01:00
committed by Ryan Nystrom
parent 20e1b6f6df
commit 377161aed0
12 changed files with 24 additions and 24 deletions

View File

@@ -146,10 +146,10 @@ final class IssueCommentDetailCell: UICollectionViewCell, ListBindable {
let editedByNonOwner = NSLocalizedString("Edited by %@", comment: "")
let editedByOwner = NSLocalizedString("Edited", comment: "")
let format = viewModel.login != editedLogin ? editedByNonOwner : editedByOwner
editedLabel.text = "\(Constants.Strings.bullet) " + String.localizedStringWithFormat(format, editedLogin)
editedLabel.text = "\(Constants.Strings.bullet) \(String(format: format, editedLogin))"
let detailFormat = NSLocalizedString("%@ edited this issue %@", comment: "")
editedLabel.detailText = String.localizedStringWithFormat(detailFormat, editedLogin, editedDate.agoString)
editedLabel.detailText = String(format: detailFormat, arguments: [editedLogin, editedDate.agoString])
} else {
editedLabel.isHidden = true
}

View File

@@ -363,7 +363,7 @@ DoubleTappableCellDelegate {
let alertTitle = NSLocalizedString("%@'s comment", comment: "Used in an action sheet title, eg. \"Basthomas's comment\".")
let alert = UIAlertController.configured(
title: .localizedStringWithFormat(alertTitle, login),
title: String(format: alertTitle, login),
preferredStyle: .actionSheet
)
alert.popoverPresentationController?.sourceView = sender

View File

@@ -24,19 +24,19 @@ func createReactionDetailText(model: ReactionViewModel) -> String {
return ""
case 1:
let format = NSLocalizedString("%@", comment: "")
return .localizedStringWithFormat(format, users[0])
return String(format: format, users[0])
case 2:
let format = NSLocalizedString("%@ and %@", comment: "")
return .localizedStringWithFormat(format, users[0], users[1])
return String(format: format, arguments: [users[0], users[1]])
default:
assert(users.count >= 3)
if model.count > 3 {
let difference = model.count - users.count
let format = NSLocalizedString("%@, %@, %@ and %d other(s)", comment: "")
return .localizedStringWithFormat(format, users[0], users[1], users[2], difference)
return String(format: format, arguments: [users[0], users[1], users[2], difference])
} else {
let format = NSLocalizedString("%@, %@ and %@", comment: "")
return .localizedStringWithFormat(format, users[0], users[1], users[2])
return String(format: format, arguments: [users[0], users[1], users[2]])
}
}
}

View File

@@ -25,8 +25,8 @@ ListSingleSectionControllerDelegate {
emptyErrorMessage: NSLocalizedString("Cannot load changes.", comment: ""),
dataSource: self
)
let titleFormat = NSLocalizedString("Files (%zi)", comment: "")
title = String.localizedStringWithFormat(titleFormat, fileCount)
let titleFormat = NSLocalizedString("Files (%d)", comment: "")
title = String(format: titleFormat, fileCount)
}
required init?(coder aDecoder: NSCoder) {

View File

@@ -34,9 +34,9 @@ final class IssueViewFilesCell: UICollectionViewCell {
func configure(changes: FileChanges) {
let attributedText = NSMutableAttributedString()
let actionFormat = NSLocalizedString("View Files (%zi) ", comment: "")
let actionFormat = NSLocalizedString("View Files (%d) ", comment: "")
attributedText.append(NSAttributedString(
string: .localizedStringWithFormat(actionFormat, changes.changedFiles),
string: String(format: actionFormat, changes.changedFiles),
attributes: [
.font: Styles.Fonts.secondary,
.foregroundColor: Styles.Colors.Blue.medium.color

View File

@@ -109,7 +109,7 @@ FlatCacheListener {
makeBackBarItemEmpty()
let labelFormat = NSLocalizedString("#%zi in repository %@ by %@", comment: "Accessibility label for an issue/pull request navigation item")
let labelFormat = NSLocalizedString("#%d in repository %@ by %@", comment: "Accessibility label for an issue/pull request navigation item")
let labelString = String(format: labelFormat, arguments: [model.number, model.repo, model.owner])
navigationItem.configure(title: "#\(model.number)", subtitle: "\(model.owner)/\(model.repo)", accessibilityLabel: labelString)

View File

@@ -26,7 +26,7 @@ final class MilestoneCell: UITableViewCell {
self.titleLabel.text = title
if let date = date {
let format = NSLocalizedString("Due by %@", comment: "")
dueDateLabel.text = .localizedStringWithFormat(format, MilestoneCell.dateFormatter.string(from: date))
dueDateLabel.text = String(format: format, MilestoneCell.dateFormatter.string(from: date))
} else {
dueDateLabel.text = NSLocalizedString("No due date", comment: "")
}

View File

@@ -84,7 +84,7 @@ final class RepositorySummaryCell: SelectableCell {
titleView.configureAndSizeToFit(text: model.title, width: contentView.bounds.width)
let format = NSLocalizedString("#%d opened %@ by %@", comment: "")
secondaryLabel.text = String.localizedStringWithFormat(format, model.number, model.created.agoString, model.author)
secondaryLabel.text = String(format: format, arguments: [model.number, model.created.agoString, model.author])
let imageName: String
let tint: UIColor

View File

@@ -25,7 +25,7 @@ enum Signature {
static func signed(text: String) -> String {
guard enabled else { return text }
let format = NSLocalizedString("Sent with %@", comment: "")
let signature = String.localizedStringWithFormat(format, "<a href=\"http://githawk.com\">GitHawk</a>")
let signature = String(format: format, "<a href=\"http://githawk.com\">GitHawk</a>")
return text + "\n\n<sub>\(signature)</sub>"
}

View File

@@ -46,14 +46,14 @@ struct AlertAction {
}
func view(client: GithubClient, repo: RepositoryDetails) -> UIAlertAction {
return UIAlertAction(title: String.localizedStringWithFormat("View %@", repo.name), style: .default) { _ in
return UIAlertAction(title: .localizedStringWithFormat("View %@", repo.name), style: .default) { _ in
let repoViewController = RepositoryViewController(client: client, repo: repo)
self.rootViewController?.show(repoViewController, sender: nil)
}
}
func view(owner: String, url: URL) -> UIAlertAction {
return UIAlertAction(title: String.localizedStringWithFormat("View @%@", owner), style: .default) { _ in
return UIAlertAction(title: .localizedStringWithFormat("View @%@", owner), style: .default) { _ in
self.rootViewController?.presentSafari(url: url)
}
}

View File

@@ -19,10 +19,10 @@ extension Bundle {
}
var prettyVersionString: String {
let ver = versionNumber ?? Constants.Strings.unknown
let version = versionNumber ?? Constants.Strings.unknown
let build = buildNumber ?? "0"
let format = NSLocalizedString("Version %@ (%@)", comment: "")
return String.localizedStringWithFormat(format, ver, build)
return String(format: format, arguments: [version, build])
}
}

View File

@@ -55,19 +55,19 @@ extension Date {
case .seconds: return NSLocalizedString("just now", comment: "")
case .minutes(let t):
let format = NSLocalizedString("%d minute(s) ago", comment: "")
return String.localizedStringWithFormat(format, t)
return String(format: format, t)
case .hours(let t):
let format = NSLocalizedString("%d hour(s) ago", comment: "")
return String.localizedStringWithFormat(format, t)
return String(format: format, t)
case .days(let t):
let format = NSLocalizedString("%d day(s) ago", comment: "")
return String.localizedStringWithFormat(format, t)
return String(format: format, t)
case .months(let t):
let format = NSLocalizedString("%d month(s) ago", comment: "")
return String.localizedStringWithFormat(format, t)
return String(format: format, t)
case .years(let t):
let format = NSLocalizedString("%d year(s) ago", comment: "")
return String.localizedStringWithFormat(format, t)
return String(format: format, t)
}
}