本地应用程序
Hello Swift developers, I want to share with you my experience and knowledge about using Property Wrappers in an application and how to reduce your code and make it easier to maintain. I will write about this in several topic parts.
您好,Swift开发人员,我想与您分享我在应用程序中使用Property Wrappers的经验和知识,以及如何减少代码并使维护更容易。 我将在几个主题部分中对此进行撰写。
In Swift 5.1, Apple introduced Property Wrappers, which gives the possibility to set up additional layer between your property and serving logic.
在Swift 5.1中,Apple引入了Property Wrappers ,它使在属性和服务逻辑之间设置附加层成为可能。
This part is about how to localize your application using Property Wrappers and simple way within @IBOutlet
.
这部分是关于如何使用Property Wrappers和@IBOutlet
简单方法来本地化应用程序的。
So, instead of the basic case:
因此,代替基本情况:
class NatureViewController: UIViewController {
@IBOutlet private var label: UILabel! {
didSet {
label.title = NSLocalizedString("natureTitle", comment: "")
}
}
@IBOutlet private var button: UIButton! {
didSet {
button.setTitle(NSLocalizedString("saveNatureButton", comment: ""), for: .normal)
}
}
}
We can improve the code with property wrapper @Localized
, and have:
我们可以使用属性包装器 @Localized
改进代码,并具有:
class NatureViewController: UIViewController {
@Localized("natureTitle")
@IBOutlet private var label: UILabel!
@Localized("saveNatureButton")
@IBOutlet private var button: UIButton!
}
It looks pretty, doesn’t it? So let’s create @Localized
property wrapper.It will be good to apply key as enum, to have: @Localized(.natureTitle)
看起来很漂亮,不是吗? 因此,让我们创建@Localized
属性@Localized
。将键用作枚举将是很好的: @Localized(.natureTitle)
@propertyWrapper
struct Localized<T: Localizable> {
private let key: LocalizationKey
var wrappedValue: T? = nil {
didSet {
wrappedValue?.set(localization: key)
}
}
init(_ key: LocalizationKey) {
self.key = key
}
}
And be able to apply for any that conforms Localizable
protocol.To achieve our goal, we will extend also UILabel
and UIButton
.
并且可以申请任何符合Localizable
协议的协议。要实现我们的目标,我们还将扩展UILabel
和UIButton
。
protocol Localizable {
func set(localization: LocalizationKey)
}
extension UIButton: Localizable {
func set(localization key: LocalizationKey) {
setTitle(key.string, for: .normal)
}
}
extension UILabel: Localizable {
func set(localization key: LocalizationKey) {
text = key.string
}
}
And the final thing that we need is LocalizationKey
:
最后,我们需要的是LocalizationKey
:
enum LocalizationKey: String {
case
natureTitle,
saveNatureButton
}
extension LocalizationKey {
var string: String {
NSLocalizedString(rawValue, comment: rawValue)
}
}
As our enum is raw representable, as a String
conforms to the protocol, it’s enough just to have Localizable.strings
with the same keys for values.
由于我们的枚举是原始可表示的,因此String
符合协议,仅使Localizable.strings
具有与值相同的键就足够了。
So in the end our code looks like:
因此,最终我们的代码如下所示:
class NatureViewController: UIViewController {
@Localized(.natureTitle)
@IBOutlet private var label: UILabel!
@Localized(.saveNatureButton)
@IBOutlet private var button: UIButton!
}
That’s all in this part! Here are some potential ideas that @Localized
can contain:
这就是全部! 以下是@Localized
可能包含的一些潜在想法:
- applying format values with strings for dynamic replacement. 将格式值与字符串一起应用以进行动态替换。
- be able to determine strings from a specific table and bundle. 能够从特定的表和捆绑确定字符串。
More about Property Wrapper read in the official documentation:
有关Property Wrapper的更多信息,请参阅官方文档:
翻译自: https://levelup.gitconnected.com/localize-swift-application-f1fd0f4af800
本地应用程序