Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Plurals in XCStrings #44

Merged
merged 3 commits into from
Mar 4, 2024
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
113 changes: 106 additions & 7 deletions Sources/swift-bundler/Bundler/StringCatalogCompiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum StringCatalogCompiler {
/// A state for a string unit to be in.
enum StringUnitState: String, Decodable {
case translated = "translated"
case needsReview = "needs-review"
case needsReview = "needs_review"
}

/// A string unit containg a value and a state.
Expand Down Expand Up @@ -58,7 +58,7 @@ enum StringCatalogCompiler {
/// A translation unit in a string catalog.
struct TranslationUnit: Decodable {
let comment: String?
let localizations: [String: StringOutlet]
let localizations: [String: StringOutlet]?
}

/// The root of a `.xcstrings` file.
Expand Down Expand Up @@ -92,13 +92,15 @@ enum StringCatalogCompiler {
/// A plural variation in a strings file.
struct StringsFilePluralVariation: Encodable {
let spec = "NSStringPluralRuleType"
let formatValueType: String?
let other: String
let zero: String?
let one: String?

// Coding keys for the plural variations.
private enum CodingKeys: String, CodingKey {
case spec = "NSStringFormatSpecTypeKey"
case formatValueType = "NSStringFormatValueTypeKey"
case other
case zero
case one
Expand Down Expand Up @@ -220,7 +222,11 @@ enum StringCatalogCompiler {
}
}

let (stringsFile, stringsDictFile) = generateStringsFile(from: data, locale: locale)
let data = generateStringsFile(url: file, from: data, locale: locale)

guard case let .success((stringsFile, stringsDictFile)) = data else {
return data.eraseSuccessValue()
}

// The paths for the strings and strings dict files.
let stringsFileURL = lprojDirectory
Expand Down Expand Up @@ -281,37 +287,130 @@ enum StringCatalogCompiler {
// Loop through all the strings and get the keys.
var locales: [String] = []
for (_, translationUnit) in files.strings {
for (locale, _) in translationUnit.localizations {
guard let localizations = translationUnit.localizations else {
continue
}

for (locale, _) in localizations {
locales.append(locale)
}
}

return locales
}

// Gets the regex to match format value types.
/// - Returns: The regex to match format value types.
private static func getFormatValueTypeRegex() -> Result<NSRegularExpression, StringCatalogCompilerError> {
// The regex to match format value types.
do {
return .success(try NSRegularExpression(pattern: "%([0-9]+\\$)?[0 #+-]?[0-9*]*\\.?\\d*[hl]{0,2}[jztL]?[dDiuUxXoOeEfgGaAcCsSpF]", options: []))
} catch {
return .failure(.failedToCreateFormatStringRegex(error))
}
}

/// Selects and returns the order of format specifiers in a string.
/// - Parameter
/// - fileURL: The file URL to select the order of format specifiers from.
/// - string: The string to select the order of format specifiers from.
/// - Returns: The order of format specifiers in the string.
private static func selectFormatSpecifierOrder(
fileURL: URL,
from string: String
) -> Result<[Int: (String, String)], StringCatalogCompilerError> {
// Initialize the format specifier regex.
let regex = getFormatValueTypeRegex()
guard case let .success(regex) = regex else {
return .failure(regex.failure ?? .failedToCreateFormatStringRegex(NSError()))
}

// Get the format specifiers.
let formatSpecifiers = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.count))
.map { (string as NSString).substring(with: $0.range) }

// Create a dictionary with the order of the format specifiers.
var currentOrder = 1

var formatSpecifierOrder = [Int: (String, String)]()

for formatSpecifier in formatSpecifiers {
// Trim the first character (%)
let formatSpecifierStrip = String(formatSpecifier.dropFirst())
// The last character is the format specifier type.
let formatSpecifierType = String(formatSpecifierStrip[formatSpecifierStrip.index(before: formatSpecifierStrip.endIndex)])
// If there is an $, get the number before it.
let formatSpecifierOrderSplit = formatSpecifierStrip.split(separator: "$")
let intBeforeDollar = formatSpecifierOrderSplit.count > 1 ? Int(formatSpecifierOrderSplit[0]) : nil

// If there is a number before the $, use it as the order.
if let intBeforeDollar = intBeforeDollar {
if formatSpecifierOrder[intBeforeDollar] == nil {
formatSpecifierOrder[intBeforeDollar] = (String(formatSpecifierType), formatSpecifier)
} else if formatSpecifierOrder[intBeforeDollar]?.0 != String(formatSpecifierType) {
return .failure(.invalidNonMatchingFormatString(URL(fileURLWithPath: ""), string))
}
} else {
// If there is no number before the $, use the current order.
if formatSpecifierOrder[currentOrder] == nil {
formatSpecifierOrder[currentOrder] = (String(formatSpecifierType), formatSpecifier)
} else if formatSpecifierOrder[currentOrder]?.0 != String(formatSpecifierType) {
print(formatSpecifierOrder[currentOrder] ?? "nil")
return .failure(.invalidNonMatchingFormatString(URL(fileURLWithPath: ""), string))
}
currentOrder += 1
}
}

return .success(formatSpecifierOrder)
}

/// Generate a strings file from a String Catalog file.
/// - Parameters:
/// - fileURL: The URL of the string catalog file.
/// - data: String Catalog file data.
/// - locale: The locale to generate the strings file for.
/// - Returns: The strings file and the strings dict file.
private static func generateStringsFile(from data: StringsCatalogFile, locale: String) -> ([String: String], [String: StringDictionaryItem]) {
private static func generateStringsFile(
url fileURL: URL,
from data: StringsCatalogFile,
locale: String
) -> Result<([String: String], [String: StringDictionaryItem]), StringCatalogCompilerError> {
// Loop through the strings and generate the strings file.
var stringsFile = [String: String]()
var stringsDictFile = [String: StringDictionaryItem]()

for (key, translationUnit) in data.strings {
// Get the translation for the locale.
let translation = translationUnit.localizations[locale] ?? translationUnit.localizations[data.sourceLanguage]
let translation = translationUnit.localizations?[locale] ?? translationUnit.localizations?[data.sourceLanguage]

if let translation = translation {
// Match the translation to the correct outlet.
switch translation {
case .unit(let unit):
stringsFile[key] = unit.value
case .variation(let variation):
// Get all the format value
let formatValueType = selectFormatSpecifierOrder(fileURL: fileURL, from: variation.plural.other.stringUnit.value)
guard case let .success(formatValueType) = formatValueType else {
return .failure(formatValueType.failure ?? .failedToCreateFormatStringRegex(NSError()))
}

// Get the format value type.
var formatSpecifer = ""
var formatSpeciferIndex: Double = .infinity
let acceptedFormatValueTypeForNumber = ["d", "D", "u", "U", "x", "X", "o", "O", "f", "e", "E", "g", "G", "a", "A", "F", "i"]
for (order, type) in formatValueType {
if Double(order) < formatSpeciferIndex && acceptedFormatValueTypeForNumber.contains(type.0) {
formatSpecifer = type.0
formatSpeciferIndex = Double(order)
}
}

// Add the other, zero, and one variations to the strings dict file.
stringsDictFile[key] = StringDictionaryItem(
value: StringsFilePluralVariation(
formatValueType: "\(Int(formatSpeciferIndex))$\(formatSpecifer)",
other: variation.plural.other.stringUnit.value,
zero: variation.plural.zero?.stringUnit.value,
one: variation.plural.one?.stringUnit.value
Expand All @@ -324,6 +423,6 @@ enum StringCatalogCompiler {
}
}

return (stringsFile, stringsDictFile)
return .success((stringsFile, stringsDictFile))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
/// An error returned by ``StringCatalogCompiler``.
enum StringCatalogCompilerError: LocalizedError {
case failedToCreateFormatStringRegex(Error)
case failedToEnumerateStringsCatalogs(URL, Error)
case failedToDeleteStringsCatalog(URL, Error)
case failedToCreateOutputDirectory(URL, Error)
Expand All @@ -10,9 +11,12 @@ enum StringCatalogCompilerError: LocalizedError {
case failedToEncodePlistStringsDictFile(URL, Error)
case failedToWriteStringsFile(URL, Error)
case failedToWriteStringsDictFile(URL, Error)
case invalidNonMatchingFormatString(URL, String)

var errorDescription: String? {
switch self {
case .failedToCreateFormatStringRegex(let error):
return "Failed to create format string regex with error '\(error)'"
case .failedToEnumerateStringsCatalogs(let directory, _):
return "Failed to enumerate strings catalogs in directory at '\(directory.relativePath)'"
case .failedToDeleteStringsCatalog(let file, _):
Expand All @@ -31,6 +35,8 @@ enum StringCatalogCompilerError: LocalizedError {
return "Failed to write strings file at '\(file.relativePath)'"
case .failedToWriteStringsDictFile(let file, _):
return "Failed to write strings dict file at '\(file.relativePath)'"
case .invalidNonMatchingFormatString(let file, let string):
return "Two or more format strings in the same string do not match in file '\(file.relativePath)' with string '\(string)'"
}
}
}
Loading