Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.2 KiB

  1. 'use strict';
  2. const execa = require('execa');
  3. const lcid = require('lcid');
  4. const mem = require('mem');
  5. const defaultOptions = {spawn: true};
  6. const defaultLocale = 'en_US';
  7. function getEnvLocale(env) {
  8. env = env || process.env;
  9. return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
  10. }
  11. function parseLocale(x) {
  12. const env = x.split('\n').reduce((env, def) => {
  13. def = def.split('=');
  14. env[def[0]] = def[1].replace(/^"|"$/g, '');
  15. return env;
  16. }, {});
  17. return getEnvLocale(env);
  18. }
  19. function getLocale(string) {
  20. return (string && string.replace(/[.:].*/, ''));
  21. }
  22. function getAppleLocale() {
  23. return execa.stdout('defaults', ['read', '-g', 'AppleLocale']);
  24. }
  25. function getAppleLocaleSync() {
  26. return execa.sync('defaults', ['read', '-g', 'AppleLocale']).stdout;
  27. }
  28. function getUnixLocale() {
  29. if (process.platform === 'darwin') {
  30. return getAppleLocale();
  31. }
  32. return execa.stdout('locale')
  33. .then(stdout => getLocale(parseLocale(stdout)));
  34. }
  35. function getUnixLocaleSync() {
  36. if (process.platform === 'darwin') {
  37. return getAppleLocaleSync();
  38. }
  39. return getLocale(parseLocale(execa.sync('locale').stdout));
  40. }
  41. function getWinLocale() {
  42. return execa.stdout('wmic', ['os', 'get', 'locale'])
  43. .then(stdout => {
  44. const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
  45. return lcid.from(lcidCode);
  46. });
  47. }
  48. function getWinLocaleSync() {
  49. const {stdout} = execa.sync('wmic', ['os', 'get', 'locale']);
  50. const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
  51. return lcid.from(lcidCode);
  52. }
  53. module.exports = mem((options = defaultOptions) => {
  54. const envLocale = getEnvLocale();
  55. let thenable;
  56. if (envLocale || options.spawn === false) {
  57. thenable = Promise.resolve(getLocale(envLocale));
  58. } else if (process.platform === 'win32') {
  59. thenable = getWinLocale();
  60. } else {
  61. thenable = getUnixLocale();
  62. }
  63. return thenable.then(locale => locale || defaultLocale)
  64. .catch(() => defaultLocale);
  65. });
  66. module.exports.sync = mem((options = defaultOptions) => {
  67. const envLocale = getEnvLocale();
  68. let res;
  69. if (envLocale || options.spawn === false) {
  70. res = getLocale(envLocale);
  71. } else {
  72. try {
  73. if (process.platform === 'win32') {
  74. res = getWinLocaleSync();
  75. } else {
  76. res = getUnixLocaleSync();
  77. }
  78. } catch (_) {}
  79. }
  80. return res || defaultLocale;
  81. });