Skip to content

Commit

Permalink
如何运用 Swift 的属性包装器实现应用本地化 (#6994)
Browse files Browse the repository at this point in the history
* 更新测试

* Revert "更新测试"

This reverts commit afb007d.

* 8 个值得了解的树形数据结构 (#6804)

* 8个值得了解的树形数据结构

8个值得了解的树形数据结构

* Update 8-Useful-Tree-Data-Structures-Worth-Knowing.md

* Update 8-useful-tree-data-structures-worth-knowing.md

Co-authored-by: sun <776766759@qq.com>

* 眼动跟踪和移动世界的最佳用户体验实践 (#6806)

* 眼动跟踪和移动世界的最佳用户体验实践

翻译完成

* 眼动跟踪和移动世界的最佳用户体验实践 #6806

谢谢指点~@xionglong58     @fanyijihua 根据第一轮校对意见修改完成

* 眼动追踪和移动世界的最佳用户体验实践

* 眼动追踪和移动世界的最佳用户体验实践

* 我并不讨厌箭头函数(#6610) (#6659)

* 我并不讨厌箭头函数(#6610)

* 我并不讨厌箭头函数(#6610) - 根据校对结果修改

* Update i-dont-hate-arrow-functions.md

Co-authored-by: 小添 <xiaotian@qunhemail.com>
Co-authored-by: sun <776766759@qq.com>

* 掌握 JavaScript 面试:什么是纯函数? (#6828)

* 掌握 JavaScript 面试:什么是纯函数?

掌握 JavaScript 面试:什么是纯函数?

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

Co-authored-by: lsvih <lsvih@qq.com>

* fix:文章翻译问题 (#6833)

* 用依赖注入来解耦你的代码 (#6823)

* translate(*): Part.1

* translate(*): Part.2

* translate(*): 完成翻译

* fix(*): 完成校对

* Create generator-functions-in-javascript.md (#6841)

* Create generator-functions-in-javascript.md

* Update generator-functions-in-javascript.md

* Create how-to-keep-your-dependencies-secure-and-up-to-date.md (#6843)

* Create how-to-keep-your-dependencies-secure-and-up-to-date.md

* Update how-to-keep-your-dependencies-secure-and-up-to-date.md

* Create deep-dive-into-react-fiber-internals.md (#6845)

* Create deep-dive-into-react-fiber-internals.md

* Update deep-dive-into-react-fiber-internals.md

* Update deep-dive-into-react-fiber-internals.md

* Update deep-dive-into-react-fiber-internals.md

* Create how-can-cloud-services-help-improve-your-businessess-efficiency.md (#6847)

* Create how-can-cloud-services-help-improve-your-businessess-efficiency.md

* Update how-can-cloud-services-help-improve-your-businessess-efficiency.md

* Create should-you-learn-vim-as-a-developer-in-2020.md (#6849)

* Go 发布新版 Protobuf API (#6827)

* Achieve translation

* 8 个值得了解的树形数据结构 (#6804)

* 8个值得了解的树形数据结构

8个值得了解的树形数据结构

* Update 8-Useful-Tree-Data-Structures-Worth-Knowing.md

* Update 8-useful-tree-data-structures-worth-knowing.md

Co-authored-by: sun <776766759@qq.com>

* 眼动跟踪和移动世界的最佳用户体验实践 (#6806)

* 眼动跟踪和移动世界的最佳用户体验实践

翻译完成

* 眼动跟踪和移动世界的最佳用户体验实践 #6806

谢谢指点~@xionglong58     @fanyijihua 根据第一轮校对意见修改完成

* 眼动追踪和移动世界的最佳用户体验实践

* 眼动追踪和移动世界的最佳用户体验实践

* 我并不讨厌箭头函数(#6610) (#6659)

* 我并不讨厌箭头函数(#6610)

* 我并不讨厌箭头函数(#6610) - 根据校对结果修改

* Update i-dont-hate-arrow-functions.md

Co-authored-by: 小添 <xiaotian@qunhemail.com>
Co-authored-by: sun <776766759@qq.com>

* 掌握 JavaScript 面试:什么是纯函数? (#6828)

* 掌握 JavaScript 面试:什么是纯函数?

掌握 JavaScript 面试:什么是纯函数?

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

* Update master-the-javascript-interview-what-is-a-pure-function.md

Co-authored-by: lsvih <lsvih@qq.com>

* Address comments

Co-authored-by: Amberlin1970 <37952468+Amberlin1970@users.noreply.github.com>
Co-authored-by: sun <776766759@qq.com>
Co-authored-by: Charlo <49369951+Charlo-O@users.noreply.github.com>
Co-authored-by: TiaossuP <tiaossup@yeah.net>
Co-authored-by: 小添 <xiaotian@qunhemail.com>
Co-authored-by: niayyy <yy761706@gmail.com>
Co-authored-by: lsvih <lsvih@qq.com>

* JSON.stringify() 的 5 个秘密特性 (#6793)

* Update 5-secret-features-of-json-stringify.md

* Update 5-secret-features-of-json-stringify.md

* Update 5-secret-features-of-json-stringify.md

* Update 5-secret-features-of-json-stringify.md

* Create high-speed-inserts-with-mysql.md (#6853)

* Create high-speed-inserts-with-mysql.md

* Update high-speed-inserts-with-mysql.md

* 组合软件:书 (#6832)

* Update composing-software-the-book.md

* Update composing-software-the-book.md

* Create 6-best-javascript-frameworks-in-2020.md (#6861)

* Create 6-best-javascript-frameworks-in-2020.md

* Update 6-best-javascript-frameworks-in-2020.md

* Update 6-best-javascript-frameworks-in-2020.md

* 2020 年用各大前端框架构建的 RealWorld 应用对比 (#6851)

* translation complete

* Update a-realworld-comparison-of-front-end-frameworks-2020.md

按照建议修改完成

* Update a-realworld-comparison-of-front-end-frameworks-2020.md

又修改了下破折号的格式

* Update a-realworld-comparison-of-front-end-frameworks-2020.md

修改了二级标题的显示问题

* Update a-realworld-comparison-of-front-end-frameworks-2020.md

添加相关个人信息

* Update a-realworld-comparison-of-front-end-frameworks-2020.md

Co-authored-by: lsvih <lsvih@qq.com>

* Create the-importance-of-why-docs.md (#6868)

* Create the-importance-of-why-docs.md

* Update the-importance-of-why-docs.md

* 不变性之道 (#6857)

* Update the-dao-of-immutability.md

* Update the-dao-of-immutability.md

* Update the-dao-of-immutability.md

* Update the-dao-of-immutability.md

* Update the-dao-of-immutability.md

* Update the-dao-of-immutability.md

Co-authored-by: lsvih <lsvih@qq.com>

* Create polymorphic-react-components.md (#6870)

* Create polymorphic-react-components.md

* Update polymorphic-react-components.md

* Update polymorphic-react-components.md

* Update polymorphic-react-components.md

* Create active-learning-in-machine-learning.md (#6872)

* Create kafka-vs-rabbitmq-why-use-kafka.md (#6874)

* Create kafka-vs-rabbitmq-why-use-kafka.md

* Update kafka-vs-rabbitmq-why-use-kafka.md

* Create i-built-an-app-that-uses-all-7-new-features-in-javascript-es2020.md (#6876)

* Create i-built-an-app-that-uses-all-7-new-features-in-javascript-es2020.md

* Update i-built-an-app-that-uses-all-7-new-features-in-javascript-es2020.md

* Update i-built-an-app-that-uses-all-7-new-features-in-javascript-es2020.md

* Update i-built-an-app-that-uses-all-7-new-features-in-javascript-es2020.md

* 云服务如何帮助你提高业务效率? (#6859)

* Update how-can-cloud-services-help-improve-your-businessess-efficiency.md

* Update how-can-cloud-services-help-improve-your-businessess-efficiency.md: revise according to proofreading suggestions

* Update how-can-cloud-services-help-improve-your-businessess-efficiency.md: revise according to Yinjias proofreading suggestions

* NestJS 实现基本用户认证和会话 (#6731)

* NestJS 实现基本用户认证和会话

NestJS 实现基本用户认证和会话

* fix:修改 NestJS 实现基本用户认证和会话

根据校对者意见,修改 NestJS 实现基本用户认证和会话

* Update nestjs-basic-auth-and-sessions.md

Co-authored-by: lsvih <lsvih@qq.com>

* 作为 2020 年的开发者,你应该学习 VIM 吗? (#6856)

* 更新测试

* Revert "更新测试"

This reverts commit afb007d.

* 作为2020年的开发者,你应该学习 VIM 吗?

* 校对更新

* 第二次校对修改

* 第三次校对修改

* Update TODO1/should-you-learn-vim-as-a-developer-in-2020.md

数字格式调整

Co-Authored-By: lsvih <lsvih@qq.com>

* Update TODO1/should-you-learn-vim-as-a-developer-in-2020.md

标题数字格式调整

Co-Authored-By: lsvih <lsvih@qq.com>

* 增加校对者名单

* 调整行数

* Update should-you-learn-vim-as-a-developer-in-2020.md

Co-authored-by: lsvih <lsvih@qq.com>

* MySQL 最佳实践—— 高效插入数据 (#6863)

* Finish translation

* Addressed comments

* 添加校对者信息

* Remove content with english

* Create combine-getting-started.md (#6883)

* Create combine-getting-started.md

* Update combine-getting-started.md

* Update combine-getting-started.md

* Create how-to-be-a-good-remote-developer.md (#6885)

* Create why-is-object-immutability-important.md (#6887)

* Create why-is-object-immutability-important.md

* Update why-is-object-immutability-important.md

* Create what-on-earth-is-the-shadow-dom-and-why-it-matters.md

* 2020 年排名前 6 位的 JavaScript 框架 (#6867)

* Update 6-best-javascript-frameworks-in-2020.md: Complete translation

* Update 6-best-javascript-frameworks-in-2020.md: revise according to 钱俊颖s and niayyys proofreading suggestions

* Update 6-best-javascript-frameworks-in-2020.md: add proofreaders' information

* JavaScript 风格元素 (#6878)

* JavaScript 风格元素

JavaScript 风格元素

* 翻译修改

* 修改标点符号

* 增加校对者信息

Co-authored-by: lsvih <lsvih@qq.com>

* 怎样让依赖库保持安全和最新 (#6864)

* 更新测试

* Revert "更新测试"

This reverts commit afb007d.

* 怎样让依赖库保持安全和最新

* 第一次校对提交(part)

* Create 5-best-practices-to-prevent-git-leaks.md (#6894)

* Create 5-best-practices-to-prevent-git-leaks.md

* Update 5-best-practices-to-prevent-git-leaks.md

* Create swiftui-3d-scroll-effect.md (#6896)

* Create swiftui-3d-scroll-effect.md

* Update swiftui-3d-scroll-effect.md

* Web 应用程序中的数据和 UI 分离 (#6794)

* Update separation-of-data-and-ui-in-your-web-app.md

* Update separation-of-data-and-ui-in-your-web-app.md

* fix:typo (#6903)

* 初译完成

* 校对完毕

Co-authored-by: Amberlin1970 <37952468+Amberlin1970@users.noreply.github.com>
Co-authored-by: sun <776766759@qq.com>
Co-authored-by: Charlo <49369951+Charlo-O@users.noreply.github.com>
Co-authored-by: TiaossuP <tiaossup@yeah.net>
Co-authored-by: 小添 <xiaotian@qunhemail.com>
Co-authored-by: niayyy <yy761706@gmail.com>
Co-authored-by: lsvih <lsvih@qq.com>
Co-authored-by: zoomdong <1344492820@qq.com>
Co-authored-by: 江不知 <448300947@qq.com>
Co-authored-by: 司徒公子 <todaycoder001@gmail.com>
Co-authored-by: jianhang <snowy.yu@foxmail.com>
Co-authored-by: Roc <qinrocdev@gmail.com>
Co-authored-by: Jessica <29631279+cyz980908@users.noreply.github.com>
Co-authored-by: Amy <46389309+febrainqu@users.noreply.github.com>
  • Loading branch information
15 people committed May 11, 2020
1 parent 53cd187 commit 4157021
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions TODO1/localize-swift-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,7 +35,7 @@ class NatureViewController: UIViewController {
}
```

We can improve the code with **property wrapper** `@Localized`, and have:
我们可以用**属性包装器** `@Localized` 改进代码,如下:

```Swift
class NatureViewController: UIViewController {
Expand All @@ -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
Expand All @@ -67,8 +67,8 @@ struct Localized<T: Localizable> {
}
```

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 {
Expand All @@ -88,7 +88,7 @@ extension UILabel: Localizable {
}
```

And the final thing that we need is `LocalizationKey`:
最后我们只需要实现 `LocalizationKey`:

```Swift
enum LocalizationKey: String {
Expand All @@ -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 {
Expand All @@ -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 链接。
Expand Down

0 comments on commit 4157021

Please sign in to comment.