@@ -20,67 +20,12 @@ import "intl.dart";
2020// as ways to get rid of this requirement.
2121/// Find the system locale, accessed via the appropriate system APIs, and
2222/// set it as the default for internationalization operations in
23- /// the [Intl.systemLocale] variable. To find it, we
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.
23+ /// the [Intl.systemLocale] variable.
3124Future <String > findSystemLocale () {
32- // On *nix systems we expect this is an environment variable, which is the
33- // easiest thing to check. On a Mac the environment variable may be present
34- // so always check it first. We have no mechanism for this right now on
35- // Windows, so it will just fail.
36- String baseLocale = _checkEnvironmentVariable ();
37- if (baseLocale != null ) return _setLocale (baseLocale);
38- if (Platform .operatingSystem == 'macos' ) {
39- return _getAppleDefaults ();
40- }
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 () {
5625 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);
26+ Intl .systemLocale = Intl .canonicalizedLocale (Platform .localeName);
27+ } catch (e) {
28+ return new Future .value ();
29+ }
8530 return new Future .value (Intl .systemLocale);
8631}
0 commit comments