"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/os-locale/index.js" (8 Mar 2017, 2621 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict';
    2 var childProcess = require('child_process');
    3 var execFileSync = childProcess.execFileSync;
    4 var lcid = require('lcid');
    5 var defaultOpts = {spawn: true};
    6 var cache;
    7 
    8 function fallback() {
    9     cache = 'en_US';
   10     return cache;
   11 }
   12 
   13 function getEnvLocale(env) {
   14     env = env || process.env;
   15     var ret = env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
   16     cache = getLocale(ret);
   17     return ret;
   18 }
   19 
   20 function parseLocale(x) {
   21     var env = x.split('\n').reduce(function (env, def) {
   22         def = def.split('=');
   23         env[def[0]] = def[1];
   24         return env;
   25     }, {});
   26     return getEnvLocale(env);
   27 }
   28 
   29 function getLocale(str) {
   30     return (str && str.replace(/[.:].*/, '')) || fallback();
   31 }
   32 
   33 module.exports = function (opts, cb) {
   34     if (typeof opts === 'function') {
   35         cb = opts;
   36         opts = defaultOpts;
   37     } else {
   38         opts = opts || defaultOpts;
   39     }
   40 
   41     if (cache || getEnvLocale() || opts.spawn === false) {
   42         setImmediate(cb, null, cache);
   43         return;
   44     }
   45 
   46     var getAppleLocale = function () {
   47         childProcess.execFile('defaults', ['read', '-g', 'AppleLocale'], function (err, stdout) {
   48             if (err) {
   49                 fallback();
   50                 return;
   51             }
   52 
   53             cache = stdout.trim() || fallback();
   54             cb(null, cache);
   55         });
   56     };
   57 
   58     if (process.platform === 'win32') {
   59         childProcess.execFile('wmic', ['os', 'get', 'locale'], function (err, stdout) {
   60             if (err) {
   61                 fallback();
   62                 return;
   63             }
   64 
   65             var lcidCode = parseInt(stdout.replace('Locale', ''), 16);
   66             cache = lcid.from(lcidCode) || fallback();
   67             cb(null, cache);
   68         });
   69     } else {
   70         childProcess.execFile('locale', function (err, stdout) {
   71             if (err) {
   72                 fallback();
   73                 return;
   74             }
   75 
   76             var res = parseLocale(stdout);
   77 
   78             if (!res && process.platform === 'darwin') {
   79                 getAppleLocale();
   80                 return;
   81             }
   82 
   83             cache = getLocale(res);
   84             cb(null, cache);
   85         });
   86     }
   87 };
   88 
   89 module.exports.sync = function (opts) {
   90     opts = opts || defaultOpts;
   91 
   92     if (cache || getEnvLocale() || !execFileSync || opts.spawn === false) {
   93         return cache;
   94     }
   95 
   96     if (process.platform === 'win32') {
   97         var stdout;
   98 
   99         try {
  100             stdout = execFileSync('wmic', ['os', 'get', 'locale'], {encoding: 'utf8'});
  101         } catch (err) {
  102             return fallback();
  103         }
  104 
  105         var lcidCode = parseInt(stdout.replace('Locale', ''), 16);
  106         cache = lcid.from(lcidCode) || fallback();
  107         return cache;
  108     }
  109 
  110     var res;
  111 
  112     try {
  113         res = parseLocale(execFileSync('locale', {encoding: 'utf8'}));
  114     } catch (err) {}
  115 
  116     if (!res && process.platform === 'darwin') {
  117         try {
  118             cache = execFileSync('defaults', ['read', '-g', 'AppleLocale'], {encoding: 'utf8'}).trim() || fallback();
  119             return cache;
  120         } catch (err) {
  121             return fallback();
  122         }
  123     }
  124 
  125     cache = getLocale(res);
  126     return cache;
  127 };