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 change-reports-folder module #436

19 changes: 19 additions & 0 deletions build/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@
"dbNotRestoredMessage": "DB has not been restored",
"confirmButtonText": "OK"
}
},
"showDocs": {
"modalDialog": {
"title": "User manual",
"cancelButtonText": "Close"
}
},
"printToPDF": {
"defaultTemplate": "No data",
"pagination": {
"page": "Page",
"from": "from"
}
},
"changeReportsFolder": {
"modalDialog": {
"title": "Change reports folder",
"buttonLabel": "Select"
}
}
},
"menu": {
Expand Down
19 changes: 19 additions & 0 deletions build/locales/ru/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@
"dbNotRestoredMessage": "БД не была восстановлена",
"confirmButtonText": "OK"
}
},
"showDocs": {
"modalDialog": {
"title": "Руководство пользователя",
"cancelButtonText": "Закрыть"
}
},
"printToPDF": {
"defaultTemplate": "Нет данных",
"pagination": {
"page": "Страница",
"from": "из"
}
},
"changeReportsFolder": {
"modalDialog": {
"title": "Изменить папку отчетов",
"buttonLabel": "Выбрать"
}
}
},
"menu": {
Expand Down
File renamed without changes.
17 changes: 13 additions & 4 deletions src/change-reports-folder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

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

const { REPORT_FILES_PATH_VERSION } = require('./const')

Expand All @@ -12,10 +13,14 @@ const showErrorModalDialog = require('./show-error-modal-dialog')
const pauseApp = require('./pause-app')
const relaunch = require('./relaunch')
const { getConfigsKeeperByName } = require('./configs-keeper')
const wins = require('./window-creators/windows')
const isMainWinAvailable = require('./helpers/is-main-win-available')

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

try {
const {
Expand All @@ -24,9 +29,9 @@ module.exports = ({ pathToUserDocuments }) => {
} = await dialog.showOpenDialog(
win,
{
title: 'Change reports folder',
title: i18next.t('common.changeReportsFolder.modalDialog.title'),
defaultPath: pathToUserDocuments,
buttonLabel: 'Select',
buttonLabel: i18next.t('common.changeReportsFolder.modalDialog.buttonLabel'),
properties: [
'openDirectory',
'createDirectory',
Expand Down Expand Up @@ -63,7 +68,11 @@ module.exports = ({ pathToUserDocuments }) => {
relaunch()
} catch (err) {
try {
await showErrorModalDialog(win, 'Change reports folder', err)
await showErrorModalDialog(
win,
i18next.t('common.changeReportsFolder.modalDialog.title'),
err
)
} catch (err) {
console.error(err)
}
Expand Down
32 changes: 32 additions & 0 deletions src/i18next/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ const availableLanguages = [...allFileNames.reduce((accum, fileName) => {
return accum
}, new Set())]

const docPath = path.join(rootPath, 'docs')
const docFileName = 'user-manual.md'
const allDocFileNames = fs.readdirSync(docPath)
const lngDocMap = allDocFileNames.reduce((accum, dirName) => {
const dirPath = path.join(docPath, dirName)
const dirStats = fs.lstatSync(dirPath)

if (!dirStats.isDirectory()) {
return accum
}

const filePath = path.join(dirPath, docFileName)
const fileStats = fs.lstatSync(filePath)

if (!fileStats.isFile()) {
return accum
}

const mdUserManual = fs.readFileSync(filePath, 'utf8')

accum.set(dirName, mdUserManual)

return accum
}, new Map())

let i18nextInstance = null

const _getLanguageFromAvailableOnes = (language) => {
Expand Down Expand Up @@ -89,6 +114,13 @@ const initI18next = () => {
i18next
.use(Backend)
.init(configs)

for (const [lng, mdUserManual] of lngDocMap) {
i18next.addResourceBundle(lng, 'mdDocs', {
userManual: mdUserManual
})
}

i18nextInstance = i18next

return i18next
Expand Down
5 changes: 3 additions & 2 deletions src/print-to-pdf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { BrowserWindow } = require('electron')
const fs = require('fs/promises')
const path = require('path')
const i18next = require('i18next')

const ipcs = require('../ipcs')
const wins = require('../window-creators/windows')
Expand All @@ -25,7 +26,7 @@ module.exports = () => {

const {
templateFilePath,
template = 'No data',
template = i18next.t('common.printToPDF.defaultTemplate'),
format = 'portrait',
orientation = 'Letter',
uid = null
Expand Down Expand Up @@ -73,7 +74,7 @@ module.exports = () => {
font-weight: 400;
font-size: 8px;
">
Page <span class=pageNumber></span> from <span class=totalPages></span>
${i18next.t('common.printToPDF.pagination.page')} <span class=pageNumber></span> ${i18next.t('common.printToPDF.pagination.from')} <span class=totalPages></span>
</span>`
})

Expand Down
16 changes: 5 additions & 11 deletions src/show-docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const { Converter } = require('showdown')
const Alert = require('electron-alert')
const { rootPath } = require('electron-root-path')
const i18next = require('i18next')

const wins = require('../window-creators/windows')
const isMainWinAvailable = require(
Expand All @@ -22,10 +23,6 @@ const getUIFontsAsCSSString = require(
'../helpers/get-ui-fonts-as-css-string'
)

const mdUserManual = fs.readFileSync(
path.join(rootPath, 'docs/user-manual.md'),
'utf8'
)
const {
WINDOW_EVENT_NAMES,
addOnceProcEventHandler
Expand Down Expand Up @@ -59,7 +56,7 @@ const converter = new Converter({
const _fireAlert = (params) => {
const {
type = 'info',
title = 'User manual',
title = i18next.t('common.showDocs.modalDialog.title'),
html
} = params
const win = wins.mainWindow
Expand Down Expand Up @@ -96,10 +93,7 @@ const _fireAlert = (params) => {
darkTheme: false,
parent: win,
modal: true,
width: 1000,
webPreferences: {
contextIsolation: false
}
width: 1000
}
const swalOptions = {
position: 'center',
Expand All @@ -115,7 +109,7 @@ const _fireAlert = (params) => {
showConfirmButton: false,
focusCancel: true,
showCancelButton: true,
cancelButtonText: 'Cancel',
cancelButtonText: i18next.t('common.showDocs.modalDialog.cancelButtonText'),
timerProgressBar: false,

willOpen: () => {
Expand Down Expand Up @@ -171,7 +165,7 @@ module.exports = async (params = {}) => {
const {
type,
title,
mdDoc = mdUserManual
mdDoc = i18next.t('mdDocs:userManual')
} = params

if (
Expand Down