add translation for dart.

This commit is contained in:
hanguokai 2013-08-10 06:35:01 +08:00
parent 8d0a2e6b79
commit c5c76b4a5d

View File

@ -9,7 +9,7 @@ translators:
Dart 是编程语言王国的新人。 Dart 是编程语言王国的新人。
它借鉴了许多其他主流语言,并且不会偏离它的兄弟语言 JavaScript 太多。 它借鉴了许多其他主流语言,并且不会偏离它的兄弟语言 JavaScript 太多。
就像 JavaScriptDart 的目标是提供良好的浏览器集成。 就像 JavaScript 一样Dart 的目标是提供良好的浏览器集成。
Dart 最有争议的特性必然是它的可选类型。 Dart 最有争议的特性必然是它的可选类型。
@ -17,15 +17,14 @@ Dart 最有争议的特性必然是它的可选类型。
import "dart:collection"; import "dart:collection";
import "dart:math" as DM; import "dart:math" as DM;
// Welcome to Learn Dart in 15 minutes. http://www.dartlang.org/ // 欢迎进入15分钟的 Dart 学习。 http://www.dartlang.org/
// This is an executable tutorial. You can run it with Dart or on // 这是一个可实际执行的向导。你可以用 Dart 运行它
// the Try Dart! site if you copy/paste it there. http://try.dartlang.org/ // 或者在线执行! 可以把代码复制/粘贴到这个网站。 http://try.dartlang.org/
// Function declaration and method declaration look the same. Function // 函数声明和方法声明看起来一样。
// declarations can be nested. The declaration takes the form of // 函数声明可以嵌套。声明使用这种 name() {} 的形式,
// name() {} or name() => singleLineExpression; // 或者 name() => 单行表达式; 的形式。
// The fat arrow function declaration has an implicit return for the result of // 右箭头的声明形式会隐式地返回表达式的结果。
// the expression.
example1() { example1() {
example1nested1() { example1nested1() {
example1nested2() => print("Example1 nested 1 nested 2"); example1nested2() => print("Example1 nested 1 nested 2");
@ -34,7 +33,7 @@ example1() {
example1nested1(); example1nested1();
} }
// Anonymous functions don't include a name. // 匿名函数没有函数名。
example2() { example2() {
example2nested1(fn) { example2nested1(fn) {
fn(); fn();
@ -42,21 +41,20 @@ example2() {
example2nested1(() => print("Example2 nested 1")); example2nested1(() => print("Example2 nested 1"));
} }
// When a function parameter is declared, the declaration can include the // 当声明函数类型的参数的时候,声明中可以包含
// number of parameters the function takes by specifying the names of the // 函数参数需要的参数,指定所需的参数名即可。
// parameters it takes.
example3() { example3() {
example3nested1(fn(informSomething)) { example3nested1(fn(informSomething)) {
fn("Example3 nested 1"); fn("Example3 nested 1");
} }
example3planB(fn) { // Or don't declare number of parameters. example3planB(fn) { // 或者不声明函数参数的参数
fn("Example3 plan B"); fn("Example3 plan B");
} }
example3nested1((s) => print(s)); example3nested1((s) => print(s));
example3planB((s) => print(s)); example3planB((s) => print(s));
} }
// Functions have closure access to outer variables. // 函数有可以访问到外层变量的闭包。
var example4Something = "Example4 nested 1"; var example4Something = "Example4 nested 1";
example4() { example4() {
example4nested1(fn(informSomething)) { example4nested1(fn(informSomething)) {
@ -65,8 +63,8 @@ example4() {
example4nested1((s) => print(s)); example4nested1((s) => print(s));
} }
// Class declaration with a sayIt method, which also has closure access // 下面这个包含 sayIt 方法的类声明,同样有一个可以访问外层变量的闭包,
// to the outer variable as though it were a function as seen before. // 就像前面的函数一样。
var example5method = "Example5 sayIt"; var example5method = "Example5 sayIt";
class Example5Class { class Example5Class {
sayIt() { sayIt() {
@ -74,14 +72,14 @@ class Example5Class {
} }
} }
example5() { example5() {
// Create an anonymous instance of the Example5Class and call the sayIt // 创建一个 Example5Class 类的匿名实例,
// method on it. // 并调用它的 sayIt 方法。
new Example5Class().sayIt(); new Example5Class().sayIt();
} }
// Class declaration takes the form of class name { [classBody] }. // 类的声明使用这种形式 class name { [classBody] }.
// Where classBody can include instance methods and variables, but also // classBody 中可以包含实例方法和变量,
// class methods and variables. // 还可以包含类方法和变量。
class Example6Class { class Example6Class {
var example6InstanceVariable = "Example6 instance variable"; var example6InstanceVariable = "Example6 instance variable";
sayIt() { sayIt() {
@ -92,7 +90,7 @@ example6() {
new Example6Class().sayIt(); new Example6Class().sayIt();
} }
// Class methods and variables are declared with "static" terms. // 类方法和变量使用 static 关键词声明。
class Example7Class { class Example7Class {
static var example7ClassVariable = "Example7 class variable"; static var example7ClassVariable = "Example7 class variable";
static sayItFromClass() { static sayItFromClass() {
@ -107,11 +105,10 @@ example7() {
new Example7Class().sayItFromInstance(); new Example7Class().sayItFromInstance();
} }
// Literals are great, but there's a restriction for what literals can be // 字面量非常方便,但是对于在函数/方法的外层的字面量有一个限制,
// outside of function/method bodies. Literals on the outer scope of class // 类的外层或外面的字面量必需是常量。
// or outside of class have to be constant. Strings and numbers are constant // 字符串和数字默认是常量。
// by default. But arrays and maps are not. They can be made constant by // 但是 array 和 map 不是。他们需要用 "const" 声明为常量。
// declaring them "const".
var example8A = const ["Example8 const array"], var example8A = const ["Example8 const array"],
example8M = const {"someKey": "Example8 const map"}; example8M = const {"someKey": "Example8 const map"};
example8() { example8() {
@ -119,9 +116,9 @@ example8() {
print(example8M["someKey"]); print(example8M["someKey"]);
} }
// Loops in Dart take the form of standard for () {} or while () {} loops, // Dart 中的循环使用标准的 for () {} 或 while () {} 的形式,
// slightly more modern for (.. in ..) {}, or functional callbacks with many // 以及更加现代的 for (.. in ..) {} 的形式, 或者
// supported features, starting with forEach. // 以 forEach 开头并具有许多特性支持的函数回调的形式。
var example9A = const ["a", "b"]; var example9A = const ["a", "b"];
example9() { example9() {
for (var i = 0; i < example9A.length; i++) { for (var i = 0; i < example9A.length; i++) {
@ -138,7 +135,7 @@ example9() {
example9A.forEach((e) => print("Example9 forEach loop '${e}'")); example9A.forEach((e) => print("Example9 forEach loop '${e}'"));
} }
// To loop over the characters of a string or to extract a substring. // 遍历字符串中的每个字符或者提取其子串。
var example10S = "ab"; var example10S = "ab";
example10() { example10() {
for (var i = 0; i < example10S.length; i++) { for (var i = 0; i < example10S.length; i++) {
@ -149,14 +146,14 @@ example10() {
} }
} }
// Int and double are the two supported number formats. // 支持两种数字格式 int 和 double 。
example11() { example11() {
var i = 1 + 320, d = 3.2 + 0.01; var i = 1 + 320, d = 3.2 + 0.01;
print("Example11 int ${i}"); print("Example11 int ${i}");
print("Example11 double ${d}"); print("Example11 double ${d}");
} }
// DateTime provides date/time arithmetic. // DateTime 提供了日期/时间的算法。
example12() { example12() {
var now = new DateTime.now(); var now = new DateTime.now();
print("Example12 now '${now}'"); print("Example12 now '${now}'");
@ -164,7 +161,7 @@ example12() {
print("Example12 tomorrow '${now}'"); print("Example12 tomorrow '${now}'");
} }
// Regular expressions are supported. // 支持正则表达式。
example13() { example13() {
var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$"); var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$");
match(s) { match(s) {
@ -178,8 +175,8 @@ example13() {
match(s2); match(s2);
} }
// Boolean expressions need to resolve to either true or false, as no // 布尔表达式必需被解析为 true 或 false
// implicit conversions are supported. // 因为不支持隐式转换。
example14() { example14() {
var v = true; var v = true;
if (v) { if (v) {
@ -188,17 +185,17 @@ example14() {
v = null; v = null;
try { try {
if (v) { if (v) {
// Never runs // 不会执行
} else { } else {
// Never runs // 不会执行
} }
} catch (e) { } catch (e) {
print("Example14 null value causes an exception: '${e}'"); print("Example14 null value causes an exception: '${e}'");
} }
} }
// try/catch/finally and throw are used for exception handling. // try/catch/finally 和 throw 语句用于异常处理。
// throw takes any object as parameter; // throw 语句可以使用任何对象作为参数。
example15() { example15() {
try { try {
try { try {
@ -214,8 +211,8 @@ example15() {
} }
} }
// To be efficient when creating a long string dynamically, use // 要想有效地动态创建长字符串,
// StringBuffer. Or you could join a string array. // 应该使用 StringBuffer。 或者 join 一个字符串的数组。
example16() { example16() {
var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e; var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
for (e in a) { sb.write(e); } for (e in a) { sb.write(e); }
@ -224,8 +221,8 @@ example16() {
print("Example16 join string array '${a.join()}'"); print("Example16 join string array '${a.join()}'");
} }
// Strings can be concatenated by just having string literals next to // 字符串连接只需让相邻的字符串字面量挨着,
// one another with no further operator needed. // 不需要额外的操作符。
example17() { example17() {
print("Example17 " print("Example17 "
"concatenate " "concatenate "
@ -233,44 +230,41 @@ example17() {
"just like that"); "just like that");
} }
// Strings have single-quote or double-quote for delimiters with no // 字符串使用单引号或双引号做分隔符,二者并没有实际的差异。
// actual difference between the two. The given flexibility can be good // 这种灵活性可以很好地避免内容中需要转义分隔符的情况。
// to avoid the need to escape content that matches the delimiter being // 例如,字符串内容里的 HTML 属性使用了双引号。
// used. For example, double-quotes of HTML attributes if the string
// contains HTML content.
example18() { example18() {
print('Example18 <a href="etc">' print('Example18 <a href="etc">'
"Don't can't I'm Etc" "Don't can't I'm Etc"
'</a>'); '</a>');
} }
// Strings with triple single-quotes or triple double-quotes span // 用三个单引号或三个双引号表示的字符串
// multiple lines and include line delimiters. // 可以跨越多行,并且包含行分隔符。
example19() { example19() {
print('''Example19 <a href="etc"> print('''Example19 <a href="etc">
Example19 Don't can't I'm Etc Example19 Don't can't I'm Etc
Example19 </a>'''); Example19 </a>''');
} }
// Strings have the nice interpolation feature with the $ character. // 字符串可以使用 $ 字符插入内容。
// With $ { [expression] }, the return of the expression is interpolated. // 使用 $ { [expression] } 的形式,表达式的值会被插入到字符串中。
// $ followed by a variable name interpolates the content of that variable. // $ 跟着一个变量名会插入变量的值。
// $ can be escaped like so \$ to just add it to the string instead. // 如果要在字符串中插入 $ ,可以使用 \$ 的转义形式代替。
example20() { example20() {
var s1 = "'\${s}'", s2 = "'\$s'"; var s1 = "'\${s}'", s2 = "'\$s'";
print("Example20 \$ interpolation ${s1} or $s2 works."); print("Example20 \$ interpolation ${s1} or $s2 works.");
} }
// Optional types allow for the annotation of APIs and come to the aid of // 可选类型允许作为 API 的标注,并且可以辅助 IDE
// IDEs so the IDEs can better refactor, auto-complete and check for // 这样 IDE 可以更好地提供重构、自动完成和错误检测功能。
// errors. So far we haven't declared any types and the programs have // 目前为止我们还没有声明任何类型,并且程序运行地很好。
// worked just fine. In fact, types are disregarded during runtime. // 事实上,类型在运行时会被忽略。
// Types can even be wrong and the program will still be given the // 类型甚至可以是错的,并且程序依然可以执行,
// benefit of the doubt and be run as though the types didn't matter. // 好像和类型完全无关一样。
// There's a runtime parameter that checks for type errors which is // 有一个运行时参数可以让程序进入检查模式,它会在运行时检查类型错误。
// the checked mode, which is said to be useful during development time, // 这在开发时很有用,但是由于增加了额外的检查会使程序变慢,
// but which is also slower because of the extra checking and is thus // 因此应该避免在部署时使用。
// avoided during deployment runtime.
class Example21 { class Example21 {
List<String> _names; List<String> _names;
Example21() { Example21() {
@ -293,7 +287,7 @@ void example21() {
print("Example21 names '${o.names}' and length '${o.length}'"); print("Example21 names '${o.names}' and length '${o.length}'");
} }
// Class inheritance takes the form of class name extends AnotherClassName {}. // 类的继承形式是 class name extends AnotherClassName {} 。
class Example22A { class Example22A {
var _name = "Some Name!"; var _name = "Some Name!";
get name => _name; get name => _name;
@ -304,13 +298,13 @@ example22() {
print("Example22 class inheritance '${o.name}'"); print("Example22 class inheritance '${o.name}'");
} }
// Class mixin is also available, and takes the form of // 类也可以使用 mixin 的形式
// class name extends SomeClass with AnotherClassName {}. // class name extends SomeClass with AnotherClassName {}.
// It's necessary to extend some class to be able to mixin another one. // 必需继承某个类才能 mixin 另一个类。
// The template class of mixin cannot at the moment have a constructor. // 当前 mixin 的模板类不能有构造函数。
// Mixin is mostly used to share methods with distant classes, so the // Mixin 主要是用来和辅助的类共享方法的,
// single inheritance doesn't get in the way of reusable code. // 这样单一继承就不会影响代码复用。
// Mixins follow the "with" statement during the class declaration. // Mixin 声明在类定义的 "with" 关键词后面。
class Example23A {} class Example23A {}
class Example23Utils { class Example23Utils {
addTwo(n1, n2) { addTwo(n1, n2) {
@ -329,10 +323,9 @@ example23() {
print("Example23 addTwo(1, 2) results in '${r2}'"); print("Example23 addTwo(1, 2) results in '${r2}'");
} }
// The Class constructor method uses the same name of the class and // 类的构造函数名和类名相同,形式为
// takes the form of SomeClass() : super() {}, where the ": super()" // SomeClass() : super() {}, 其中 ": super()" 的部分是可选的,
// part is optional and it's used to delegate constant parameters to the // 它用来传递参数给父类的构造函数。
// super-parent's constructor.
class Example24A { class Example24A {
var _value; var _value;
Example24A({value: "someValue"}) { Example24A({value: "someValue"}) {
@ -350,9 +343,9 @@ example24() {
print("Example24 calling super during constructor '${o2.value}'"); print("Example24 calling super during constructor '${o2.value}'");
} }
// There's a shortcut to set constructor parameters in case of simpler classes. // 对于简单的类,有一种设置构造函数参数的快捷方式。
// Just use the this.parameterName prefix and it will set the parameter on // 只需要使用 this.parameterName 的前缀,
// an instance variable of same name. // 它就会把参数设置为同名的实例变量。
class Example25 { class Example25 {
var value, anotherValue; var value, anotherValue;
Example25({this.value, this.anotherValue}); Example25({this.value, this.anotherValue});
@ -363,9 +356,9 @@ example25() {
"'${o.anotherValue}'"); "'${o.anotherValue}'");
} }
// Named parameters are available when declared between {}. // 可以在大括号 {} 中声明命名参数。
// Parameter order can be optional when declared between {}. // 大括号 {} 中声明的参数的顺序是随意的。
// Parameters can be made optional when declared between []. // 在中括号 [] 中声明的参数也是可选的。
example26() { example26() {
var _name, _surname, _email; var _name, _surname, _email;
setConfig1({name, surname}) { setConfig1({name, surname}) {
@ -385,13 +378,11 @@ example26() {
"email '${_email}'"); "email '${_email}'");
} }
// Variables declared with final can only be set once. // 使用 final 声明的变量只能被设置一次。
// In case of classes, final instance variables can be set via constant // 在类里面final 实例变量可以通过常量的构造函数参数设置。
// constructor parameter.
class Example27 { class Example27 {
final color1, color2; final color1, color2;
// A little flexibility to set final instance variables with syntax // 更灵活一点的方法是在冒号 : 后面设置 final 实例变量。
// that follows the :
Example27({this.color1, color2}) : color2 = color2; Example27({this.color1, color2}) : color2 = color2;
} }
example27() { example27() {
@ -400,11 +391,11 @@ example27() {
print("Example27 color is '${o.color1}' and '${o.color2}'"); print("Example27 color is '${o.color1}' and '${o.color2}'");
} }
// To import a library, use import "libraryPath" or if it's a core library, // 要导入一个库,使用 import "libraryPath" 的形式,或者如果要导入的是
// import "dart:libraryName". There's also the "pub" package management with // 核心库使用 import "dart:libraryName" 。还有一个称为 "pub" 的包管理工具,
// its own convention of import "package:packageName". // 它使用 import "package:packageName" 的约定形式。
// See import "dart:collection"; at the top. Imports must come before // 看下这个文件顶部的 import "dart:collection"; 语句。
// other code declarations. IterableBase comes from dart:collection. // 导入语句必需在其它代码声明之前出现。IterableBase 来自于 dart:collection 。
class Example28 extends IterableBase { class Example28 extends IterableBase {
var names; var names;
Example28() { Example28() {
@ -417,11 +408,11 @@ example28() {
o.forEach((name) => print("Example28 '${name}'")); o.forEach((name) => print("Example28 '${name}'"));
} }
// For control flow we have: // 对于控制流语句,我们有:
// * standard switch with must break statements // * 必需带 break 的标准 switch 语句
// * if-else if-else and ternary ..?..:.. operator // * if-else 和三元操作符 ..?..:..
// * closures and anonymous functions // * 闭包和匿名函数
// * break, continue and return statements // * break, continue 和 return 语句
example29() { example29() {
var v = true ? 30 : 60; var v = true ? 30 : 60;
switch (v) { switch (v) {
@ -448,12 +439,12 @@ example29() {
} else { } else {
continue; continue;
} }
// Never gets here. // 不会到这里。
} }
} }
// Parse int, convert double to int, or just keep int when dividing numbers // 解析 int把 double 转成 int或者使用 ~/ 操作符在除法计算时仅保留整数位。
// by using the ~/ operation. Let's play a guess game too. // 让我们也来场猜数游戏吧。
example30() { example30() {
var gn, tooHigh = false, var gn, tooHigh = false,
n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0; n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0;
@ -481,11 +472,9 @@ example30() {
} }
} }
// Programs have only one entry point in the main function. // 程序的唯一入口点是 main 函数。
// Nothing is expected to be executed on the outer scope before a program // 在程序开始执行 main 函数之前,不期望执行任何外层代码。
// starts running with what's in its main function. // 这样可以帮助程序更快地加载,甚至仅惰性加载程序启动时需要的部分。
// This helps with faster loading and even lazily loading of just what
// the program needs to startup with.
main() { main() {
print("Learn Dart in 15 minutes!"); print("Learn Dart in 15 minutes!");
[example1, example2, example3, example4, example5, example6, example7, [example1, example2, example3, example4, example5, example6, example7,
@ -500,8 +489,8 @@ main() {
## 延伸阅读 ## 延伸阅读
Dart 有一个全面的网站。它涵盖了 API 参考、入门向导、文章以及更多, Dart 有一个综合性网站。它涵盖了 API 参考、入门向导、文章以及更多,
包括一个有用的 Dart 在线试用 还包括一个有用的在线试用 Dart 页面
http://www.dartlang.org/ http://www.dartlang.org/
http://try.dartlang.org/ http://try.dartlang.org/