import Foundation import Testing @testable import ParcelDev struct LocalizationToolsTests { @Test(arguments: [ "Hello world", "%@ is %@", "%1$@ · %2$lld", "Progress: %3$*1$.*2$f%%", "%#@windows@ in %2$@", "%1$#@count@ %@", "%ld %lu %zu %hhd %llx %g %e %p", "**Keep** [this](https://example.org)\nCafé, 日本語 👋", ]) func pseudoTextKeepsAllFormattingTokens(_ original: String) throws { let pseudo = try LocalizationTools.pseudoText(original) let originalTokens = try tokens(original) #expect(try tokens(pseudo) == originalTokens) #expect(pseudo.hasPrefix("⟦") && pseudo.hasSuffix("⟧")) #expect(pseudo != original) } @Test func pseudoTextExpandsLiteralCopyByThirtyFivePercent() throws { let original = "A longer explanation that needs room in the interface." let pseudo = try LocalizationTools.pseudoText(original) #expect(pseudo.count == original.count + Int(ceil(Double(original.count) * 0.35))) #expect(pseudo.contains("ľóñğéŕ")) #expect(try LocalizationTools.pseudoText("") == "") } @Test func catalogPreservesPluralSubstitutionsAndMetadata() throws { let catalog = try DevJSON.decode( Data( #""" {"sourceLanguage":"en","version":"1.0","strings":{ "summary":{"comment":"Context","localizations":{"en":{ "stringUnit":{"state":"translated","value":"%1$@: %#@windows@"}, "substitutions":{"windows":{"argNum":2,"formatSpecifier":"lld", "variations":{"plural":{ "one":{"stringUnit":{"state":"translated","value":"%lld window"}}, "other":{"stringUnit":{"state":"translated","value":"%lld windows"}} }}}} },"fr":{"stringUnit":{"state":"translated","value":"Existing"}}}}, "Source key":{}, "device":{"localizations":{"en":{"variations":{"device":{ "mac":{"stringUnit":{"state":"translated","value":"Open on Mac"}} }}}}} }} """#.utf8)) let generated = try LocalizationTools.pseudoCatalog(catalog) let entry = generated["strings"]?["summary"] #expect(entry?["comment"] == .string("Context")) #expect(entry?["localizations"]?["en"] == catalog["strings"]?["summary"]?["localizations"]?["en"]) #expect(entry?["localizations"]?["fr"] == catalog["strings"]?["summary"]?["localizations"]?["fr"]) let pseudo = try #require(entry?["localizations"]?["en-XA"]) #expect(pseudo["substitutions"]?["windows"]?["argNum"] == .number(2)) #expect(pseudo["substitutions"]?["windows"]?["formatSpecifier"] == .string("lld")) #expect(pseudo["stringUnit"]?["value"] == .string(try LocalizationTools.pseudoText("%1$@: %#@windows@"))) let plurals = try #require(pseudo["substitutions"]?["windows"]?["variations"]?["plural"]) for form in ["one", "other"] { let value = try #require(plurals[form]?["stringUnit"]?["value"]?.string) #expect(value.contains("%lld")) #expect(value.hasPrefix("⟦")) #expect(plurals[form]?["stringUnit"]?["state"] == .string("translated")) } #expect( generated["strings"]?["Source key"]?["localizations"]?["en-XA"]?["stringUnit"]?["value"] == .string(try LocalizationTools.pseudoText("Source key"))) #expect( generated["strings"]?["device"]?["localizations"]?["en-XA"]?["variations"]?["device"]?["mac"]?[ "stringUnit"]?["value"] == .string(try LocalizationTools.pseudoText("Open on Mac"))) #expect(try LocalizationTools.pseudoCatalog(generated) == generated) } @Test func invalidPseudoInputsFailLoudly() throws { let catalog = DevJSON.object(["sourceLanguage": .string("fr"), "strings": .object([:])]) #expect(throws: DevError.self) { try LocalizationTools.pseudoCatalog(catalog) } #expect(throws: DevError.self) { try LocalizationTools.run("prepare-pseudo-catalog", []) } #expect(throws: DevError.self) { try LocalizationTools.run("check-localization", []) } } @Test func sourceAuditChecksCatalogBackedModuleLookups() throws { let tables: [String: Set] = [ "Menu": ["Later"], "UI": ["Keep going"], "Settings": ["Saved %@"], "Language": ["Restart Now"], ] let source = #""" Button(MenuText.text("Later")) {} Text(UIStrings.text("Keep going")) Text(SettingsText.string("Saved \(format(value, suffix: ")"))")) Button(LanguageText.string("Restart Now")) {} Text(dynamicWindowTitle) Text("") """# #expect(try LocalizationTools.sourceFailures(source, path: "Fixture.swift", tables: tables).isEmpty) } @Test(arguments: [ #"Text("Missing")"#, #"Button("Later") {}"#, #"NSMenuItem(title: "Later", action: nil, keyEquivalent: "")"#, #"Text(UIStrings.text("Not in catalog"))"#, #"String(localized: "Later", table: "Menu")"#, #"String(localized: "Later", table: "Menu", bundle: .main)"#, #"Bundle.main.localizedString(forKey: "Later", value: nil, table: "Menu")"#, #"AppText.string("Saved \(reason)")"#, ]) func sourceAuditRejectsRawCopyMissingKeysAndErasedInterpolations(_ source: String) throws { let failures = try LocalizationTools.sourceFailures( source, path: "Fixture.swift", tables: ["Menu": ["Later"], "App": ["Saved %@"]]) #expect(!failures.isEmpty) #expect(failures.allSatisfy { $0.hasPrefix("Fixture.swift:1:") }) } @Test func scannerKeepsNestedInterpolationsAndEscapedLiterals() throws { #expect(try LocalizationTools.literalKeys(#""Saved \(format(value, suffix: ")"))""#) == ["Saved %@"]) #expect(try LocalizationTools.literalKeys(#""Literal \\(example)""#) == [#"Literal \(example)"#]) #expect(throws: DevError.self) { try LocalizationTools.literalKeys(#""Saved \(value""#) } } @Test func localizationAuditChecksPlaceholdersStatesAndLanguagePlurals() throws { let english = catalogEntry( value: "%@ has %lld", plural: ["one": catalogEntry(value: "%lld item"), "other": catalogEntry(value: "%lld items")]) let japanese = catalogEntry( value: "%2$lld 個に %1$@", plural: ["other": catalogEntry(value: "%lld 個")]) #expect( try LocalizationTools.localizationFailures( english, japanese, context: "Fixture: count [ja]", language: "ja" ) .isEmpty) let mismatch = catalogEntry( value: "%2$@ 個に %1$@", plural: ["other": catalogEntry(value: "%@ 個")]) let failures = try LocalizationTools.localizationFailures( english, mismatch, context: "Fixture: count [ja]", language: "ja") #expect(failures.contains { $0.contains("placeholder mismatch") }) let needsReview = catalogEntry(value: "%@ has %lld", state: "needs_review") #expect( try LocalizationTools.localizationFailures( english, needsReview, context: "Fixture: count [es]", language: "es" ) .contains { $0.contains("state must be translated") }) } @Test(arguments: ["fr", "pt-BR"]) func oneCategoryCanRenderTheSameCountAsOther(language: String) throws { let english = catalogEntry( value: "%lld windows", plural: ["one": catalogEntry(value: "One window"), "other": catalogEntry(value: "%lld windows")]) let translated = catalogEntry( value: "%lld fenêtres", plural: ["one": catalogEntry(value: "%lld fenêtre"), "other": catalogEntry(value: "%lld fenêtres")]) #expect( try LocalizationTools.localizationFailures(english, translated, context: "count", language: language) .isEmpty) } @Test(arguments: ["%2$lld fenêtre", "%@ fenêtre", "%lld %lld fenêtres"]) func numericOneCategoryStillChecksArgumentPositionTypeAndCount(value: String) throws { let english = catalogEntry( value: "%lld windows", plural: ["one": catalogEntry(value: "One window"), "other": catalogEntry(value: "%lld windows")]) let translated = catalogEntry( value: "%lld fenêtres", plural: ["one": catalogEntry(value: value), "other": catalogEntry(value: "%lld fenêtres")]) #expect( try LocalizationTools.localizationFailures(english, translated, context: "count", language: "fr") .contains { $0.contains("placeholder mismatch") }) } @Test func nonPluralTextCannotIntroduceACount() throws { #expect( try LocalizationTools.localizationFailures( catalogEntry(value: "One window"), catalogEntry(value: "%lld fenêtre"), context: "count", language: "fr" ) .contains { $0.contains("placeholder mismatch") }) } @Test func localizationAuditAcceptsLiteralPercentAfterANumber() throws { let english = catalogEntry(value: "Dim other windows by 10 to 90 percent.") let portuguese = catalogEntry(value: "Escureça as outras janelas de 10% a 90%.") #expect( try LocalizationTools.localizationFailures( english, portuguese, context: "Fixture: dimming [pt-BR]", language: "pt-BR" ) .isEmpty) } @Test func localizationAuditKeepsKeycapGlyphs() throws { let english = catalogEntry(value: "Press ⌃⌥⇧⌘ to continue") let french = catalogEntry(value: "Appuyez sur ⌃⌥⇧⌘ pour continuer") #expect(try LocalizationTools.localizationFailures(english, french, context: "Fixture: keycaps").isEmpty) let spelledOut = catalogEntry(value: "Appuyez sur Ctrl Option Maj Commande pour continuer") #expect( try LocalizationTools.localizationFailures(english, spelledOut, context: "Fixture: keycaps") .contains { $0.contains("placeholder mismatch") }) } @Test func localizationAuditPreservesInflectionMarkup() throws { let english = catalogEntry(value: "^[one window](inflect: true)") let french = catalogEntry(value: "^[une fenêtre](inflect: true)") #expect(try LocalizationTools.localizationFailures(english, french, context: "Fixture: inflection").isEmpty) let missingMarker = catalogEntry(value: "une fenêtre") #expect( try LocalizationTools.localizationFailures(english, missingMarker, context: "Fixture: inflection") .contains { $0.contains("placeholder mismatch") }) } @Test func inflectedLocalesMustIncludeTheirOnePluralBranch() throws { let english = catalogEntry( value: "%@ has %lld", plural: ["one": catalogEntry(value: "%lld item"), "other": catalogEntry(value: "%lld items")]) let spanish = catalogEntry(value: "%@ tiene %lld", plural: ["other": catalogEntry(value: "%lld artículos")]) let failures = try LocalizationTools.localizationFailures( english, spanish, context: "Fixture: count [es]", language: "es") #expect(failures.contains { $0.contains("missing localized string unit") && $0.contains("plural/one") }) } @Test func localizationAuditChecksSubstitutionMetadata() throws { func entry(argument: Int) -> DevJSON { .object([ "stringUnit": .object(["state": .string("translated"), "value": .string("%#@count@")]), "substitutions": .object([ "count": .object([ "argNum": .number(Double(argument)), "formatSpecifier": .string("lld"), "variations": .object([ "plural": .object([ "one": catalogEntry(value: "%lld window"), "other": catalogEntry(value: "%lld windows"), ]) ]), ]) ]), ]) } let english = entry(argument: 1) let correctFrench = entry(argument: 1) #expect( try LocalizationTools.localizationFailures( english, correctFrench, context: "Fixture: windows [fr]", language: "fr" ) .isEmpty) let changedArgument = entry(argument: 2) #expect( try LocalizationTools.localizationFailures( english, changedArgument, context: "Fixture: windows [fr]", language: "fr" ) .contains { $0.contains("format substitution metadata mismatch") }) } @Test(arguments: [ #" || path.hasPrefix("#, "ready && enabled", "path.hasPrefix(value)", "let value = result", "var value: String", "func update(value: String)", "guard ready else { return }", "if ready {", "return settings.load()", "try load()", ]) func catalogAuditRejectsExtractedCodeFragments(_ key: String) throws { #expect(try LocalizationTools.catalogKeyFailures([key], path: "Fixture.xcstrings").count == 1) } @Test(arguments: [ "settings.focusModes.noPeekLevel", "Saved %@", "Ready…", "⌃⌥N", "", "{count} windows", "Forget a size to let the next resize learn it again.", "Click the title bar to return to your window.", "Controls use 10% to 90%.", "Check Settings and try again.", "Use two tiled windows while you try this.", ]) func catalogAuditAcceptsIdentifiersProseAndFormatting(_ key: String) throws { #expect(try LocalizationTools.catalogKeyFailures([key], path: "Fixture.xcstrings").isEmpty) } @Test(arguments: ["ParcelApp", "ParcelUI", "ParcelEngine", "ParcelConfig"]) func repositoryAuditChecksCodeKeysInEveryModule(_ module: String) throws { let manager = FileManager.default let root = manager.temporaryDirectory.resolvingSymlinksInPath().appendingPathComponent( "parcel-catalog-keys-\(UUID())") let resources = root.appendingPathComponent("Sources/\(module)/Resources") try manager.createDirectory(at: resources, withIntermediateDirectories: true) for name in ["ParcelUI", "ParcelApp"] { try manager.createDirectory( at: root.appendingPathComponent("Sources/\(name)"), withIntermediateDirectories: true) } defer { do { try manager.removeItem(at: root) } catch { Issue.record(error) } } let file = resources.appendingPathComponent("Fixture.xcstrings") let key = #" || path.hasPrefix("# let catalog = DevJSON.object([ "sourceLanguage": .string("en"), "strings": .object([key: .object(["shouldTranslate": .bool(false)])]), ]) try catalog.encoded().write(to: file) let failures = try LocalizationTools.audit(root) #expect(failures.count == 1) let failure = try #require(failures.first) #expect( failure.hasSuffix( "Sources/\(module)/Resources/Fixture.xcstrings: catalog key looks like a code fragment: \(key)")) } @Test func sourceKeyOnlyLocalesStillCheckFormatArguments() throws { let manager = FileManager.default let root = manager.temporaryDirectory.appendingPathComponent("parcel-localization-audit-\(UUID())") let resources = root.appendingPathComponent("Sources/ParcelUI/Resources") try manager.createDirectory(at: resources, withIntermediateDirectories: true) try manager.createDirectory( at: root.appendingPathComponent("Sources/ParcelApp"), withIntermediateDirectories: true) defer { do { try manager.removeItem(at: root) } catch { Issue.record(error) } } let catalogURL = resources.appendingPathComponent("Menu.xcstrings") let key = "Item %@ on %lld" func write(_ value: String) throws { let entry = DevJSON.object(["localizations": .object(["fr": catalogEntry(value: value)])]) let catalog = DevJSON.object([ "sourceLanguage": .string("en"), "strings": .object([key: entry]), ]) try catalog.encoded().write(to: catalogURL) } try write("Élément %2$@ sur %1$lld") let failures = try LocalizationTools.audit(root) #expect(failures.contains { $0.contains("placeholder mismatch") }) try write("Élément %1$@ sur %2$lld") #expect(try LocalizationTools.audit(root).isEmpty) } private func catalogEntry(value: String, state: String = "translated", plural: [String: DevJSON] = [:]) -> DevJSON { var entry: [String: DevJSON] = [ "stringUnit": .object(["state": .string(state), "value": .string(value)]) ] if !plural.isEmpty { entry["variations"] = .object(["plural": .object(plural)]) } return .object(entry) } private func tokens(_ text: String) throws -> [String] { try LocalizationTools.formatTokens(text).map { range in let range = try #require(Range(range, in: text)) return String(text[range]) } } }