In my previous framework, I have a localizable.strings file with some global entries for localization. On the left lane, there was a “Localize” button in Xcode to do this.
How can I do this with Swift Package Manager?
In my previous framework, I have a localizable.strings file with some global entries for localization. On the left lane, there was a “Localize” button in Xcode to do this.
How can I do this with Swift Package Manager?
To add localizable string file in SPM, it is possible from swift-tools-version: 5.3
,
Changes in Package.swift
,
Needs to set defaultLocalization: "en"
then add resources filed in target like,
And package structure should be like,
And to access localizable string through code in the package,
SPM supports resources since 5.3. Check the accepted answer, and this checklist if you still have trouble.
(outdated)
Here are a couple of provisional workarounds that may fit some use cases.
If your framework is distributed only with an app, you can move the localization resources to the app and reference them through its bundle.
Add the Localization.strings
file in the app with bundle id my.app.bundle
.
Write this from your framework:
let bundle = Bundle(identifier: "my.app.bundle")!
NSLocalizedString("SomeKey", tableName: "Localization", bundle: bundle, value: "SomeKey", comment: "")
You can do something similar with images.
Another option is to store your resources in the framework as source code. This requires encoding, for instance, to Base64, then decoding to access the resources. Base64 uses more space, but this may not be an issue depending on your case.