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

Working with dates

The ISO 8601 standard for dates is 'Year Month Day', and naturally, no country in the world uses this standard day to day! A typical variation is to have the month or day first, and then the separator character is used between digits in shorthand.

The intl package can help provide the appropriate format and translations for each language. Translation strings are required too as the name of the day or month may be required.

Date handling is implemented in the SlideShowApp class:

 import 'package:intl/date_symbol_data_local.dart';

A field is used to keep a reference to the active DateFormat object:

DateFormat slideDateFormatter;

We will consider how this formatter is initialized and kept up to date with the changes made to the users language.

Formatting for the locale

The locale for the date formatter will need to be updated whenever the interface language is changed:

    qs("#interfaceLang").onChange.listen((e){
      var lang = qs("#interfaceLang").value;
      setInterfaceLang(lang);
      langInterface = lang;
      initDefaultDate();
    });

When a locale has been requested, the appropriate DateFormat object needs to be created:

  initDefaultDate() {
    initializeDateFormatting(langInterface, null).then((d) {
      slideDateFormatter = new DateFormat.yMMMMEEEEd(langInterface);
    });
  }

Once this is in place, it is possible to change the interface language using the combo box and get a date in the correct format and language.

Formatting for the locale

As a final thought on this topic, most web applications are not nearly as dynamic in their choice of language. Typically, an interface language selection is a per user configuration option that is set up for each session when the user logs in.

主站蜘蛛池模板: 淳化县| 凌海市| 昭通市| 奉化市| 临邑县| 颍上县| 盖州市| 唐山市| 萨迦县| 平邑县| 阿拉善左旗| 涿鹿县| 镇巴县| 广饶县| 乌拉特前旗| 搜索| 岐山县| 文登市| 布尔津县| 彭州市| 宁武县| 仙桃市| 宁城县| 会理县| 洱源县| 乌审旗| 本溪市| 丰镇市| 奉贤区| 怀化市| 瓮安县| 金塔县| 松原市| 高青县| 武穴市| 临桂县| 昭觉县| 南宁市| 永顺县| 西贡区| 高清|