Localize UI Components in a simple and declarative way
pod ‘LocalizedSwift’
From Xcode, select File → Swift Packages → Add Package Dependency → Select your project → Search LocalizedSwift
It is possible to localize a class that conforms Localizable
adding @Localized("YouKey")
before the component declaration.
UILabel
, UIButton
, UITextField
and UIImageView
already conform Localizable
.
// In the following example the string for the key "Label.Title" will be searched in the file "Localizable.strings".
@Localized("Label.Title")
@IBOutlet private var label: UILabel!
If the strings are defined in a file different from Localizable.strings
set the parameter stringsFileName
with the name of the file.
// In the following example the string for the key "Label.Title" will be searched in the file "AFile.strings".
@Localized("Label.Title", stringsFileName: "AFile")
@IBOutlet private var label: UILabel!
If the .strings
file is in a bundle other than .main
set the parameter bundle
with the bundle where the file is embedded.
// In the following example the string for the key "Label.Title" will be searched in the file "AFile.strings" in the bundle `.anotherBundle`.
@Localized("Label.Title", stringsFileName: "AFile", bundle: .anotherBundle)
@IBOutlet private var label: UILabel!
It is possible to set different localized strings for different UIControl.State
using the declaration @Localized(.key(, for:), .key(, for:) ..., stringsFileName:, bundle:)
.
The method .key(:, for: )
is a factory method to instantiate the object that associate a key to a state LocalizedConfiguration
.
// In the following example the string for the key "Button.Title" will be set for the normal state and the string for the key "Button.Highlighted.Title" will be set for the highlighted state.
@Localized(.key("Button.Title", for: .normal),
.key("Button.Highlighted.Title", for: .highlighted))
@IBOutlet private var button: UIButton!
It is possible to set stringsFileName
and bundle
just like in the examples above.
Made with ❤️ and ☕️ by @MarioIannotta