Reputation: 1
As the changes of cocoapods 1.0.0.beta.1 say "Localized interface files (XIB, Storyboard) using Base Internationalization - Base.lproj/Main.xib and en.lproj/Main.strings are represented as a variant group named Main.xib" and as far as cocoapods 1.0.0.beta.1 "Special case interface files to use the XIB or Storyboard name for the variant group when using Base Internationalization."
I refer to the demo and use resource_bundles to organize my resources on my podspecs.
s.resource_bundles = {
'Resources' => ['LocalizationDemo/LocalizationDemo/Resources/**/*.{lproj,storyboard}']
}
and my cocoapods version is 1.0.1 but the directories result is
-Resources
--en.lproj
---LocalizationDemo.strings
--LocalizationDemo.storyboard
--de.lproj
---LocalizationDemo.strings
It's not the result directories which I expected and the interface internationalization don't work. Cloud anyone show me a correct usage or demo?
ADD: I use import/export localization by xcode before be pod to other projects. and I want it's could work by imported xliff files directly instead of add the IBOutlet or a subclass for UI controls.
Upvotes: 0
Views: 1114
Reputation: 3588
You should write classes for your controls
used in XIB OR Storyboard
Views
& assign the classes to respective control types like this -
class LocalizedTextField: UITextField {
override func drawPlaceholderInRect(rect: CGRect) {
let localizedPlaceHolder = self.placeholder!.localized
self.placeholder = localizedPlaceHolder
super.drawPlaceholderInRect(rect)
}
}
class LocalizedLabel : UILabel {
override func awakeFromNib() {
if let text = text {
self.text = text.localized
self.bounds.size.width = CGFloat.max
self.sizeToFit()
}
}
}
class LocalizedButton : UIButton {
override func awakeFromNib() {
for state in [UIControlState.Normal, UIControlState.Highlighted, UIControlState.Selected, UIControlState.Disabled, UIControlState.Focused] {
if let title = titleForState(state) {
setTitle(title.localized, forState: state)
}
}
}
}
extension String {
var localized: String {
let localizedValue = NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
if localizedValue == "" {
return self
}
else
{
return localizedValue
}
return self
}
}
Upvotes: 0