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

Side by Side Diff: pkg/intl/intl_standalone.dart

Issue 10907266: Find the system locale from the browser or OS (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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 /**
6 * This provides facilities for Internationalization that are only available
7 * when running standalone. You should import only one of this or
8 * intl_browser.dart. Right now the only thing provided here is finding
9 * the operating system locale.
10 */
11
12 #library("intl_standalone");
13
14 #import("dart:io");
15 #import("intl.dart");
16
17 // TODO(alanknight): The need to do this by forcing the user to specially
18 // import a particular library is a horrible hack, only done because there
19 // seems to be no graceful way to do this at all. Either mirror access on
20 // dart2js or the ability to do spawnUri in the browser would be promising
21 // as ways to get rid of this requirement.
22 /**
23 * Find the system locale, accessed via the appropriate system APIs, and
24 * set it as the default for internationalization operations in
25 * the [Intl.systemLocale] variable. To find it, we
26 * check the "LANG" environment variable on *nix, use the "systeminfo"
27 * command on Windows, and use "defaults read -g AppleLocale" on the Mac. This
Emily Fortuna 2012/09/17 21:26:38 rephrase the mac section to say you fall back to A
Alan Knight 2012/09/17 22:16:18 Done.
28 * is not an ideal version of getting a single system locale, even if that
29 * concept really made sense, but it's a reasonable first approximation that's
30 * not too difficult to get. If it can't find the locale information, it will
31 * not modify [Intl.systemLocale] and the Future will complete with null.
32 */
33 Future<String> findSystemLocale() {
34 // On *nix systems we expect this is an environment variable, which is the
35 // easiest thing to check. On a Mac the environment variable may be present
36 // so always check it first.
37 String baseLocale = _checkEnvironmentVariable();
38 if (baseLocale != null) return _setLocale(baseLocale);
39 if (Platform.operatingSystem == 'windows') {
40 return _getWindowsSystemInfo();
41 }
42 if (Platform.operatingSystem == 'macos') {
43 return _getAppleDefaults();
44 }
45 // We can't find anything, don't set the system locale and return null.
46 return new Future.immediate(null);
47 }
48
49 /**
50 * Check to see if we have a "LANG" environment variable we can use and return
51 * it if found. Otherwise return null;
52 */
53 String _checkEnvironmentVariable() {
54 try {
55 return Platform.environment['LANG'];
56 } catch (e) {};
57 return null;
58 }
59
60 /**
61 * Run the "defaults read -g AppleLocale" command and return the output in
62 * a future.
63 */
64 Future _getAppleDefaults() {
65 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']);
66 var myResult = p.chain((result) => _checkResult(result, _appleDefaultsRegex));
67 return myResult;
68 }
69
70 /**
71 * Run the "systemlocale" command and return the output in a future.
72 */
73 Future _getWindowsSystemInfo() {
74 var p = Process.run('systeminfo', []);
75 var myResult = p.chain((result) => _checkResult(result, _sysInfoRegex));
76 return myResult;
77 }
78
79 /**
80 * Regular expression to match the expected output of systeminfo on
81 * Windows. e.g. System Locale:<tab>en_US;English (United States)
82 */
83 RegExp _sysInfoRegex = const RegExp(@"System Locale:\t(\w\w-\w+);");
Emily Fortuna 2012/09/17 21:26:38 I could be persuaded otherwise, but I prefer havin
Alan Knight 2012/09/17 22:16:18 I prefer having the important bit at the top, whic
84
85 /**
86 * Regular expression to match the expected output of reading the defaults
87 * database for AppleLanguages on Mac systems.
88 * e.g. {
89 * en,
90 * "pt-PT",
91 * ...
92 */
93 RegExp _appleDefaultsRegex = const RegExp(@'(\w\w_\w+)');
94
95 /**
96 * Given [result], find its text and extract the locale from it using
97 * [regex], and set it as the system locale. If the process didn't run correctly
98 * then don't set the variable and return a future that completes with null.
99 */
100 Future<String> _checkResult(ProcessResult result, RegExp regex) {
101 if (result.exitCode != 0) return new Future.immediate(null);
102 var match = regex.firstMatch(result.stdout);
103 if (match == null) return new Future.immediate(null);
104 var locale = match.group(1);
105 _setLocale(locale);
106 return new Future.immediate(locale);
107 }
108
109 /**
110 * Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale].
111 */
112 Future<String> _setLocale(aLocale) {
113 Intl.systemLocale = Intl.canonicalizedLocale(aLocale);
114 return new Future.immediate(Intl.systemLocale);
115 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698