Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translation support to export-db module #441

Merged
27 changes: 27 additions & 0 deletions build/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@
"message": "Another version of {{productName}} is currently running. Quit it, then launch this version of the app again",
"confirmButtonText": "OK"
}
},
"showMessageModalDialog": {
"confirmButtonText": "OK",
"cancelButtonText": "Cancel"
},
"showErrorModalDialog": {
"confirmButtonText": "OK",
"enoentErrorMessage": "No such file or directory",
"eaccesErrorMessage": "Permission denied",
"invalidFilePathErrorMessage": "Invalid file path",
"invalidFileNameInArchErrorMessage": "Invalid file name in archive",
"dbImportingErrorMessage": "The database has not been imported",
"dbRemovingErrorMessage": "The database has not been removed",
"reportsFolderChangingErrorMessage": "The reports folder has not been changed",
"syncFrequencyChangingErrorMessage": "The sync frequency has not been changed",
"unexpectedExceptionMessage": "An unexpected exception occurred"
},
"exportDB": {
"saveDialog": {
"title": "Database export",
"buttonLabel": "Export"
},
"modalDialog": {
"confirmButtonText": "OK",
"title": "Database export",
"message": "Exported successfully"
}
}
},
"menu": {
Expand Down
27 changes: 27 additions & 0 deletions build/locales/ru/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@
"message": "В настоящее время запущена другая версия {{productName}}. Закройте ее, затем снова запустите эту версию приложения",
"confirmButtonText": "OK"
}
},
"showMessageModalDialog": {
"confirmButtonText": "OK",
"cancelButtonText": "Отмена"
},
"showErrorModalDialog": {
"confirmButtonText": "OK",
"enoentErrorMessage": "Данный файл или каталог отсутствует",
"eaccesErrorMessage": "Доступ запрещен",
"invalidFilePathErrorMessage": "Неверный путь к файлу",
"invalidFileNameInArchErrorMessage": "Неверное имя файла в архиве",
"dbImportingErrorMessage": "База данных не была импортирована",
"dbRemovingErrorMessage": "База данных не была удалена",
"reportsFolderChangingErrorMessage": "Папка отчетов не была изменена",
"syncFrequencyChangingErrorMessage": "Частота синхронизации не была изменена",
"unexpectedExceptionMessage": "Произошло неожиданное исключение"
},
"exportDB": {
"saveDialog": {
"title": "Экспорт базы данных",
"buttonLabel": "Экспортировать"
},
"modalDialog": {
"confirmButtonText": "OK",
"title": "Экспорт базы данных",
"message": "Экспортирована успешно"
}
}
},
"menu": {
Expand Down
26 changes: 19 additions & 7 deletions src/export-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path')
const { dialog, BrowserWindow } = require('electron')
const i18next = require('i18next')

const { InvalidFilePathError } = require('./errors')
const { zip } = require('./archiver')
Expand All @@ -11,6 +12,8 @@ const {
showLoadingWindow,
hideLoadingWindow
} = require('./window-creators/change-loading-win-visibility-state')
const wins = require('./window-creators/windows')
const isMainWinAvailable = require('./helpers/is-main-win-available')
const {
DEFAULT_ARCHIVE_DB_FILE_NAME,
DB_FILE_NAME,
Expand All @@ -35,7 +38,9 @@ module.exports = ({
const secretKeyPath = path.join(pathToUserData, SECRET_KEY_FILE_NAME)

return async () => {
const win = BrowserWindow.getFocusedWindow()
const win = isMainWinAvailable(wins.mainWindow)
? wins.mainWindow
: BrowserWindow.getFocusedWindow()

try {
const {
Expand All @@ -44,9 +49,10 @@ module.exports = ({
} = await dialog.showSaveDialog(
win,
{
title: 'Database export',
title: i18next.t('common.exportDB.saveDialog.title'),
defaultPath,
buttonLabel: 'Export',
buttonLabel: i18next
.t('common.exportDB.saveDialog.buttonLabel'),
filters: [{ name: 'ZIP', extensions: ['zip'] }]
}
)
Expand All @@ -71,15 +77,21 @@ module.exports = ({
await hideLoadingWindow()

await showMessageModalDialog(win, {
buttons: ['OK'],
buttons: [
i18next.t('common.exportDB.modalDialog.confirmButtonText')
],
defaultId: 0,
title: 'Database export',
message: 'Exported successfully'
title: i18next.t('common.exportDB.modalDialog.title'),
message: i18next.t('common.exportDB.modalDialog.message')
})
} catch (err) {
try {
await hideLoadingWindow()
await showErrorModalDialog(win, 'Database export', err)
await showErrorModalDialog(
win,
i18next.t('common.exportDB.modalDialog.title'),
err
)
} catch (err) {
console.error(err)
}
Expand Down
24 changes: 14 additions & 10 deletions src/show-error-modal-dialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const i18next = require('i18next')

const {
InvalidFilePathError,
InvalidFileNameInArchiveError,
Expand All @@ -14,7 +16,9 @@ const showMessageModalDialog = require('./show-message-modal-dialog')
const _showErrorBox = (win, title = '', message = '') => {
return showMessageModalDialog(win, {
type: 'error',
buttons: ['OK'],
buttons: [
i18next.t('common.showErrorModalDialog.confirmButtonText')
],
defaultId: 0,
cancelId: 0,
title,
Expand All @@ -24,56 +28,56 @@ const _showErrorBox = (win, title = '', message = '') => {

module.exports = async (win, title = 'Error', err) => {
if (err.code === 'ENOENT') {
const message = 'No such file or directory'
const message = i18next.t('common.showErrorModalDialog.enoentErrorMessage')
const content = (err.syscall && err.path)
? `${message}, ${err.syscall}: '${err.path}'`
: message

return _showErrorBox(win, title, content)
}
if (err.code === 'EACCES') {
const message = 'Permission denied'
const message = i18next.t('common.showErrorModalDialog.eaccesErrorMessage')
const content = (err.syscall && err.path)
? `${message}, ${err.syscall}: '${err.path}'`
: message

return _showErrorBox(win, title, content)
}
if (err instanceof InvalidFilePathError) {
const message = 'Invalid file path'
const message = i18next.t('common.showErrorModalDialog.invalidFilePathErrorMessage')

return _showErrorBox(win, title, message)
}
if (err instanceof InvalidFileNameInArchiveError) {
const message = 'Invalid file name in archive'
const message = i18next.t('common.showErrorModalDialog.invalidFileNameInArchErrorMessage')

return _showErrorBox(win, title, message)
}
if (
err instanceof DbImportingError ||
err instanceof InvalidFolderPathError
) {
const message = 'The database has not been imported'
const message = i18next.t('common.showErrorModalDialog.dbImportingErrorMessage')

return _showErrorBox(win, title, message)
}
if (err instanceof DbRemovingError) {
const message = 'The database has not been removed'
const message = i18next.t('common.showErrorModalDialog.dbRemovingErrorMessage')

return _showErrorBox(win, title, message)
}
if (err instanceof ReportsFolderChangingError) {
const message = 'The reports folder has not been changed'
const message = i18next.t('common.showErrorModalDialog.reportsFolderChangingErrorMessage')

return _showErrorBox(win, title, message)
}
if (err instanceof SyncFrequencyChangingError) {
const message = 'The sync frequency has not been changed'
const message = i18next.t('common.showErrorModalDialog.syncFrequencyChangingErrorMessage')

return _showErrorBox(win, title, message)
}

const message = 'An unexpected exception occurred'
const message = i18next.t('common.showErrorModalDialog.syncFrequencyChangingErrorMessage')

return _showErrorBox(win, title, message)
}
18 changes: 14 additions & 4 deletions src/show-message-modal-dialog.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
'use strict'

const { dialog, BrowserWindow } = require('electron')
const i18next = require('i18next')

const wins = require('./window-creators/windows')
const isMainWinAvailable = require('./helpers/is-main-win-available')

module.exports = async (win, opts = {}) => {
const _win = win && typeof win === 'object'
? win
const defaultWin = isMainWinAvailable(wins.mainWindow)
? wins.mainWindow
: BrowserWindow.getFocusedWindow()
const parentWin = win && typeof win === 'object'
? win
: defaultWin

const {
response: btnId,
checkboxChecked
} = await dialog.showMessageBox(_win, {
} = await dialog.showMessageBox(parentWin, {
type: 'info',
buttons: ['Cancel', 'OK'],
buttons: [
i18next.t('common.showMessageModalDialog.cancelButtonText'),
i18next.t('common.showMessageModalDialog.confirmButtonText')
],
defaultId: 1,
cancelId: 0,
title: '',
Expand Down