locales.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import path from "path";
  2. import { fileURLToPath } from "url";
  3. import Logs from "../libs/logs.js";
  4. import Cache from "../libs/cache.js";
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = path.dirname(__filename);
  7. const LOCALES_FILE = path.join(__dirname, '../data/locales.json');
  8. const LOCALES_DATA = {};
  9. const isPlainObject = (obj) => {
  10. if (Object.prototype.toString.call(obj) !== '[object Object]') return false;
  11. const proto = Object.getPrototypeOf(obj);
  12. return proto === Object.prototype || proto === null;
  13. }
  14. const initLocales = () => {
  15. Cache.getData(LOCALES_FILE).then(data => {
  16. Logs.out('load locales data', data);
  17. Object.assign(LOCALES_DATA, data);
  18. }).catch(error => {
  19. Logs.out('not load locales data', error.message);
  20. });
  21. }
  22. const saveLocales = () => {
  23. Cache.setData(LOCALES_FILE, LOCALES_DATA).catch(error => {
  24. Logs.out('save cache failed', error);
  25. });
  26. }
  27. export const get = async (keys) => {
  28. return new Promise((resolve, reject) => {
  29. if (typeof(keys) === 'undefined') {
  30. resolve(LOCALES_DATA);
  31. }
  32. else if (typeof(keys) === 'string') {
  33. resolve(LOCALES_DATA[keys] ?? null);
  34. }
  35. else if (Array.isArray(keys) && keys.every(key => typeof(key) === 'string')) {
  36. const result = {};
  37. keys.forEach(key => {
  38. result[key] = LOCALES_DATA[key] ?? null;
  39. });
  40. resolve(result);
  41. }
  42. else {
  43. reject(new Error('invalid keys'));
  44. }
  45. });
  46. }
  47. export const set = async (key, value) => {
  48. return new Promise((resolve, reject) => {
  49. if (typeof(key) === 'string' && typeof(value) !== 'undefined') {
  50. LOCALES_DATA[key] = value;
  51. saveLocales();
  52. resolve();
  53. }
  54. else if (isPlainObject(key)
  55. && Object.keys(key).every(k => typeof(k) === 'string')
  56. && Object.values(key).every(v => typeof(v) === 'string')) {
  57. Object.assign(LOCALES_DATA, key);
  58. saveLocales();
  59. resolve();
  60. }
  61. else {
  62. reject(new Error('invalid key or value'));
  63. }
  64. });
  65. }
  66. export const remove = async (keys) => {
  67. return new Promise((resolve, reject) => {
  68. if (typeof(keys) === 'string') {
  69. delete LOCALES_DATA[keys];
  70. saveLocales();
  71. resolve();
  72. }
  73. else if (Array.isArray(keys) && keys.every(key => typeof(key) === 'string')) {
  74. keys.forEach(key => {
  75. delete LOCALES_DATA[key];
  76. });
  77. saveLocales();
  78. resolve();
  79. }
  80. else {
  81. reject(new Error('invalid keys'));
  82. }
  83. });
  84. }
  85. // 进程结束时保存数据
  86. process.on('exit', saveLocales);
  87. process.on('SIGINT', () => {
  88. process.exit(0);
  89. });
  90. process.on('SIGTERM', () => {
  91. process.exit(0);
  92. });
  93. process.on('SIGUSR2', () => {
  94. process.exit(0);
  95. });
  96. // 初始化缓存
  97. initLocales();
  98. export default { get, set, remove };