Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146#!/bin/bashset -euo pipefailcd "$(dirname "$0")/../.."root=$PWDif [[ -z ${PARCEL_DEV_BINARY:-} ]]; then PARCEL_DEV_BINARY=$(Scripts/parcel-dev.sh --path); fiexport PARCEL_DEV_BINARYmkdir -p .buildwork=$(mktemp -d "$PWD/.build/localization.XXXXXX")trap 'rm -rf "$work"' EXITScripts/check-localization.shmkdir -p "$work/Probe.app/Contents/MacOS" "$work/Probe.app/Contents/Resources"app="$work/Probe.app"cat > "$app/Contents/Info.plist" <<'PLIST'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>CFBundleIdentifier</key><string>org.example.localization-probe</string><key>CFBundleExecutable</key><string>Probe</string><key>CFBundlePackageType</key><string>APPL</string><key>CFBundleDevelopmentRegion</key><string>en</string><key>CFBundleLocalizations</key><array><string>en</string></array><key>CFBundleAllowMixedLocalizations</key><true/></dict></plist>PLIST# Every actual catalog, including newly added package tables, participates automatically.for directory in Sources/*/Resources; do shopt -s nullglob catalogs=("$directory"/*.xcstrings) (( ${#catalogs[@]} )) || continue module=${directory#Sources/} module=${module%/Resources} bundle="$app/Contents/Resources/$module.bundle" mkdir -p "$bundle" printf '%s\n' '<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>CFBundleDevelopmentRegion</key><string>en</string></dict></plist>' > "$bundle/Info.plist" Scripts/generate-config-strings.sh "$directory" "$bundle" > "$work/compile.log" 2>&1 Scripts/check-localization-pseudo.sh "$directory" "$bundle" >> "$work/compile.log" 2>&1 [[ -z $(find "$bundle" -maxdepth 1 -name '.pseudo.*' -print -quit) ]]donecat > "$work/Probe.swift" <<'SWIFT'import Foundation
struct Catalog: Decodable { let strings: [String: Entry]; struct Entry: Decodable {} }struct Plural: Decodable { let format: String let rules: [String: [String: String]] struct Key: CodingKey { let stringValue: String var intValue: Int? { nil } init?(stringValue: String) { self.stringValue = stringValue } init?(intValue: Int) { return nil } } init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: Key.self) var format: String? var rules: [String: [String: String]] = [:] for key in container.allKeys { if key.stringValue == "NSStringLocalizedFormatKey" { format = try container.decode(String.self, forKey: key) } else { rules[key.stringValue] = try container.decode([String: String].self, forKey: key) } } guard let format else { throw CocoaError(.coderReadCorrupt) } self.format = format self.rules = rules }}
if CommandLine.arguments.contains("--bundle-domain") { print(Bundle.main.bundleIdentifier ?? "<none>") exit(0)}UserDefaults.standard.setVolatileDomain(["AppleLanguages": ["en-XA"]], forName: UserDefaults.argumentDomain)let root = URL(fileURLWithPath: CommandLine.arguments[1])guard let resources = Bundle.main.resourceURL else { fatalError("Missing main resources") }var catalogCount = 0var keyCount = 0for bundleURL in try FileManager.default.contentsOfDirectory(at: resources, includingPropertiesForKeys: nil)where bundleURL.pathExtension == "bundle" { guard let bundle = Bundle(url: bundleURL) else { fatalError("Missing bundle: \(bundleURL)") } let source = root.appendingPathComponent("Sources/\(bundleURL.deletingPathExtension().lastPathComponent)/Resources") for catalogURL in try FileManager.default.contentsOfDirectory(at: source, includingPropertiesForKeys: nil) where catalogURL.pathExtension == "xcstrings" { let catalog = try JSONDecoder().decode(Catalog.self, from: Data(contentsOf: catalogURL)) let table = catalogURL.deletingPathExtension().lastPathComponent for key in catalog.strings.keys where !key.isEmpty { let value = bundle.localizedString(forKey: key, value: nil, table: table) precondition(value.contains("⟦") || value.contains("#@"), "Not pseudo-localized: \(source.lastPathComponent)/\(table): \(key) = \(value)") if key == "Retiled %lld windows." || key == "%lld points" { precondition(String.localizedStringWithFormat(value, Int64(2)).contains("⟦")) } keyCount += 1 } if table == "Settings" { precondition(String(localized: "General", table: table, bundle: bundle).hasPrefix("⟦")) } else if table == "App" { let value = String(localized: "System shortcuts could not be checked: \("failure")", table: table, bundle: bundle) precondition(value.hasPrefix("⟦") && value.contains("failure")) } else if table == "Feedback" { let value = String(localized: "Retiled \(2) windows.", table: table, bundle: bundle) precondition(value.hasPrefix("⟦") && value.contains("2")) } let english = bundleURL.appendingPathComponent("en.lproj/\(table).stringsdict") let pseudo = bundleURL.appendingPathComponent("en-XA.lproj/\(table).stringsdict") if FileManager.default.fileExists(atPath: english.path) { guard let selected = bundle.url(forResource: table, withExtension: "stringsdict") else { fatalError("Missing selected plural table") } precondition(selected.path.contains("en-XA.lproj"), "Wrong selected plural localization: \(selected)") let original = try PropertyListDecoder().decode([String: Plural].self, from: Data(contentsOf: english)) let transformed = try PropertyListDecoder().decode([String: Plural].self, from: Data(contentsOf: pseudo)) precondition(Set(original.keys) == Set(transformed.keys)) for (key, entry) in original { guard let translated = transformed[key] else { fatalError("Missing pseudo plural \(key)") } precondition(Set(entry.rules.keys) == Set(translated.rules.keys)) for (name, rule) in entry.rules { guard let translatedRule = translated.rules[name] else { fatalError("Missing rule \(name)") } precondition(Set(rule.keys) == Set(translatedRule.keys)) for (field, value) in rule { if field.hasPrefix("NSString") { precondition(translatedRule[field] == value) } else { precondition(translatedRule[field]?.hasPrefix("⟦") == true) } } } } } catalogCount += 1 }}print("Pseudo lookup checks passed: \(catalogCount) catalogs, \(keyCount) strings")SWIFTswiftc -swift-version 6 -warnings-as-errors -module-cache-path "$work/module-cache" "$work/Probe.swift" -o "$app/Contents/MacOS/Probe""$app/Contents/MacOS/Probe" "$root" -AppleLanguages '(fr)'[[ $("$app/Contents/MacOS/Probe" --bundle-domain) == org.example.localization-probe ]]mkdir "$app/Contents/Resources/bin"cp "$app/Contents/MacOS/Probe" "$app/Contents/Resources/bin/prcl"cli_domain=$("$app/Contents/Resources/bin/prcl" --bundle-domain)[[ $cli_domain == '<none>' ]]printf 'Nested CLI main bundle identifier: %s\n' "$cli_domain"# Invalid sources fail the gate, including catalog-backed strings that use the wrong lookup API.mkdir -p "$work/fixture/Sources/ParcelUI/Resources" "$work/fixture/Sources/ParcelApp"printf '%s\n' '{"sourceLanguage":"en","version":"1.0","strings":{"Later":{}}}' > "$work/fixture/Sources/ParcelUI/Resources/Menu.xcstrings"printf '%s\n' 'Text("Later")' > "$work/fixture/Sources/ParcelUI/MenuView.swift"if Scripts/check-localization.sh "$work/fixture" > "$work/failure.log" 2>&1; then printf 'Source localization check accepted a raw literal.\n' >&2 exit 1figrep -q 'catalog-backed module lookup' "$work/failure.log"printf '%s\n' 'Text(MenuText.text("Later"))' > "$work/fixture/Sources/ParcelUI/MenuView.swift"Scripts/check-localization.sh "$work/fixture"printf 'Localization script checks passed\n'