OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /// This provides facilities for Internationalization that are only available |
6 * This provides facilities for Internationalization that are only available | 6 /// when running standalone. You should import only one of this or |
7 * when running standalone. You should import only one of this or | 7 /// intl_browser.dart. Right now the only thing provided here is finding |
8 * intl_browser.dart. Right now the only thing provided here is finding | 8 /// the operating system locale. |
9 * the operating system locale. | |
10 */ | |
11 | 9 |
12 library intl_standalone; | 10 library intl_standalone; |
13 | 11 |
14 import "dart:async"; | 12 import "dart:async"; |
15 import "dart:io"; | 13 import "dart:io"; |
16 import "intl.dart"; | 14 import "intl.dart"; |
17 | 15 |
18 // TODO(alanknight): The need to do this by forcing the user to specially | 16 // TODO(alanknight): The need to do this by forcing the user to specially |
19 // import a particular library is a horrible hack, only done because there | 17 // import a particular library is a horrible hack, only done because there |
20 // seems to be no graceful way to do this at all. Either mirror access on | 18 // seems to be no graceful way to do this at all. Either mirror access on |
21 // dart2js or the ability to do spawnUri in the browser would be promising | 19 // dart2js or the ability to do spawnUri in the browser would be promising |
22 // as ways to get rid of this requirement. | 20 // as ways to get rid of this requirement. |
23 /** | 21 /// Find the system locale, accessed via the appropriate system APIs, and |
24 * Find the system locale, accessed via the appropriate system APIs, and | 22 /// set it as the default for internationalization operations in |
25 * set it as the default for internationalization operations in | 23 /// the [Intl.systemLocale] variable. |
26 * the [Intl.systemLocale] variable. To find it, we | |
27 * check the "LANG" environment variable on *nix, use the "systeminfo" | |
28 * command on Windows, and on the Mac check the environment variable "LANG", | |
29 * and if it's not found, use "defaults read -g AppleLocale". This | |
30 * is not an ideal way of getting a single system locale, even if that | |
31 * concept really made sense, but it's a reasonable first approximation that's | |
32 * not too difficult to get. If it can't find the locale information, it will | |
33 * not modify [Intl.systemLocale] and the Future will complete with null. | |
34 */ | |
35 Future<String> findSystemLocale() { | 24 Future<String> findSystemLocale() { |
36 // On *nix systems we expect this is an environment variable, which is the | 25 try { |
37 // easiest thing to check. On a Mac the environment variable may be present | 26 Intl.systemLocale = Intl.canonicalizedLocale(Platform.localeName); |
38 // so always check it first. We have no mechanism for this right now on | 27 } catch (e) { |
39 // Windows, so it will just fail. | 28 return new Future.value(); |
40 String baseLocale = _checkEnvironmentVariable(); | |
41 if (baseLocale != null) return _setLocale(baseLocale); | |
42 if (Platform.operatingSystem == 'macos') { | |
43 return _getAppleDefaults(); | |
44 } | 29 } |
45 // We can't find anything, don't set the system locale and return null. | |
46 return new Future.value(); | |
47 } | |
48 | |
49 /** | |
50 * Regular expression to match the expected output of reading the defaults | |
51 * database for AppleLanguages on Mac systems. | |
52 * e.g. { | |
53 * en, | |
54 * "pt-PT", | |
55 * ... | |
56 */ | |
57 RegExp _appleDefaultsRegex = new RegExp(r'((\w\w)_\w+)'); | |
58 | |
59 /** | |
60 * Check to see if we have a "LANG" environment variable we can use and return | |
61 * it if found. Otherwise return null; | |
62 */ | |
63 String _checkEnvironmentVariable() { | |
64 try { | |
65 return Platform.environment['LANG']; | |
66 } catch (e) {} | |
67 return null; | |
68 } | |
69 | |
70 /** | |
71 * Run the "defaults read -g AppleLocale" command and return the output in | |
72 * a future. | |
73 */ | |
74 Future _getAppleDefaults() { | |
75 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']); | |
76 var myResult = p.then((result) => _checkResult(result, _appleDefaultsRegex)); | |
77 return myResult; | |
78 } | |
79 | |
80 /** | |
81 * Given [result], find its text and extract the locale from it using | |
82 * [regex], and set it as the system locale. If the process didn't run correctly | |
83 * then don't set the variable and return a future that completes with null. | |
84 */ | |
85 Future<String> _checkResult(ProcessResult result, RegExp regex) { | |
86 if (result.exitCode != 0) return new Future.value(); | |
87 var match = regex.firstMatch(result.stdout); | |
88 if (match == null) return new Future.value(); | |
89 var locale = match.group(1); | |
90 _setLocale(locale); | |
91 return new Future.value(locale); | |
92 } | |
93 | |
94 /** | |
95 * Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale]. | |
96 */ | |
97 Future<String> _setLocale(aLocale) { | |
98 Intl.systemLocale = Intl.canonicalizedLocale(aLocale); | |
99 return new Future.value(Intl.systemLocale); | 30 return new Future.value(Intl.systemLocale); |
100 } | 31 } |
OLD | NEW |