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

Side by Side Diff: lib/intl_standalone.dart

Issue 2786223004: Use Platform.localeName on standalone (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /// This provides facilities for Internationalization that are only available 5 /// This provides facilities for Internationalization that are only available
6 /// when running standalone. You should import only one of this or 6 /// when running standalone. You should import only one of this or
7 /// intl_browser.dart. Right now the only thing provided here is finding 7 /// intl_browser.dart. Right now the only thing provided here is finding
8 /// the operating system locale. 8 /// the operating system locale.
9 9
10 library intl_standalone; 10 library intl_standalone;
11 11
12 import "dart:async"; 12 import "dart:async";
13 import "dart:io"; 13 import "dart:io";
14 import "intl.dart"; 14 import "intl.dart";
15 15
16 // 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
17 // 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
18 // 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
19 // 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
20 // as ways to get rid of this requirement. 20 // as ways to get rid of this requirement.
21 /// Find the system locale, accessed via the appropriate system APIs, and 21 /// Find the system locale, accessed via the appropriate system APIs, and
22 /// set it as the default for internationalization operations in 22 /// set it as the default for internationalization operations in
23 /// the [Intl.systemLocale] variable. To find it, we 23 /// the [Intl.systemLocale] variable.
24 /// check the "LANG" environment variable on *nix, use the "systeminfo"
25 /// command on Windows, and on the Mac check the environment variable "LANG",
26 /// and if it's not found, use "defaults read -g AppleLocale". This
27 /// is not an ideal way of getting a single system locale, even if that
28 /// concept really made sense, but it's a reasonable first approximation that's
29 /// not too difficult to get. If it can't find the locale information, it will
30 /// not modify [Intl.systemLocale] and the Future will complete with null.
31 Future<String> findSystemLocale() { 24 Future<String> findSystemLocale() {
32 // On *nix systems we expect this is an environment variable, which is the 25 try {
33 // easiest thing to check. On a Mac the environment variable may be present 26 Intl.systemLocale = Intl.canonicalizedLocale(Platform.localeName);
Alan Knight 2017/03/31 17:24:56 If we're going to be consistent here, we should re
zra 2017/03/31 17:40:07 It's returned. You'll see it if you scroll down a
34 // so always check it first. We have no mechanism for this right now on 27 } catch (e) {
35 // Windows, so it will just fail. 28 return new Future.value();
36 String baseLocale = _checkEnvironmentVariable();
37 if (baseLocale != null) return _setLocale(baseLocale);
38 if (Platform.operatingSystem == 'macos') {
39 return _getAppleDefaults();
40 } 29 }
41 // We can't find anything, don't set the system locale and return null.
42 return new Future.value();
43 }
44
45 /// Regular expression to match the expected output of reading the defaults
46 /// database for AppleLanguages on Mac systems.
47 /// e.g. {
48 /// en,
49 /// "pt-PT",
50 /// ...
51 RegExp _appleDefaultsRegex = new RegExp(r'((\w\w)_\w+)');
52
53 /// Check to see if we have a "LANG" environment variable we can use and return
54 /// it if found. Otherwise return null;
55 String _checkEnvironmentVariable() {
56 try {
57 return Platform.environment['LANG'];
58 } catch (e) {}
59 return null;
60 }
61
62 /// Run the "defaults read -g AppleLocale" command and return the output in
63 /// a future.
64 Future<String> _getAppleDefaults() {
65 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']);
66 var myResult = p.then((result) => _checkResult(result, _appleDefaultsRegex));
67 return myResult;
68 }
69
70 /// Given [result], find its text and extract the locale from it using [regex],
71 /// and set it as the system locale. If the process didn't run correctly then
72 /// don't set the variable and return a future that completes with null.
73 String _checkResult(ProcessResult result, RegExp regex) {
74 if (result.exitCode != 0) return null;
75 var match = regex.firstMatch(result.stdout);
76 if (match == null) return null;
77 var locale = match.group(1);
78 _setLocale(locale);
79 return locale;
80 }
81
82 /// Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale].
83 Future<String> _setLocale(aLocale) {
84 Intl.systemLocale = Intl.canonicalizedLocale(aLocale);
85 return new Future.value(Intl.systemLocale); 30 return new Future.value(Intl.systemLocale);
86 } 31 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698