OSDN Git Service

Add the Language to menu
authorZhiting Lin <zlin035@uottawa.ca>
Mon, 26 Mar 2018 05:35:27 +0000 (13:35 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Mon, 26 Mar 2018 05:35:27 +0000 (13:35 +0800)
main-process/i18n.js
main-process/logger.js
main-process/menus/application-menu.js
main.js
src/features/app/components/Container.jsx
src/features/core/actions.js

index cbf49b1..ec76f88 100644 (file)
@@ -41,12 +41,12 @@ i18n.getBestMatchedLangCode = (langCode) => {
   return bestMatchedCode
 }
 
-
-// init i18n
 i18n.init({
-  lng: global.language || 'en',
+  lng:   global.language || 'en',
+  fallbackLng: 'en',
   resources,
   interpolation: { prefix: '__', suffix: '__' },
 })
 
+
 module.exports = i18n
index 396aa1e..675b289 100644 (file)
@@ -22,14 +22,14 @@ exports.setup = function (options) {
         filename: `${logFolder}/category/main.log`,
 
       },
-      swarm: {
+      menu: {
         type: 'file',
-        filename: `${logFolder}/category/swarm.log`
+        filename: `${logFolder}/category/menu.log`
       }
     },
     categories: {
       default: { appenders: [ 'out', 'all', 'main' ], level },
-      swarm: { appenders: [ 'out', 'all', 'swarm' ], level }
+      menu: { appenders: [ 'out', 'all', 'menu' ], level }
     }
   }
 
index 7fe54df..f7355da 100755 (executable)
@@ -1,14 +1,20 @@
-const {BrowserWindow, Menu, app, shell, dialog} = require('electron')
+const {Menu, app, shell} = require('electron')
 const settings = require('electron-settings')
+const i18n = global.i18n
+const path = require('path')
+const logger = require('../logger')
+const log = logger.create('menu')
 
 let advNav = settings.get('browserSetting.app.navAdvancedState') || 'normal'
 let btmAmountUnit = settings.get('browserSetting.core.btmAmountUnit') || 'BTM'
 let menu = null
-const i18n = global.i18n
+
+const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
+const loggerOptions = Object.assign(logFolder)
+logger.setup(loggerOptions)
 
 let menuTempl = function () {
   const menu = []
-
   // APP
   const fileMenu = []
   const name = app.getName()
@@ -108,12 +114,18 @@ let menuTempl = function () {
 
 
   // LANGUAGE (VIEW)
+  const defaultLanguage = i18n.getBestMatchedLangCode(app.getLocale())
+  let currentLanguage = settings.get('browserSetting.core.lang') || defaultLanguage
   const LanguageMenu = [{
     label: i18n.t('desktop.applicationMenu.view.default'),
     type: 'checkbox',
-    // checked: btmAmountUnit === 'BTM',
     click: (item, focusedWindow) => {
       if (focusedWindow) {
+        i18n.changeLanguage(defaultLanguage, (err, t) => {
+          if (err) return log.error('something went wrong loading', err)
+          focusedWindow.webContents.send('lang', defaultLanguage)
+          createMenu()
+        })
       }
     }
   },{
@@ -121,11 +133,12 @@ let menuTempl = function () {
   },{
     label: i18n.t('desktop.applicationMenu.view.langCodes.zh'),
     type: 'checkbox',
-    // checked: btmAmountUnit === 'mBTM',
+    checked: currentLanguage === 'zh',
     click: (item, focusedWindow) => {
       if (focusedWindow) {
         i18n.changeLanguage('zh', (err, t) => {
-          if (err) return console.log('something went wrong loading', err)
+          if (err) return log.error('something went wrong loading', err)
+          focusedWindow.webContents.send('lang', 'zh')
           createMenu()
         })
 
@@ -134,12 +147,12 @@ let menuTempl = function () {
   },{
     label: i18n.t('desktop.applicationMenu.view.langCodes.en'),
     type: 'checkbox',
-    // checked: btmAmountUnit === 'NEU',
+    checked: currentLanguage === 'en',
     click: (item, focusedWindow) => {
       if (focusedWindow) {
-        // debugger
         i18n.changeLanguage('en', (err, t) => {
-          if (err) return console.log('something went wrong loading', err)
+          if (err) return log.error('something went wrong loading', err)
+          focusedWindow.webContents.send('lang', 'en')
           createMenu()
         })
       }
@@ -243,6 +256,7 @@ function findReopenMenuItem () {
 
 
 const createMenu = function () {
+  log.info('Create Menu')
   menu = Menu.buildFromTemplate(menuTempl())
   Menu.setApplicationMenu(menu)
 }
@@ -262,6 +276,13 @@ app.on('ready', () => {
     menu.items[2].submenu.items[0].submenu.items[2].checked = ( btmAmountUnit === 'NEU' )
   })
 
+  settings.watch('browserSetting.core.lang', newValue => {
+    i18n.changeLanguage(newValue, (err, t) => {
+      if (err) return log.error('i18n: something went wrong loading', err)
+      createMenu()
+    })
+  })
+
 })
 
 app.on('browser-window-created', () => {
diff --git a/main.js b/main.js
index 8c3e093..5f0a284 100644 (file)
--- a/main.js
+++ b/main.js
@@ -2,6 +2,10 @@ const {app, BrowserWindow, ipcMain} = require('electron')
 const autoUpdater = require('./auto-updater')
 const exec = require('child_process').exec
 const glob = require('glob')
+const settings = require('electron-settings')
+
+global.language = settings.get('browserSetting.core.lang')
+
 const i18n = require('./main-process/i18n.js')
 const url = require('url')
 const path = require('path')
@@ -11,11 +15,13 @@ const log = logger.create('main')
 
 let win, bytomdInit, bytomdMining
 
-global.i18n = i18n
+
 global.fileExist = false
+global.i18n = i18n
 
 
 function initialize () {
+
   loadMenu()
 
   function createWindow() {
@@ -47,6 +53,11 @@ function initialize () {
   }
 
   app.on('ready', () => {
+    // init i18n
+    if(!settings.get('browserSetting.core.lang')){
+      i18n.init({lng: app.getLocale()})
+    }
+
     const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
     const loggerOptions = Object.assign(logFolder)
     logger.setup(loggerOptions)
@@ -70,7 +81,6 @@ function initialize () {
     })
 
     createWindow()
-    // autoUpdater.initialize()
   })
 
 
@@ -144,7 +154,7 @@ function setBytomInit(event, bytomNetwork) {
 function loadMenu () {
   const files = glob.sync(path.join(__dirname, 'main-process/menus/*.js'))
   files.forEach((file) => { require(file) })
-  autoUpdater.updateMenu()
+  // autoUpdater.updateMenu()
 }
 
 // Handle Squirrel on Windows startup events
index 9ffc40c..94b846b 100644 (file)
@@ -43,8 +43,10 @@ class Container extends React.Component {
       window.ipcRenderer.on('btmAmountUnitState', (event, arg) => {
         this.props.uptdateBtmAmountUnit(arg)
       })
+      window.ipcRenderer.on('lang', (event, arg) => {
+        this.props.uptdateLang(arg)
+      })
       window.ipcRenderer.on('FileExist', (event, arg) => {
-        debugger
         if(arg === 'true'){
           this.props.updateConfiguredStatus()
         }
@@ -109,6 +111,7 @@ export default connect(
     showRoot: () => dispatch(actions.app.showRoot),
     showConfiguration: () => dispatch(actions.app.showConfiguration()),
     uptdateBtmAmountUnit: (param) => dispatch(actions.core.updateBTMAmountUnit(param)),
+    uptdateLang: (param) => dispatch(actions.core.updateLang(param)),
     updateConfiguredStatus: () => dispatch(actions.core.updateConfiguredStatus)
   })
 )(Container)
index 57f9cc8..cd9c85c 100644 (file)
@@ -5,6 +5,7 @@ const setClientToken = (token) => ({type: 'SET_CLIENT_TOKEN', token})
 const clearSession = ({ type: 'USER_LOG_OUT' })
 const updateBTMAmountUnit = (param) => ({type: 'UPDATE_BTM_AMOUNT_UNIT', param})
 const updateConfiguredStatus = ({ type: 'SET_CONFIGURED' })
+const updateLang = (param) => ({type: 'UPDATE_CORE_LANGUAGE', lang:param})
 
 const fetchCoreInfo = (options = {}) => {
   return (dispatch) => {
@@ -30,6 +31,7 @@ let actions = {
   setClientToken,
   updateInfo,
   updateBTMAmountUnit,
+  updateLang,
   updateConfiguredStatus,
   fetchCoreInfo,
   clearSession,