swiftxcodelocalizationswift-package-managerswift-package

String Catalog doesn't work with Swift Package


Can't make the new String Catalog work with my Swift Package (command line app)

Here is my Package.swift

// swift-tools-version:5.9

import PackageDescription

let package = Package(
    name: "MyPackageName",
    defaultLocalization: "en",
    platforms: [.macOS(.v13)],
    targets: [
        .executableTarget(
            name: "MyPackageName",
            dependencies: [],
            resources: [
                .process("_Resources/Localizable.xcstrings")
            ])
    ]
)

Here is how I use it in the code:

print(String(localized: "aaaa"))
// or
print(String(localized: "aaaa", table: "Localizable", bundle: .main, locale: Locale(identifier: "en"), comment: nil))
// or
print(String(localized: "aaaa", defaultValue: .init(stringLiteral: "b")))
// or
print(String(localized: "aaaa", table: "Localizable", bundle: .main, locale: Locale(identifier: "en"), comment: ""))
// or
// I've tried almost all combinations)

I'm sure that the file is properly linked because I can find the localization files in the Build folder in the MyPackageName_ MyPackageName.bundle:

<?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>aaaa</key>
    <string>bbbb</string>
</dict>
</plist>

Any thoughts on this?


Solution

  • It turned out that the answer is pretty straightforward. You just need to explicitly specify the bundle)

    String(localized: "key", bundle: .module)