官术网_书友最值得收藏!

Adding metadata

At the time of writing, some of the fullscreen APIs are marked as experimental in the Dart API documentation, and this is made clear in the documentation as well. By the time you read this, the fullscreen APIs should hopefully be stabilized and fully operational.

This is the normal process for new web browser features, which are eventually made stable. The adding of experimental APIs is likely to be a continuous process—it seems very unlikely that web browsers will ever be 100 percent complete! The onFullscreenChange Dart API documentation reads:

Experimental
ElementStream<Event> get onFullscreenChange
Stream of fullscreenchange events handled by this Element.

The source code for this part of the dart:html package has several annotations including the @experimental marker:

  /// Stream of `fullscreenchange` events handled by this [Element].
  @DomName('Element.onwebkitfullscreenchange')
  @DocsEditable()
  // https://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html
  @Experimental()
  ElementStream<Event> get onFullscreenChange => fullscreenChangeEvent.forElement(this);

Dart has a very flexible annotation system, and new systems can be created to add metadata to most of the components in Dart, such as classes, functions, and libraries. The pattern, as you probably already spotted, is used to start them with a @ sign followed by a call to the const constructor or a reference to a compile-time constant variable.

Creating a custom annotation

Let's create our own Temporary annotation that will mark classes as temporary to the design of the application and will allow a label to record which planned build the temporary classes are scheduled to be removed:

import 'dart:mirrors';

class Temporary {
  final String removalBuildLabel;
  const Temporary(this.removalBuildLabel);

  String toString() => removalBuildLabel;
}

@Temporary('Build1')
class TempWidgetTxt {}

@Temporary('Build2')
class TempWidgetWithGFX {}

void main() {
  ClassMirror classMirror1 = reflectClass(TempWidgetTxt);
  ClassMirror classMirror2 = reflectClass(TempWidgetWithGFX);

  List<InstanceMirror> metadata1 = classMirror1.metadata;
  print(metadata1.first.reflectee);

  List<InstanceMirror> metadata2 = classMirror2.metadata;
  print(metadata2.first.reflectee);
}

The Temporary constructor allows a label to be set. This field must be final (a single-assignment variable or field) if this class is to be used as an annotation. The main function calls reflectClass to obtain the details of the classes and then accesses this metadata and stores it in a list. The toString method allows the metadata to be the output via print:

Build1
Build2

Annotations are used extensively in the core Dart APIs, and in packages, they are used as the basis of frameworks, for example, web applications. They are not an obscure feature of the core SDK.

Translating the user interface text

My first ever professional software release did not involve any creation of code. It was a language update release for an existing package, and it was just as satisfying to mail out the CD-ROM (this was a while ago!) as something I had coded from scratch. Thankfully, it made me famous in Norway!

The translation of a software application is critical for its acceptance in some markets. This is even more true in the world of web applications. Dart has a package called intl that is used to help bring your applications to a global audience.

The presenter interface needs to be updated so that it displays the editor in French and Spanish. This will involve extracting the strings that we wish to use, obtaining translations, and integrating them back into the project.

The language will be selectable at runtime by user selection. The current OS interface language may not be the language that the user wishes to use the application in.

主站蜘蛛池模板: 彭山县| 汶上县| 铁岭市| 阳新县| 阿拉善盟| 孝义市| 大田县| 四子王旗| 胶州市| 交城县| 雷州市| 花莲县| 大庆市| 弋阳县| 长寿区| 屯门区| 保德县| 卓资县| 汨罗市| 黑水县| 晴隆县| 宜川县| 西峡县| 潼南县| 黎平县| 娱乐| 衡水市| 洪洞县| 房山区| 道真| 司法| 邻水| 宜州市| 泗水县| 玉田县| 永胜县| 青神县| 临洮县| 张家界市| 乳山市| 定兴县|