diff --git a/TODO1/localize-swift-application.md b/TODO1/localize-swift-application.md index c42323fed07..3fdb4498920 100644 --- a/TODO1/localize-swift-application.md +++ b/TODO1/localize-swift-application.md @@ -2,22 +2,22 @@ > * 原文作者:[Dmytro Pylypenko](https://medium.com/@dimpiax) > * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) > * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/localize-swift-application.md](https://github.com/xitu/gold-miner/blob/master/TODO1/localize-swift-application.md) -> * 译者: -> * 校对者: +> * 译者:[chaingangway](https://github.com/chaingangway) +> * 校对者:[Bruce-pac](https://github.com/Bruce-pac) -# Localize Swift Application +# 如何运用 Swift 的属性包装器实现应用本地化 ![](https://cdn-images-1.medium.com/max/4800/1*SjXk6V6r3e94guJcg5mRzw.png) -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 Wrapper**)的使用,以及如何简化代码并使其易于维护。我会通过几个主题对此进行说明。 -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 引入了**属性包装器**,它可以让我们在属性和访问逻辑(getter 和 setter)之间设置中间层。 -This part is about how to localize your application using **Property Wrappers** and simple way within `@IBOutlet`. +下面的内容是在 `@IBOutlet` 变量内部使用**属性包装器**的简便方法来实现应用本地化。 --- -So, instead of the **basic** case: +优化下面这个**基础**版本: ```Swift class NatureViewController: UIViewController { @@ -35,7 +35,7 @@ class NatureViewController: UIViewController { } ``` -We can improve the code with **property wrapper** `@Localized`, and have: +我们可以用**属性包装器** `@Localized` 改进代码,如下: ```Swift class NatureViewController: UIViewController { @@ -47,8 +47,8 @@ class NatureViewController: UIViewController { } ``` -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` 属性包装器。 +将 key 当做枚举来使用会更好,如:`@Localized(.natureTitle)`。 ```Swift @propertyWrapper @@ -67,8 +67,8 @@ struct Localized { } ``` -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` 的扩展方法。 ```Swift protocol Localizable { @@ -88,7 +88,7 @@ extension UILabel: Localizable { } ``` -And the final thing that we need is `LocalizationKey`: +最后我们只需要实现 `LocalizationKey`: ```Swift enum LocalizationKey: String { @@ -104,9 +104,9 @@ extension LocalizationKey { } ``` -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. +我们可以直接用 raw 的值来表示相应的 key,`String` 类型默认遵守这个协议,所以只需要枚举中的值与 `Localizable.strings` 中的 key 保持一致就可以了。 -So in the end our code looks like: +最终的代码如下: ```Swift class NatureViewController: UIViewController { @@ -120,14 +120,13 @@ class NatureViewController: UIViewController { --- -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:** -[**Properties - The Swift Programming Language (Swift 5.2)** -**Properties associate values with a particular class, structure, or enumeration. Stored properties store constant and…**docs.swift.org](https://docs.swift.org/swift-book/LanguageGuide/Properties.html) +**想了解更多关于属性包装器的知识,请阅读官方文档:** +[**Properties - The Swift Programming Language (Swift 5.2)**](https://docs.swift.org/swift-book/LanguageGuide/Properties.html) > 如果发现译文存在错误或其他需要改进的地方,欢迎到 [掘金翻译计划](https://github.com/xitu/gold-miner) 对译文进行修改并 PR,也可获得相应奖励积分。文章开头的 **本文永久链接** 即为本文在 GitHub 上的 MarkDown 链接。