Skip to content

A full-featured download manager.

feat: i18n main logic

Dr_rOotf8848ba0e58c
diff --git a/src/renderer/components/Locale/index.js b/src/renderer/components/Locale/index.js
new file mode 100644
index 00000000..ff294982
--- /dev/null
+++ b/src/renderer/components/Locale/index.js
@@ -0,0 +1,29 @@
+import Vue from 'vue'
+import i18next from 'i18next'
+import VueI18Next from '@panter/vue-i18next'
+import { determineLocale } from '@shared/utils'
+import resources from './resources'
+
+Vue.use(VueI18Next)
+
+export default class LocaleManager {
+ constructor (options = {}) {
+ this.options = options
+
+ this.i18n = null
+ }
+
+ init (locale) {
+ const lng = determineLocale(locale)
+ i18next.init({
+ lng,
+ resources
+ })
+
+ this.i18n = new VueI18Next(i18next)
+ }
+
+ getI18n () {
+ return this.i18n
+ }
+}
diff --git a/src/renderer/components/Locale/resources.js b/src/renderer/components/Locale/resources.js
new file mode 100644
index 00000000..a338ce46
--- /dev/null
+++ b/src/renderer/components/Locale/resources.js
@@ -0,0 +1,21 @@
+import elementEnLocale from 'element-ui/lib/locale/lang/en'
+import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN'
+import enLocale from '@shared/locales/en'
+import zhLocale from '@shared/locales/zh'
+
+const resources = {
+ 'en': {
+ translation: {
+ ...elementEnLocale,
+ ...enLocale
+ }
+ },
+ 'zh': {
+ translation: {
+ ...elementZhLocale,
+ ...zhLocale
+ }
+ }
+}
+
+export default resources
diff --git a/src/renderer/pages/index/main.js b/src/renderer/pages/index/main.js
index dc481624..01d6f330 100644
--- a/src/renderer/pages/index/main.js
+++ b/src/renderer/pages/index/main.js
@@ -1,48 +1,61 @@
+import is from 'electron-is'
import Vue from 'vue'
+import { sync } from 'vuex-router-sync'
+import Element, { Loading } from 'element-ui'
import axios from 'axios'
+import 'svg-innerhtml'
import App from './App'
-import { sync } from 'vuex-router-sync'
import router from '@/router'
import store from '@/store'
+import LocaleManager from '@/components/Locale'
-import Element, { Loading } from 'element-ui'
-import '../../components/Theme/Index.scss'
-
-import Icon from '../../components/Icons/Icon'
-import 'svg-innerhtml'
-
-import is from 'electron-is'
-if (is.renderer()) {
- Vue.use(require('vue-electron'))
-}
+import Icon from '@/components/Icons/Icon'
+import '@/components/Theme/Index.scss'
-Vue.http = Vue.prototype.$http = axios
-Vue.config.productionTip = false
+function init (config) {
+ if (is.renderer()) {
+ Vue.use(require('vue-electron'))
+ }
-Vue.use(Element, { size: 'mini' })
-Vue.component('mo-icon', Icon)
+ Vue.http = Vue.prototype.$http = axios
+ Vue.config.productionTip = false
-sync(store, router)
+ console.log('config.locale==>', config.locale)
+ const localManager = new LocaleManager()
+ localManager.init(config.locale)
+ const i18n = localManager.getI18n()
-const loading = Loading.service({
- fullscreen: true,
- background: 'rgba(0, 0, 0, 0.1)'
-})
+ Vue.use(Element, {
+ size: 'mini',
+ i18n: (key, value) => i18n.t(key, value)
+ })
+ const loading = Loading.service({
+ fullscreen: true,
+ background: 'rgba(0, 0, 0, 0.1)'
+ })
+ Vue.component('mo-icon', Icon)
+
+ sync(store, router)
+
+ /* eslint-disable no-new */
+ window.app = new Vue({
+ components: { App },
+ router,
+ store,
+ i18n,
+ template: '<App/>'
+ }).$mount('#app')
+
+ setTimeout(() => {
+ loading.close()
+ }, 400)
+}
store.dispatch('preference/fetchPreference')
.then((config) => {
console.log('fetchPreference===>', config)
- /* eslint-disable no-new */
- window.app = new Vue({
- components: { App },
- router,
- store,
- template: '<App/>'
- }).$mount('#app')
- setTimeout(() => {
- loading.close()
- }, 400)
+ init(config)
})
.catch((err) => {
alert(err)
diff --git a/src/shared/utils.js b/src/shared/utils.js
index a9576b27..2116129f 100644
--- a/src/shared/utils.js
+++ b/src/shared/utils.js
@@ -34,27 +34,47 @@ export function timeRemaining (totalLength, completedLength, downloadSpeed) {
return Math.ceil(remainingLength / downloadSpeed)
}
-export function timeFormat (seconds, prefix = '', suffix = '') {
+/**
+ * timeFormat
+ * @param {int} seconds
+ * @param {string} prefix
+ * @param {string} suffix
+ * @param {object} i18n
+ * i18n: {
+ * gt1d: 'More than one day',
+ * hour: 'H',
+ * minute: 'm',
+ * second: 's'
+ * }
+ */
+export function timeFormat (seconds, { prefix = '', suffix = '', i18n }) {
let result = ''
let hours = ''
let minutes = ''
let secs = seconds || 0
+ const i = {
+ gt1d: '> 1 day',
+ hour: 'H',
+ minute: 'm',
+ second: 's',
+ ...i18n
+ }
if (secs <= 0) {
return ''
}
if (secs > 86400) {
- return `${prefix} 超过一天 ${suffix}`
+ return `${prefix} ${i.gt1d} ${suffix}`
}
if (secs > 3600) {
- hours = `${Math.floor(secs / 3600)}时 `
+ hours = `${Math.floor(secs / 3600)}${i.hour} `
secs %= 3600
}
if (secs > 60) {
- minutes = `${Math.floor(secs / 60)}分 `
+ minutes = `${Math.floor(secs / 60)}${i.minute} `
secs %= 60
}
- secs += '秒'
+ secs += i.second
result = hours + minutes + secs
return result ? `${prefix} ${result} ${suffix}` : result
}