Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将 Android 程序移植为 Kotlin 程序 #2039

Merged
merged 18 commits into from
Aug 21, 2017
Merged

Conversation

wilsonandusa
Copy link
Contributor

@wilsonandusa wilsonandusa commented Aug 10, 2017

@sqrthree 翻译完成。resolve #1969

@wilsonandusa wilsonandusa changed the title Master Master将 Android 程序移植为 Kotlin 程序 Aug 10, 2017
@wilsonandusa wilsonandusa changed the title Master将 Android 程序移植为 Kotlin 程序 将 Android 程序移植为 Kotlin 程序 Aug 10, 2017
@Zhiw
Copy link
Contributor

Zhiw commented Aug 10, 2017

校对认领

@linhe0x0
Copy link
Member

@Zhiw 好的呢 🍺

Copy link
Contributor

@Zhiw Zhiw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sqrthree @wilsonandusa 校对完成

If you’re curious you can jump straight into the [source code on GitHub](https://github.com/googlesamples/android-topeka/tree/kotlin).
*For now the code is on a separate branch, but we’re planning to merge the Kotlin code into master at some point in the future.*
如果你好奇的话可以直接来看 [GitHub 上的源代码](https://github.com/googlesamples/android-topeka/tree/kotlin)
目前 Kotlin 代码在一个独立的分支上,但在未来我们打算将其合并到主代码中。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原文是斜体

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所以译文要粗体(逃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但在未来我们打算将其合并到主代码中。

但我们计划在未来某个时刻将其……


---

![](https://cdn-images-1.medium.com/max/1600/1*oML2dls3WxjhTnR4a_TTRg.png)

It still looks the same
依旧看上去一样
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『依旧看上去一样』=>『看上去依旧(仍然)一样』


**Going step by step is a sensible approach.
**Kotlin compiles down to Java byte code and the two languages are interoperable. Also it’s possible to have both languages within the same project. So it’s not necessary to migrate all code to another language.
**一步步去做是很明智的做法。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『一步步去做是很明智的做法』,这句加粗,搬运文章有问题,参考了原文


Starting off with the less complex data classes was the clear choice for me.
They are being used throughout the project, yet their complexity is comparatively low. This makes them an ideal starting point to set off the journey into a new language.
在通过使用 Android Studio 自带的 Koltlin 代码转换器移植一部分代码后,我开始执行并通过测试,直到最终将测试本身也移植为 Kotlin 代码。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kotlin 单词打错咯


> *‘Multiple exclamation marks,’ he went on, shaking his head, ‘are a sure sign of a diseased mind. — *[*Terry Pratchett*](https://wiki.lspace.org/mediawiki/Multiple_exclamation_marks)
> *“过多使用感叹号,” 他一边摇头一边说道, ”是心理不正常的表现。” — *[*Terry Pratchett*](https://wiki.lspace.org/mediawiki/Multiple_exclamation_marks)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议参考原文进行修改


Let’s take a look at some examples that I came across.
Kotlin 是门很强大的语言, 因为我们可以通过评论和反馈不断地改写生成的代码来使其变得更加符合语言的习惯。这是代码更加简洁并且提升了可读性。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这使代码更加简洁


Let’s take an adapter’s `getView` as example:
#### 少读点儿并不一定是件坏事
我们拿 adapter 里面的 getView 来举例:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『getView』 => 『getView


That may be because I am still new to the language or because there hasn’t been that much investment in gathering and publicising these yet. Maybe there is a collection which I am yet to come across, but it seems that there is quite some space for platform specific idioms.
If you’re aware of collections like this, please add them to the comments.
这主要是因为我对这门语言还不是很熟,或者说我还没有花太大精力去研究这方面。也许是因为我还没有碰到这类情况,but it seems that there is quite some space for platform specific idioms.(这句不知道怎么翻译。。)如果你知道这种情况,请在评论区补充。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『but it seems that there is quite some space for platform specific idioms』=>『但似乎还有相当多的平台特定的语言风格』

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

platform可能是kotlin版本。。
某个版本就有编译问题

@phxnirvana
Copy link
Contributor

我来接盘吧。。校对认领@sqrthree

@linhe0x0
Copy link
Member

@phxnirvana 妥妥哒 🍻

Copy link
Contributor

@phxnirvana phxnirvana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这篇译文质量不太好啊。。有一种秋名山翻车的感觉

> * 校对者:

# Migrating an Android project to Kotlin
# Android 程序移植为 Kotlin 程序
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

供参考,将 Android 项目迁移到 Kotlin 语言

A while ago we open sourced [Topeka](https://github.com/googlesamples/android-topeka), an Android quiz app.
It is tested, using [integration tests](https://github.com/googlesamples/android-topeka/tree/master/app/src/androidTest/java/com/google/samples/apps/topeka) and [unit tests](https://github.com/googlesamples/android-topeka/tree/master/app/src/test/java/com/google/samples/apps/topeka).
And it is purely written in Java. Well… it was
不久前我们开源了 [Topeka](https://github.com/googlesamples/android-topeka),一个安卓写的小测试程序。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

android≠安卓

It is tested, using [integration tests](https://github.com/googlesamples/android-topeka/tree/master/app/src/androidTest/java/com/google/samples/apps/topeka) and [unit tests](https://github.com/googlesamples/android-topeka/tree/master/app/src/test/java/com/google/samples/apps/topeka).
And it is purely written in Java. Well… it was
不久前我们开源了 [Topeka](https://github.com/googlesamples/android-topeka),一个安卓写的小测试程序。
这个程序是用 [integration tests](https://github.com/googlesamples/android-topeka/tree/master/app/src/androidTest/java/com/google/samples/apps/topeka) 和 [unit tests](https://github.com/googlesamples/android-topeka/tree/master/app/src/test/java/com/google/samples/apps/topeka) 进行测试的, 而且本身全部是用 Java 写的。至少以前是这样的...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

而且全部是用 Java 写的。呃……好吧,这是个过去时


At Google I/O 2017 we announced official [support for the Kotlin programming language](https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/). That’s when I started migrating the code base away from Java, **learning Kotlin on the way.**
2017年谷歌在开发者大会上官方宣布 [支持 Kotlin 编程语言](https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/)。从那时起,我便开始移植 Java 代码,**同时在过程中学习Kotlin。**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we announced official,这个we会不会代表作者是官方的?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phxnirvana 作者就是 Google 大佬:joy:
不过这里不碍事啦

If you’re curious you can jump straight into the [source code on GitHub](https://github.com/googlesamples/android-topeka/tree/kotlin).
*For now the code is on a separate branch, but we’re planning to merge the Kotlin code into master at some point in the future.*
如果你好奇的话可以直接来看 [GitHub 上的源代码](https://github.com/googlesamples/android-topeka/tree/kotlin)
目前 Kotlin 代码在一个独立的分支上,但在未来我们打算将其合并到主代码中。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所以译文要粗体(逃

@@ -254,44 +248,45 @@ fun Parcel.writeBoolean(toWrite: Boolean) = writeByte(if (toWrite) 1 else 0)
fun Parcel.readBoolean() = 1 == this.readByte()
```

Having this written in one place, makes it possible to call `parcel.writeBoolean(value)` and `parcel.readBoolean()` directly, as if it were part of the framework. If Android Studio would not highlight extension functions differently, they were almost not noticeable.
当写好以上代码之后,我们可以把
`parcel.writeBoolean(value)` 和 `parcel.readBoolean()` 当成框架的一部分直接调用。要不是因为 Android Studio 使用不同的高亮方式区分扩展函数,它们看上去简直一模一样。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一模一样这个。。怎么翻过来的


**Extending functionality makes things easier to read.** Let’s take a look at another example: replacing a Fragment in a view hierarchy
**扩展功能可以提升代码的可读性** 来看看另一个例子:在 view 的层次结构中替换 Fragment
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

扩展功能->扩展函数
代码的可读性句号


Within several places in Topeka, I am relying on an `OnLayoutChangeListener` to inject behaviour. In a pre-Kotlin world this would usually result in an anonymous class, with some duplicated code.
Topeka 里我有好几次都是依靠 `OnLayoutChangeListener` 来实现 inject behaviour. 如果没有 Kotlin ,这样做会生成一个包含重复代码的匿名类。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inject behaviour.
注入行为句号


That may be because I am still new to the language or because there hasn’t been that much investment in gathering and publicising these yet. Maybe there is a collection which I am yet to come across, but it seems that there is quite some space for platform specific idioms.
If you’re aware of collections like this, please add them to the comments.
这主要是因为我对这门语言还不是很熟,或者说我还没有花太大精力去研究这方面。也许是因为我还没有碰到这类情况,but it seems that there is quite some space for platform specific idioms.(这句不知道怎么翻译。。)如果你知道这种情况,请在评论区补充。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gathering and publicising these yet这句没翻译吧


That may be because I am still new to the language or because there hasn’t been that much investment in gathering and publicising these yet. Maybe there is a collection which I am yet to come across, but it seems that there is quite some space for platform specific idioms.
If you’re aware of collections like this, please add them to the comments.
这主要是因为我对这门语言还不是很熟,或者说我还没有花太大精力去研究这方面。也许是因为我还没有碰到这类情况,but it seems that there is quite some space for platform specific idioms.(这句不知道怎么翻译。。)如果你知道这种情况,请在评论区补充。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

platform可能是kotlin版本。。
某个版本就有编译问题

Copy link
Contributor

@Zhiw Zhiw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好多地方没能指出,惭愧惭愧,以后晚上头脑不清醒的时候不能搞校对。

And I couldn’t help but think of a very fitting quote:
我发现自动转换会生成很多的 `?` `!!`
这些符号是用来定义可为空的数值和判断其是否不为空值的。他们可能会导致 `空指针异常`。
我突然想起一条很恰当的名言
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不禁想到一条很恰当的名言(引用?引述?)

In these and other cases you will still have to check, whether something is `null`. Using `*supportActionBar*?.setDisplayShowTitleEnabled(false)` only executes the part after the question mark if there is a `supportActionBar`.
This means a lot less `if` statement based null checks. 🔥
在其他情况下你还是得检查是否也 `null` 存在。如果有 `supportActionBar` 存在的话, `*supportActionBar*?.setDisplayShowTitleEnabled(false)` 只能执行问号以后的代码。
这意味着大部分 `if` 语句不会依靠空置检查。 🔥
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这意味着更少的基于 null 检查的 if 条件声明

@wilsonandusa
Copy link
Contributor Author

一改完成 @sqrthree

Copy link
Member

@linhe0x0 linhe0x0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一些小问题需要调整下哈。


At Google I/O 2017 we announced official [support for the Kotlin programming language](https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/). That’s when I started migrating the code base away from Java, **learning Kotlin on the way.**
2017年谷歌在开发者大会上官方宣布 [支持 Kotlin 编程语言](https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/)。从那时起,我便开始移植 Java 代码,**同时在过程中学习Kotlin。**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

英文前后需要添加空格。


> Not that this migration was necessary from a technological standpoint. The app is solid as it stands, but mainly to satisfy my curiosity; Topeka serving as my vehicle to learn a new language.
> 从技术角度上来讲,这次的移植并不是必须的,程序本身是十分稳定的,而(这次移植)主要是为了满足我的好奇心。Topeka成为了我学习一门新语言的媒介。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

英文前后需要添加空格。

With Kotlin, [extension functions](https://kotlinlang.org/docs/reference/extensions.html) solve that once and for all:
有时我们会在 Topeka 里通过
`Parcel` 传递 boolean。Android 框架的 API 无法直接支持这项功能。在一开始实现这项功能的时候必须调用一个功能类函数例如`ParcelableHelper.writeBoolean(parcel, value)`
如果使用 Kotlin, [ 扩展函数](https://kotlinlang.org/docs/reference/extensions.html)可以解决之前的难题:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『扩展函数』前面的逗号请使用全角符号,另外不需要空格。

With Kotlin, [extension functions](https://kotlinlang.org/docs/reference/extensions.html) solve that once and for all:
有时我们会在 Topeka 里通过
`Parcel` 传递 boolean。Android 框架的 API 无法直接支持这项功能。在一开始实现这项功能的时候必须调用一个功能类函数例如`ParcelableHelper.writeBoolean(parcel, value)`
如果使用 Kotlin, [ 扩展函数](https://kotlinlang.org/docs/reference/extensions.html)可以解决之前的难题:

```
import android.os.Parcel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释也要翻译。

@@ -254,44 +248,45 @@ fun Parcel.writeBoolean(toWrite: Boolean) = writeByte(if (toWrite) 1 else 0)
fun Parcel.readBoolean() = 1 == this.readByte()
```

Having this written in one place, makes it possible to call `parcel.writeBoolean(value)` and `parcel.readBoolean()` directly, as if it were part of the framework. If Android Studio would not highlight extension functions differently, they were almost not noticeable.
当写好以上代码之后,我们可以把
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要换行?


With Kotlin, an extension function makes it possible to simply call `replaceFragment(R.id.container, MyFragment())` to replace a fragment within any `FragmentActivity` within the project, by adding this code:
如果使用 Kotlin,当我们在 `FragmentActivity` 中需要替换 Fragment 的时候,只需要使用如下代码调用`replaceFragment(R.id.container, MyFragment())` 即可:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaceFragment(R.id.container, MyFragment()) 前面缺少一个空格。

@linhe0x0
Copy link
Member

译文的质量还需要两位校对者来把关哈。

Copy link
Contributor

@Zhiw Zhiw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

稍微补充一点点


At Google I/O 2017 we announced official [support for the Kotlin programming language](https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/). That’s when I started migrating the code base away from Java, **learning Kotlin on the way.**
2017年谷歌在开发者大会上官方宣布 [支持 Kotlin 编程语言](https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/)。从那时起,我便开始移植 Java 代码,**同时在过程中学习Kotlin。**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phxnirvana 作者就是 Google 大佬:joy:
不过这里不碍事啦


### Tests ease your mind
搭配使用单元和整合测试的好处很多。在绝大多数情况下,这些测试是用来确保当前修改没有损坏现有的功能。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『integration tests』 ->『 集成测试』

@wilsonandusa
Copy link
Contributor Author

二改完成 @sqrthree

@linhe0x0
Copy link
Member

@wilsonandusa 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。

@linhe0x0 linhe0x0 merged commit ec6a5f5 into xitu:master Aug 21, 2017
@wilsonandusa
Copy link
Contributor Author

atuooo added a commit to atuooo/gold-miner that referenced this pull request Aug 23, 2017
…slate

* 'master' of https://github.com/xitu/gold-miner: (178 commits)
  🚀 添加文章『关于 React Router 4 的一切』到文章列表
  🚀 添加文章『为什么我们渴求女性来设计 AI 』到文章列表
  🚀 添加文章『如何在 Webpack 2 中使用 tree-shaking』到文章列表
  🚀 添加文章『Redux 有多棒?』到文章列表
  🚀 添加文章『别再使用图片轮播了』到文章列表
  🚀 添加文章『将 Android 项目迁移到 Kotlin 语言』到文章列表
  🚀 添加文章『巧用 ARKit 和 SpriteKit 从零开始做 AR 游戏』到文章列表
  别再使用图片轮播了 (xitu#2073)
  Update how-to-do-proper-tree-shaking-in-webpack-2.md (xitu#2100)
  为什么我们渴求女性来设计 AI (xitu#2074)
  如何在 webpack2 中使用 tree-shaking (xitu#2076)
  怎么写出完美的错误消息 (xitu#2080)
  巧用 ARKit 和 SpriteKit 从零开始做 AR 游戏 (xitu#2043)
  将 Android 程序移植为 Kotlin 程序 (xitu#2039)
  Redux 有多棒? (xitu#2003)
  🚀 添加文章『函数式响应编程入门指南』到文章列表
  🚀 添加文章『虚拟现实是如何改变用户体验的:从原型到设备的设计』到文章列表
  ✨ Create angular-vs-react-which-is-better-for-web-development.md
  ✨ Create evolving-the-facebook-news-feed-to-serve-you-better.md
  ✨ Create why-context-value-matters-and-how-to-improve-it.md
  ...
cdadar pushed a commit to cdadar/gold-miner that referenced this pull request Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

将 Android 程序移植为 Kotlin 程序
4 participants