Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(816)

Side by Side Diff: lib/i18n/intl.dart

Issue 10827227: Start moving non-platform libraries from lib/ to pkg/ . (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library('intl');
6
7 #import('dart:web');
8
9 #source('intl_message.dart');
10 #source('date_format.dart');
11 #source('bidi_formatter.dart');
12 #source('bidi_utils.dart');
13
14 /**
15 * Internationalization object providing access to message formatting objects,
16 * date formatting, parsing, bidirectional text relative to a specific locale.
17 */
18
19 class Intl {
20
21 /**
22 * String indicating the locale code with which the message is to be
23 * formatted (such as en-CA).
24 */
25 static String _locale;
26
27 IntlMessage intlMsg;
28
29 DateFormat date;
30
31 /**
32 * Constructor optionally [_locale] for specifics of the language
33 * locale to be used, otherwise, we will attempt to infer it (acceptable if
34 * Dart is running on the client, we can infer from the browser/client
35 * preferences).
36 */
37 Intl([a_locale]) {
38 _locale = a_locale;
39 if (_locale == null) _locale = _getDefaultLocale();
40 intlMsg = new IntlMessage(_locale);
41 date = new DateFormat(_locale);
42 }
43
44 /**
45 * Create a message that can be internationalized. It takes a
46 * [message_str] that will be translated, which may be interpolated
47 * based on one or more variables, a [desc] providing a description of usage
48 * for the [message_str], and a map of [examples] for each data element to be
49 * substituted into the message. For example, if message="Hello, $name", then
50 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
51 * if the locale is not easily detectable, explicitly pass [locale].
52 * The values of [desc] and [examples] are not used at run-time but are only
53 * made available to the translators, so they MUST be simple Strings available
54 * at compile time: no String interpolation or concatenation.
55 * The expected usage of this is inside a function that takes as parameters
56 * the variables used in the interpolated string, and additionally also a
57 * locale (optional).
58 */
59 static String message(String message_str, [final String desc='',
60 final Map examples=const {}, String locale='']) {
61 return message_str;
62 }
63
64 /**
65 * Support method for message formatting. Select the correct plural form from
66 * [cases] given [howMany].
67 */
68 static String plural(var howMany, Map cases, [num offset=0]) {
69 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others!
70 return select(howMany.toString(), cases);
71 }
72
73 /**
74 * Format the given function with a specific [locale], given a
75 * [msg_function] that takes no parameters and returns a String. We
76 * basically delay calling the message function proper until after the proper
77 * locale has been set.
78 */
79 static String withLocale(String locale, Function msg_function) {
80 // We have to do this silliness because Locale is not known at compile time,
81 // but must be a static variable.
82 if (_locale == null) _locale = _getDefaultLocale();
83 var oldLocale = _locale;
84 _locale = locale;
85 var result = msg_function();
86 _locale = oldLocale;
87 return result;
88 }
89
90 /**
91 * Support method for message formatting. Select the correct exact (gender,
92 * usually) form from [cases] given the user [choice].
93 */
94 static String select(String choice, Map cases) {
95 if (cases.containsKey(choice)) {
96 return cases[choice];
97 } else if (cases.containsKey('other')){
98 return cases['other'];
99 } else {
100 return '';
101 }
102 }
103
104 /**
105 * Helper to detect the locale as defined at runtime.
106 */
107 static String _getDefaultLocale() {
108 // TODO(efortuna): Detect the default locale given the user preferences.
109 // Yay, hard-coding for now!
110 return 'en-US';
111 }
112
113 /**
114 * Accessor for the current locale. This should always == the default locale,
115 * unless for some reason this gets called inside a message that resets the
116 * locale.
117 */
118 static String getCurrentLocale() {
119 return _locale;
120 }
121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698