| 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 /** |
| 6 * This provides facilities for Internationalization that are only available | 6 * This provides facilities for Internationalization that are only available |
| 7 * when running standalone. You should import only one of this or | 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 | 8 * intl_browser.dart. Right now the only thing provided here is finding |
| 9 * the operating system locale. | 9 * the operating system locale. |
| 10 */ | 10 */ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 * command on Windows, and on the Mac check the environment variable "LANG", | 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 | 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 | 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 | 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 | 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. | 33 * not modify [Intl.systemLocale] and the Future will complete with null. |
| 34 */ | 34 */ |
| 35 Future<String> findSystemLocale() { | 35 Future<String> findSystemLocale() { |
| 36 // On *nix systems we expect this is an environment variable, which is the | 36 // On *nix systems we expect this is an environment variable, which is the |
| 37 // easiest thing to check. On a Mac the environment variable may be present | 37 // easiest thing to check. On a Mac the environment variable may be present |
| 38 // so always check it first. | 38 // so always check it first. We have no mechanism for this right now on |
| 39 // Windows, so it will just fail. |
| 39 String baseLocale = _checkEnvironmentVariable(); | 40 String baseLocale = _checkEnvironmentVariable(); |
| 40 if (baseLocale != null) return _setLocale(baseLocale); | 41 if (baseLocale != null) return _setLocale(baseLocale); |
| 41 if (Platform.operatingSystem == 'windows') { | |
| 42 return _getWindowsSystemInfo(); | |
| 43 } | |
| 44 if (Platform.operatingSystem == 'macos') { | 42 if (Platform.operatingSystem == 'macos') { |
| 45 return _getAppleDefaults(); | 43 return _getAppleDefaults(); |
| 46 } | 44 } |
| 47 // We can't find anything, don't set the system locale and return null. | 45 // We can't find anything, don't set the system locale and return null. |
| 48 return new Future.immediate(null); | 46 return new Future.immediate(null); |
| 49 } | 47 } |
| 50 | 48 |
| 51 /** | 49 /** |
| 52 * Regular expression to match the expected output of systeminfo on | |
| 53 * Windows. e.g. System Locale:<tab>en_US;English (United States) | |
| 54 */ | |
| 55 RegExp sysInfoRegex = new RegExp(r"System Locale:\s+((\w\w;)|(\w\w-\w+;))"); | |
| 56 | |
| 57 /** | |
| 58 * Regular expression to match the expected output of reading the defaults | 50 * Regular expression to match the expected output of reading the defaults |
| 59 * database for AppleLanguages on Mac systems. | 51 * database for AppleLanguages on Mac systems. |
| 60 * e.g. { | 52 * e.g. { |
| 61 * en, | 53 * en, |
| 62 * "pt-PT", | 54 * "pt-PT", |
| 63 * ... | 55 * ... |
| 64 */ | 56 */ |
| 65 RegExp _appleDefaultsRegex = new RegExp(r'((\w\w)_\w+)'); | 57 RegExp _appleDefaultsRegex = new RegExp(r'((\w\w)_\w+)'); |
| 66 | 58 |
| 67 /** | 59 /** |
| (...skipping 11 matching lines...) Expand all Loading... |
| 79 * Run the "defaults read -g AppleLocale" command and return the output in | 71 * Run the "defaults read -g AppleLocale" command and return the output in |
| 80 * a future. | 72 * a future. |
| 81 */ | 73 */ |
| 82 Future _getAppleDefaults() { | 74 Future _getAppleDefaults() { |
| 83 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']); | 75 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']); |
| 84 var myResult = p.then((result) => _checkResult(result, _appleDefaultsRegex)); | 76 var myResult = p.then((result) => _checkResult(result, _appleDefaultsRegex)); |
| 85 return myResult; | 77 return myResult; |
| 86 } | 78 } |
| 87 | 79 |
| 88 /** | 80 /** |
| 89 * Run the "systemlocale" command and return the output in a future. | |
| 90 */ | |
| 91 Future _getWindowsSystemInfo() { | |
| 92 var p = Process.run('systeminfo', []); | |
| 93 var myResult = p.then((result) => _checkResult(result, sysInfoRegex)); | |
| 94 return myResult; | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Given [result], find its text and extract the locale from it using | 81 * Given [result], find its text and extract the locale from it using |
| 99 * [regex], and set it as the system locale. If the process didn't run correctly | 82 * [regex], and set it as the system locale. If the process didn't run correctly |
| 100 * then don't set the variable and return a future that completes with null. | 83 * then don't set the variable and return a future that completes with null. |
| 101 */ | 84 */ |
| 102 Future<String> _checkResult(ProcessResult result, RegExp regex) { | 85 Future<String> _checkResult(ProcessResult result, RegExp regex) { |
| 103 if (result.exitCode != 0) return new Future.immediate(null); | 86 if (result.exitCode != 0) return new Future.immediate(null); |
| 104 var match = regex.firstMatch(result.stdout); | 87 var match = regex.firstMatch(result.stdout); |
| 105 if (match == null) return new Future.immediate(null); | 88 if (match == null) return new Future.immediate(null); |
| 106 var locale = match.group(1); | 89 var locale = match.group(1); |
| 107 _setLocale(locale); | 90 _setLocale(locale); |
| 108 return new Future.immediate(locale); | 91 return new Future.immediate(locale); |
| 109 } | 92 } |
| 110 | 93 |
| 111 /** | 94 /** |
| 112 * Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale]. | 95 * Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale]. |
| 113 */ | 96 */ |
| 114 Future<String> _setLocale(aLocale) { | 97 Future<String> _setLocale(aLocale) { |
| 115 Intl.systemLocale = Intl.canonicalizedLocale(aLocale); | 98 Intl.systemLocale = Intl.canonicalizedLocale(aLocale); |
| 116 return new Future.immediate(Intl.systemLocale); | 99 return new Future.immediate(Intl.systemLocale); |
| 117 } | 100 } |
| OLD | NEW |