Search code examples
swiftswift-package-manager

How to include assets / resources in a Swift Package Manager library?


I would like to ship my library using Apple's Swift Package Manager. However my lib includes a .bundle file with several strings translated in different languages. Using cocoapods, I can include it using spec.resource. But in SwiftPM, I cannot do it. Any solution?


Solution

  • Using Swift 5.3 it's finally possible to add localized resources 🎉

    The Package initializer now has a defaultLocalization parameter which can be used for localization resources.

    public init(
        name: String,
        defaultLocalization: LocalizationTag = nil, // New defaultLocalization parameter.
        pkgConfig: String? = nil,
        providers: [SystemPackageProvider]? = nil,
        products: [Product] = [],
        dependencies: [Dependency] = [],
        targets: [Target] = [],
        swiftLanguageVersions: [Int]? = nil,
        cLanguageStandard: CLanguageStandard? = nil,
        cxxLanguageStandard: CXXLanguageStandard? = nil
    )
    

    Let's say you have an Icon.png which you want to be localised for English and German speaking people.

    The images should be included in Resources/en.lproj/Icon.png & Resources/de.lproj/Icon.png.

    After you can reference them in your package like that:

    let package = Package(
        name: "BestPackage",
        defaultLocalization: "en",
        targets: [
            .target(name: "BestTarget", resources: [
                .process("Resources/Icon.png"),
            ])
        ]
    )
    

    Please note LocalizationTag is a wrapper of IETF Language Tag.

    Credits and input from following proposals overview, please check it for more details.