#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")/../.."
root=$PWD
if [[ -z ${PARCEL_DEV_BINARY:-} ]]; then PARCEL_DEV_BINARY=$(Scripts/parcel-dev.sh --path); fi
export PARCEL_DEV_BINARY
mkdir -p .build
work=$(mktemp -d "$PWD/.build/localization.XXXXXX")
trap 'rm -rf "$work"' EXIT
Scripts/check-localization.sh
mkdir -p "$work/Probe.app/Contents/MacOS" "$work/Probe.app/Contents/Resources"
app="$work/Probe.app"
cat > "$app/Contents/Info.plist" <<'PLIST'
CFBundleIdentifierorg.example.localization-probe
CFBundleExecutableProbe
CFBundlePackageTypeAPPL
CFBundleDevelopmentRegionen
CFBundleLocalizationsen
CFBundleAllowMixedLocalizations
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' 'CFBundleDevelopmentRegionen' > "$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) ]]
done
cat > "$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 ?? "")
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 = 0
var keyCount = 0
for 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")
SWIFT
swiftc -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 == '' ]]
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 1
fi
grep -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'