A full-featured download manager.
fix: main i18n #120
Dr_rOot1fcf0bb0d005
diff --git a/src/main/Application.js b/src/main/Application.jsindex a7fa5bf1..8fdf10c2 100644--- a/src/main/Application.js+++ b/src/main/Application.js@@ -2,8 +2,8 @@ import { EventEmitter } from 'events'import { app, shell, dialog, ipcMain } from 'electron'import is from 'electron-is'import logger from './core/Logger'-import ExceptionHandler from './core/ExceptionHandler'import ConfigManager from './core/ConfigManager'+import { setupLocaleManager } from '@/ui/Locale'import Engine from './core/Engine'import UpdateManager from './core/UpdateManager'import EnergyManager from './core/EnergyManager'@@ -15,15 +15,14 @@ import TouchBarManager from './ui/TouchBarManager'export default class Application extends EventEmitter {constructor () {super()-- this.exceptionHandler = new ExceptionHandler()-this.init()}init () {this.configManager = new ConfigManager()+this.locale = this.configManager.getLocale()+ this.localeManager = setupLocaleManager(this.locale)this.windowManager = new WindowManager({userConfig: this.configManager.getUserConfig()diff --git a/src/main/core/Engine.js b/src/main/core/Engine.jsindex 4c6511e3..25285516 100644--- a/src/main/core/Engine.js+++ b/src/main/core/Engine.js@@ -6,6 +6,7 @@ import { existsSync } from 'fs'import { resolve, join } from 'path'import forever from 'forever-monitor'import logger from './Logger'+import { getI18n } from '@/ui/Locale'import {getEngineBin,getSessionPath,@@ -16,6 +17,9 @@ export default class Engine {static instance = nullconstructor (options = {}) {+ this.options = options++ this.i18n = getI18n()this.systemConfig = options.systemConfigthis.userConfig = options.userConfig}@@ -30,14 +34,14 @@ export default class Engine {const binName = getEngineBin(platform)if (!binName) {- throw new Error('引擎已损坏,请重新安装: (')+ throw new Error(this.i18n.t('app.engine-damaged-message'))}let binPath = join(basePath, `/engine/${binName}`)const binIsExist = existsSync(binPath)if (!binIsExist) {logger.error('[Motrix] engine bin is not exist===>', binPath)- throw new Error('引擎文件缺失,请重新安装: (')+ throw new Error(this.i18n.t('app.engine-missing-message'))}let confPath = join(basePath, '/engine/aria2.conf')@@ -73,13 +77,29 @@ export default class Engine {const { child } = this.instancelogger.info('[Motrix] Engine pid===>', child.pid)- this.instance.on('exit:code', function (code) {- logger.info(`[Motrix] Engine has exited after 3 restarts===> ${code}`)+ this.instance.on('error', (err) => {+ logger.info(`[Motrix] Engine error===> ${err}`)})- this.instance.on('error', (err) => {- logger.info(`[Motrix] Engine has exited after 3 restarts===> ${err}`)+ this.instance.on('start', function (process, data) {+ logger.info(`[Motrix] Engine started===>`)+ })++ this.instance.on('stop', function (process) {+ logger.info(`[Motrix] Engine stopped===>`)})++ this.instance.on('restart', function (forever) {+ logger.info(`[Motrix] Engine exit===>`)+ })++ this.instance.on('exit:code', function (code) {+ logger.info(`[Motrix] Engine exit===> ${code}`)+ })++ // this.instance.on('stderr', (data) => {+ // logger.info(`[Motrix] Engine stderr===> ${data}`)+ // })}stop () {@@ -87,7 +107,6 @@ export default class Engine {try {logger.info('[Motrix] Engine stopping===>')this.instance.stop()- logger.info('[Motrix] Engine stopped===>', pid)} catch (err) {logger.error('[Motrix] Engine stop fail===>', err.message)this.forceStop(pid)diff --git a/src/main/core/UpdateManager.js b/src/main/core/UpdateManager.jsindex f3b7e4e3..f23fcc75 100644--- a/src/main/core/UpdateManager.js+++ b/src/main/core/UpdateManager.js@@ -4,6 +4,7 @@ import is from 'electron-is'import { autoUpdater } from 'electron-updater'import { resolve } from 'path'import logger from './Logger'+import { getI18n } from '@/ui/Locale'if (is.dev()) {autoUpdater.updateConfigPath = resolve(__dirname, '../../../app-update.yml')@@ -13,6 +14,7 @@ export default class UpdateManager extends EventEmitter {constructor (options = {}) {super()this.options = options+ this.i18n = getI18n()this.updater = autoUpdaterthis.updater.autoDownload = false@@ -48,9 +50,9 @@ export default class UpdateManager extends EventEmitter {this.emit('update-available', info)dialog.showMessageBox({type: 'info',- title: '发现新版本',- message: '发现新版本,是否现在更新?',- buttons: ['是', '否'],+ title: this.i18n.t('app.check-for-updates-title'),+ message: this.i18n.t('app.update-available-message'),+ buttons: [this.i18n.t('app.yes'), this.i18n.t('app.no')],cancelId: 1}, (buttonIndex) => {if (buttonIndex === 0) {@@ -62,8 +64,8 @@ export default class UpdateManager extends EventEmitter {updateNotAvailable (event, info) {this.emit('update-not-available', info)dialog.showMessageBox({- title: '没有更新的版本',- message: '您目前使用的已是最新版本'+ title: this.i18n.t('app.check-for-updates-title'),+ message: this.i18n.t('app.update-not-available-message')})}@@ -84,8 +86,8 @@ export default class UpdateManager extends EventEmitter {this.emit('update-downloaded', info)this.updater.logger.log(`Update Downloaded: ${info}`)dialog.showMessageBox({- title: '安装更新',- message: '更新下载完成,应用程序将退出并开始更新...'+ title: this.i18n.t('app.check-for-updates-title'),+ message: this.i18n.t('app.update-downloaded-message')}, () => {this.emit('will-updated')setImmediate(() => {@@ -96,8 +98,8 @@ export default class UpdateManager extends EventEmitter {updateError (event, error) {this.emit('update-error', error)- const msg = error == null ? '未知错误' : (error.stack || error).toString()- this.updater.logger.warn(`Update Error: ${msg}`)- dialog.showErrorBox('错误: ', msg)+ const msg = error == null ? this.i18n.t('update-error-message') : (error.stack || error).toString()+ this.updater.logger.warn(`[Motrix] update-error: ${msg}`)+ dialog.showErrorBox(msg)}}diff --git a/src/main/ui/Locale.js b/src/main/ui/Locale.jsnew file mode 100644index 00000000..9e77f025--- /dev/null+++ b/src/main/ui/Locale.js@@ -0,0 +1,24 @@+import resources from '@shared/locales/app'+import LocaleManager from '@shared/locales/LocaleManager'++const localeManager = new LocaleManager({+ resources+})++export function getLocaleManager () {+ return localeManager+}++export function setupLocaleManager (locale) {+ localeManager.changeLanguageByLocale(locale)++ return localeManager+}++export function getI18n () {+ return localeManager.getI18n()+}++export function getI18nTranslator () {+ return localeManager.getI18n().t+}diff --git a/src/main/utils/index.js b/src/main/utils/index.jsindex f28376ae..268f4320 100644--- a/src/main/utils/index.js+++ b/src/main/utils/index.js@@ -44,14 +44,14 @@ export function isRunningInDmg () {return result}-export function moveAppToApplicationsFolder () {+export function moveAppToApplicationsFolder (errorMsg = '') {return new Promise((resolve, reject) => {try {const result = app.moveToApplicationsFolder()if (result) {resolve(result)} else {- reject(new Error('应用程序移动失败'))+ reject(new Error(errorMsg))}} catch (err) {reject(err)diff --git a/src/renderer/components/Locale/index.js b/src/renderer/components/Locale/index.jsindex 59a00f51..99cbf9e0 100644--- a/src/renderer/components/Locale/index.js+++ b/src/renderer/components/Locale/index.js@@ -1,32 +1,10 @@-import i18next from 'i18next'-import { getLanguage } from '@shared/locales'import resources from '@shared/locales/all'+import LocaleManager from '@shared/locales/LocaleManager'-export class LocaleManager {- constructor (options = {}) {- this.options = options+const localeManager = new LocaleManager({+ resources+})- i18next.init({- fallbackLng: 'en-US',- resources- })- }-- changeLanguage (lng) {- i18next.changeLanguage(lng)- }-- changeLanguageByLocale (locale) {- const lng = getLanguage(locale)- this.changeLanguage(lng)- }-- getI18n () {- return i18next- }-}--const localeManager = new LocaleManager()export function getLocaleManager () {return localeManager}diff --git a/src/renderer/pages/index/main.js b/src/renderer/pages/index/main.jsindex 5e45f640..79c01797 100644--- a/src/renderer/pages/index/main.js+++ b/src/renderer/pages/index/main.js@@ -11,9 +11,10 @@ import router from '@/router'import store from '@/store'import { getLocaleManager } from '@/components/Locale'import Icon from '@/components/Icons/Icon'-import '@/components/Theme/Index.scss'import Msg from '@/components/Msg'+import '@/components/Theme/Index.scss'+function init (config) {if (is.renderer()) {Vue.use(require('vue-electron'))diff --git a/src/shared/locales/LocaleManager.js b/src/shared/locales/LocaleManager.jsnew file mode 100644index 00000000..bb5e6a96--- /dev/null+++ b/src/shared/locales/LocaleManager.js@@ -0,0 +1,26 @@+import i18next from 'i18next'+import { getLanguage } from '@shared/locales'++export default class LocaleManager {+ constructor (options = {}) {+ this.options = options++ i18next.init({+ fallbackLng: 'en-US',+ resources: options.resources+ })+ }++ changeLanguage (lng) {+ i18next.changeLanguage(lng)+ }++ changeLanguageByLocale (locale) {+ const lng = getLanguage(locale)+ this.changeLanguage(lng)+ }++ getI18n () {+ return i18next+ }+}