I'm struggling with localized resources defined in my own pod.
This pod uses Base Internationalization with the following files:
Base.lproj
|- Localizable.strings
|- MyViewController.xib
fr.lproj
|- Localizable.strings
|- MyViewController.strings
This is how they are defined in my podspec
:
spec.resources = "MySDK/*.lproj/*"
In my app's Podfile
, my pod is defined as a local dependency:
pod 'MySDK', :path => '../MySDK'
After I run pod install
, I can see the exact same lproj
folders in Development Pods/MySDK/Resources/MySDK
.
MyViewController.xib
is perfectly loaded and localized, I can change the language on my phone and the text changes accordingly.
In order to use the localized strings, I have defined a function in my SDK pod:
public func NSLocalizedIntentString(key: String) -> String {
let sdkBundle = NSBundle(forClass: MySDKClass.self)
return NSLocalizedString(key, tableName: nil, bundle: sdkBundle, value: key, comment: "")
}
It works, except that it only uses the string defined in Base.lproj
, whatever the selected language on the phone or simulator.
I changed my podspec
for:
spec.resource_bundles = {
"MySDK" => ["MySDK/*.lproj"]
}
in order to load the bundle from the code, but now nothing works, the XIB can't get loaded. It's strange because in Development Pods
I can still see the exact same structure as before, but with blue folder icons instead of yellow.
Here is how I load my XIB from my SDK, not from my app:
MyViewController.swift
public init(...) {
...
super.init(nibName: "MyViewController", bundle: NSBundle(forClass: MyViewController.self))
}
I changed it for:
public init(...) {
...
super.init(nibName: "MyViewController", bundle: NSBundle(path: NSBundle.mainBundle().pathForResource("MySDK", ofType: "bundle")!))
}
but it can't find the bundle.
I've read tons of documentation and forums since this morning without success, so any help would be more than welcome! :)
I finally figured it out.
spec.resources
instead of spec.resource_bundles
in my podspec
because I couldn't access the generated bundle.Then I had to turn off localization for the XIB, because I realized that the resources in the lang subdirectories are not compiled. Thus, the XIB are now outside the lproj
directories.
My new podspec
:
spec.resources = "MySDK/*.lproj", "MySDK/*.xib"
Note that I copy the lproj
directories themselves (*.lproj
), not the content of the lproj
directories (*.lproj/*
), because the Localizable.strings
would override each other. I need to keep them in their lang subdirectories.
Finally, I access the SDK bundle with:
NSBundle(forClass: MySDKClass.self)
I'm still not 100% satisfied because I didn't manage to use the new spec.resource_bundles
property. But anyway, it works.
I've been struggling with this on and off for a week.
The most elucidating thing I found so far was this: https://github.com/CocoaPods/CocoaPods/pull/4635
The above pull requests states in order to use resource_bundle you have to use the RESOURCEDIR/**/*
glob pattern. It doesn't seem to matter what RESOURCEDIR
is. I had been trying all sorts of things, but that **/*
glob results in Xcode recognizing the localization directories.
You can try to reproduce my results
pod lib create
to create a new projectBase.lproj
directorys.resource_bundles
section and change *.png
to **/*
pod install
Now in Xcode you should see the Assets folder with a Base.lproj folder.
pod install
again to bring in the new fileThat should produce an XLIFF file you can use to make translations, then use the Editor > Import Localizations to bring in the translations.
Note You might have to run Build > Clean after importing your translations.
Note It seems the project that is including the pod also has to support localization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With