Files
sdk/pkg/intl/example/basic/basic_example.dart
T
alanknight@google.com 0446767578 Adds facilities for extracting Intl.message calls and generating code from translations
This adds both general libraries for extracting messages and generating translations and scripts for using them with a trivial translation format. It removes the previous mechanism in message_lookup_local that relied on mirrors and traversing libraries in favor of one with a more explicit lookup by library that's consistent with the generated code. It also changes the code in basic_example to use that.

It also includes a couple of other changes. It now uses pathos for path manipulation, and the data_directory.dart hack for running tests from multiple different places was cleaned up and made more general. In addition, Intl.message now returns a string rather than a Future, which was changed as part of the libv2 integration.

This version does not yet support the plural syntax, as it disallows any interpolations that aren't just replacing a variable name. It also doesn't support named arguments to functions containing Intl.message, and it doesn't yet have a mechanism for designating sections of a text as not being subject to translation.

Finally, it relies on analyzer-experimental, which has a different name in the repository and in pub. So this version will run in the repository but will not run if used from pub. Bug 9071 asks for the names to be made consistent.

Review URL: https://codereview.chromium.org//12733003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@20047 260f80e4-7a28-3924-810f-c04153c831b5
2013-03-14 20:49:26 +00:00

75 lines
3.4 KiB
Dart

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/**
* This provides a basic example of internationalization usage. It uses the
* local variant of all the facilities, meaning that libraries with the
* data for all the locales are directly imported by the program. Once lazy
* loading is available, we expect this to be the preferred mode, with
* the initialization code actually loading the specific libraries needed.
*
* This defines messages for an English locale directly in the program and
* has separate libraries that define German and Thai messages that say more or
* less the same thing, and prints the message with the date and time in it
* formatted appropriately for the locale.
*/
library intl_basic_example;
import 'dart:async';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';
import 'messages_all.dart';
/**
* In order to use this both as an example and as a test case, we pass in
* the function for what we're going to do with the output. For a simple
* example we just pass in [print] and for tests we pass in a function that
* adds it a list to be verified.
*/
Function doThisWithTheOutput;
void setup(Function program, Function output) {
// Before we use any messages or use date formatting for a locale we must
// call their initializtion messages, which are asynchronous, since they
// might be reading information from files or over the web. Since we are
// running here in local mode they will all complete immediately.
doThisWithTheOutput = output;
var germanDatesFuture = initializeDateFormatting('de_DE', null);
var thaiDatesFuture = initializeDateFormatting('th_TH', null);
var germanMessagesFuture = initializeMessages('de_DE');
var thaiMessagesFuture = initializeMessages('th_TH');
Future.wait([germanDatesFuture, thaiDatesFuture, germanMessagesFuture,
thaiMessagesFuture]).then(program);
}
// Because the initialization messages return futures we split out the main
// part of our program into a separate function that runs once all the
// futures have completed. We are passed the collection of futures, but we
// don't need to use them, so ignore the parameter.
runProgram(List<Future> _) {
var aDate = new DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
var de = new Intl('de_DE');
var th = new Intl('th_TH');
// This defines a message that can be internationalized. It is written as a
// function that returns the result of an Intl.message call. The primary
// parameter is a string that may use interpolation.
runAt(time, date) =>
Intl.message('Ran at $time on $date', name: 'runAt', args: [time, date]);
printForLocale(aDate, new Intl(), runAt);
printForLocale(aDate, de, runAt);
printForLocale(aDate, th, runAt);
// Example making use of the return value from withLocale;
var returnValue = Intl.withLocale(th.locale, () => runAt('now', 'today'));
doThisWithTheOutput(returnValue);
}
printForLocale(aDate, intl, operation) {
var hmsFormat = intl.date().add_Hms();
var dayFormat = intl.date().add_yMMMMEEEEd();
var time = hmsFormat.format(aDate);
var day = dayFormat.format(aDate);
Intl.withLocale(intl.locale, () => doThisWithTheOutput(operation(time,day)));
}