i18n.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. // 获取当前语言设置
  5. const getCurrentLanguage = () => {
  6. try {
  7. // 1. 优先从用户数据目录的缓存文件读取(由前端设置)
  8. const userDataPath = getElectronUserDataPath();
  9. const langCachePath = path.join(userDataPath, 'language.json');
  10. if (fs.existsSync(langCachePath)) {
  11. const data = JSON.parse(fs.readFileSync(langCachePath, 'utf-8'));
  12. if (data.language && ['zh', 'en', 'zh-CN'].includes(data.language)) {
  13. return data.language;
  14. }
  15. }
  16. // 2. 尝试从配置文件读取
  17. const configPath = path.join(__dirname, 'config.default.js');
  18. if (fs.existsSync(configPath)) {
  19. const config = require(configPath);
  20. return config.language || 'zh';
  21. }
  22. } catch (e) {
  23. console.log('Failed to read language config:', e.message);
  24. }
  25. return 'zh';
  26. };
  27. // 获取 Electron 用户数据目录
  28. const getElectronUserDataPath = () => {
  29. try {
  30. const { app } = require('electron');
  31. return app.getPath('userData');
  32. } catch (e) {
  33. // 在非 Electron 环境(如测试)下返回默认路径
  34. return path.join(__dirname, '..', '..');
  35. }
  36. };
  37. // 加载翻译文件
  38. const loadTranslations = () => {
  39. const language = getCurrentLanguage();
  40. // 标准化语言代码:en -> en, zh-CN/zh -> zh
  41. let i18nFile = 'en';
  42. if (language.startsWith('zh')) {
  43. i18nFile = 'zh';
  44. }
  45. const i18nPath = path.join(__dirname, 'i18n', `${i18nFile}.json`);
  46. try {
  47. if (fs.existsSync(i18nPath)) {
  48. return JSON.parse(fs.readFileSync(i18nPath, 'utf-8'));
  49. }
  50. } catch (e) {
  51. console.log('Failed to load i18n file:', e.message);
  52. }
  53. // 默认加载中文
  54. const defaultPath = path.join(__dirname, 'i18n', 'zh.json');
  55. try {
  56. return JSON.parse(fs.readFileSync(defaultPath, 'utf-8'));
  57. } catch (e) {
  58. console.error('Failed to load default i18n file:', e.message);
  59. return {};
  60. }
  61. };
  62. // 缓存翻译数据
  63. let translations = null;
  64. // 获取翻译
  65. const t = (key, params = {}) => {
  66. if (!translations) {
  67. translations = loadTranslations();
  68. }
  69. // 支持嵌套 key 如 "camera.cameraNotConnected"
  70. const keys = key.split('.');
  71. let value = translations;
  72. for (const k of keys) {
  73. if (value && typeof value === 'object' && k in value) {
  74. value = value[k];
  75. } else {
  76. return key; // 未找到翻译,返回原始 key
  77. }
  78. }
  79. // 如果找到翻译,用参数替换占位符
  80. if (typeof value === 'string' && Object.keys(params).length > 0) {
  81. return value.replace(/\{(\w+)\}/g, (match, paramKey) => {
  82. return params[paramKey] !== undefined ? params[paramKey] : match;
  83. });
  84. }
  85. return value;
  86. };
  87. // 重新加载翻译(当语言改变时调用)
  88. const reloadTranslations = () => {
  89. translations = loadTranslations();
  90. console.log('i18n translations reloaded');
  91. };
  92. module.exports = {
  93. t,
  94. getCurrentLanguage,
  95. loadTranslations,
  96. reloadTranslations,
  97. };