| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 return _getAppleDefaults(); | 45 return _getAppleDefaults(); |
| 46 } | 46 } |
| 47 // We can't find anything, don't set the system locale and return null. | 47 // We can't find anything, don't set the system locale and return null. |
| 48 return new Future.immediate(null); | 48 return new Future.immediate(null); |
| 49 } | 49 } |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * Regular expression to match the expected output of systeminfo on | 52 * Regular expression to match the expected output of systeminfo on |
| 53 * Windows. e.g. System Locale:<tab>en_US;English (United States) | 53 * Windows. e.g. System Locale:<tab>en_US;English (United States) |
| 54 */ | 54 */ |
| 55 RegExp _sysInfoRegex = new RegExp(r"System Locale:\s+(\w\w-\w+);"); | 55 RegExp sysInfoRegex = new RegExp(r"System Locale:\s+((\w\w;)|(\w\w-\w+;))"); |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * Regular expression to match the expected output of reading the defaults | 58 * Regular expression to match the expected output of reading the defaults |
| 59 * database for AppleLanguages on Mac systems. | 59 * database for AppleLanguages on Mac systems. |
| 60 * e.g. { | 60 * e.g. { |
| 61 * en, | 61 * en, |
| 62 * "pt-PT", | 62 * "pt-PT", |
| 63 * ... | 63 * ... |
| 64 */ | 64 */ |
| 65 RegExp _appleDefaultsRegex = new RegExp(r'(\w\w_\w+)'); | 65 RegExp _appleDefaultsRegex = new RegExp(r'((\w\w)_\w+)'); |
| 66 | 66 |
| 67 /** | 67 /** |
| 68 * Check to see if we have a "LANG" environment variable we can use and return | 68 * Check to see if we have a "LANG" environment variable we can use and return |
| 69 * it if found. Otherwise return null; | 69 * it if found. Otherwise return null; |
| 70 */ | 70 */ |
| 71 String _checkEnvironmentVariable() { | 71 String _checkEnvironmentVariable() { |
| 72 try { | 72 try { |
| 73 return Platform.environment['LANG']; | 73 return Platform.environment['LANG']; |
| 74 } catch (e) {}; | 74 } catch (e) {}; |
| 75 return null; | 75 return null; |
| 76 } | 76 } |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * Run the "defaults read -g AppleLocale" command and return the output in | 79 * Run the "defaults read -g AppleLocale" command and return the output in |
| 80 * a future. | 80 * a future. |
| 81 */ | 81 */ |
| 82 Future _getAppleDefaults() { | 82 Future _getAppleDefaults() { |
| 83 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']); | 83 var p = Process.run('defaults', ['read', '-g', 'AppleLocale']); |
| 84 var myResult = p.then((result) => _checkResult(result, _appleDefaultsRegex)); | 84 var myResult = p.then((result) => _checkResult(result, _appleDefaultsRegex)); |
| 85 return myResult; | 85 return myResult; |
| 86 } | 86 } |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Run the "systemlocale" command and return the output in a future. | 89 * Run the "systemlocale" command and return the output in a future. |
| 90 */ | 90 */ |
| 91 Future _getWindowsSystemInfo() { | 91 Future _getWindowsSystemInfo() { |
| 92 var p = Process.run('systeminfo', []); | 92 var p = Process.run('systeminfo', []); |
| 93 var myResult = p.then((result) => _checkResult(result, _sysInfoRegex)); | 93 var myResult = p.then((result) => _checkResult(result, sysInfoRegex)); |
| 94 return myResult; | 94 return myResult; |
| 95 } | 95 } |
| 96 | 96 |
| 97 /** | 97 /** |
| 98 * Given [result], find its text and extract the locale from it using | 98 * 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 | 99 * [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. | 100 * then don't set the variable and return a future that completes with null. |
| 101 */ | 101 */ |
| 102 Future<String> _checkResult(ProcessResult result, RegExp regex) { | 102 Future<String> _checkResult(ProcessResult result, RegExp regex) { |
| 103 if (result.exitCode != 0) return new Future.immediate(null); | 103 if (result.exitCode != 0) return new Future.immediate(null); |
| 104 var match = regex.firstMatch(result.stdout); | 104 var match = regex.firstMatch(result.stdout); |
| 105 if (match == null) return new Future.immediate(null); | 105 if (match == null) return new Future.immediate(null); |
| 106 var locale = match.group(1); | 106 var locale = match.group(1); |
| 107 _setLocale(locale); | 107 _setLocale(locale); |
| 108 return new Future.immediate(locale); | 108 return new Future.immediate(locale); |
| 109 } | 109 } |
| 110 | 110 |
| 111 /** | 111 /** |
| 112 * Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale]. | 112 * Set [Intl.systemLocale] to be the canonicalizedLocale of [aLocale]. |
| 113 */ | 113 */ |
| 114 Future<String> _setLocale(aLocale) { | 114 Future<String> _setLocale(aLocale) { |
| 115 Intl.systemLocale = Intl.canonicalizedLocale(aLocale); | 115 Intl.systemLocale = Intl.canonicalizedLocale(aLocale); |
| 116 return new Future.immediate(Intl.systemLocale); | 116 return new Future.immediate(Intl.systemLocale); |
| 117 } | 117 } |
| OLD | NEW |