From ce3d997ed20504858a97cd6d7a177478fd84bfed Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 16 Feb 2022 19:40:08 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20setup=20translations=20=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frappe/index.js | 1 - frappe/utils/translation.js | 45 +++++++++++++++++++-------- src/initialization.js | 2 +- src/pages/SetupWizard/setupCompany.js | 11 ++++--- src/utils.js | 18 +++++------ 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/frappe/index.js b/frappe/index.js index daeeb56e..15e0ed2b 100644 --- a/frappe/index.js +++ b/frappe/index.js @@ -91,7 +91,6 @@ module.exports = { this.flags = {}; this.methods = {}; this.errorLog = []; - this.languages = {}; // temp params while calling routes this.params = {}; }, diff --git a/frappe/utils/translation.js b/frappe/utils/translation.js index fc2c3c56..dd5e1089 100644 --- a/frappe/utils/translation.js +++ b/frappe/utils/translation.js @@ -1,3 +1,9 @@ +import { + getIndexFormat, + getIndexList, + getSnippets, + getWhitespaceSanitized, +} from '../../scripts/helpers'; import { ValueError } from '../common/errors'; class TranslationString { @@ -14,19 +20,23 @@ class TranslationString { return this; } - #translate(segment) { - const startSpace = segment.match(/^\s+/)?.[0] ?? ''; - const endSpace = segment.match(/\s+$/)?.[0] ?? ''; - segment = segment.replace(/\s+/g, ' ').trim(); - // TODO: implement translation backend - // segment = translate(segment) - return startSpace + segment + endSpace; - } - #formatArg(arg) { return arg ?? ''; } + #translate() { + let indexFormat = getIndexFormat(this.args[0]); + indexFormat = getWhitespaceSanitized(indexFormat); + + const translatedIndexFormat = + this.languageMap[indexFormat]?.translation ?? indexFormat; + + this.argList = getIndexList(translatedIndexFormat).map( + (i) => this.argList[i] + ); + this.strList = getSnippets(translatedIndexFormat); + } + #stitch() { if (!(this.args[0] instanceof Array)) { throw new ValueError( @@ -36,10 +46,15 @@ class TranslationString { ); } - const strList = this.args[0]; - const argList = this.args.slice(1); - return strList - .map((s, i) => this.#translate(s) + this.#formatArg(argList[i])) + this.strList = this.args[0]; + this.argList = this.args.slice(1); + + if (this.languageMap) { + this.#translate(); + } + + return this.strList + .map((s, i) => s + this.#formatArg(this.argList[i])) .join('') .replace(/\s+/g, ' ') .trim(); @@ -65,3 +80,7 @@ export function T(...args) { export function t(...args) { return new TranslationString(...args).s; } + +export function setLanguageMapOnTranslationString(languageMap) { + TranslationString.prototype.languageMap = languageMap; +} diff --git a/src/initialization.js b/src/initialization.js index 17d35a1d..aee8ee94 100644 --- a/src/initialization.js +++ b/src/initialization.js @@ -72,6 +72,7 @@ export async function connectToLocalDatabase(filePath) { // set file info in config const { companyName } = frappe.AccountingSettings; + await setLanguageMap(); let files = config.get('files') || []; if ( !files.find( @@ -90,7 +91,6 @@ export async function connectToLocalDatabase(filePath) { // set last selected file config.set('lastSelectedFilePath', filePath); - await setLanguageMap(); // second init with currency, normal usage await callInitializeMoneyMaker(); diff --git a/src/pages/SetupWizard/setupCompany.js b/src/pages/SetupWizard/setupCompany.js index 7ae77b58..38da8154 100644 --- a/src/pages/SetupWizard/setupCompany.js +++ b/src/pages/SetupWizard/setupCompany.js @@ -4,9 +4,9 @@ import { DEFAULT_LOCALE } from 'frappe/utils/consts'; import countryList from '~/fixtures/countryInfo.json'; import generateTaxes from '../../../models/doctype/Tax/RegionalEntries'; import regionalModelUpdates from '../../../models/regionalModelUpdates'; -import { callInitializeMoneyMaker } from '../../utils'; +import { callInitializeMoneyMaker, setLanguageMap } from '../../utils'; -export default async function setupCompany(setupWizardValues) { +export default async function setupCompany(setupWizardValues, language) { const { companyLogo, companyName, @@ -18,6 +18,7 @@ export default async function setupCompany(setupWizardValues) { fiscalYearEnd, } = setupWizardValues; + await setLanguageMap(language); const accountingSettings = frappe.AccountingSettings; const currency = countryList[country]['currency']; const locale = countryList[country]['locale'] ?? DEFAULT_LOCALE; @@ -45,12 +46,12 @@ export default async function setupCompany(setupWizardValues) { await setupGlobalCurrencies(countryList); await setupChartOfAccounts(bankName, country); await setupRegionalChanges(country); - updateCompanyNameInConfig(); + updateInitializationConfig(); await accountingSettings.update({ setupComplete: 1 }); frappe.AccountingSettings = accountingSettings; - (await frappe.getSingle('SystemSettings')).update({ locale }); + (await frappe.getSingle('SystemSettings')).update({ locale, language }); } async function setupGlobalCurrencies(countries) { @@ -109,7 +110,7 @@ async function setupRegionalChanges(country) { await frappe.db.migrate(); } -function updateCompanyNameInConfig() { +function updateInitializationConfig(language) { let filePath = frappe.db.dbPath; let files = config.get('files', []); files.forEach((file) => { diff --git a/src/utils.js b/src/utils.js index b420f80f..82aacb91 100644 --- a/src/utils.js +++ b/src/utils.js @@ -4,11 +4,12 @@ import router from '@/router'; import { ipcRenderer } from 'electron'; import frappe, { t } from 'frappe'; import { isPesa } from 'frappe/utils'; +import { setLanguageMapOnTranslationString } from 'frappe/utils/translation'; import lodash from 'lodash'; import { createApp, h } from 'vue'; import { handleErrorWithDialog } from './errorHandling'; -import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; import { languageCodeMap } from './languageCodeMap'; +import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; export async function showMessageDialog({ message, @@ -458,15 +459,14 @@ export function stringifyCircular( export async function checkForUpdates(force = false) { ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES, force); - await setLanguageMap(getLanguageCode()); + await setLanguageMap(); } -export async function setLanguageMap(code) { - if (code === undefined) { - code = getLanguageCode(); - } +export async function setLanguageMap(language) { + const code = getLanguageCode(language); if (code === 'en') { + setLanguageMapOnTranslationString(undefined); return; } @@ -480,10 +480,10 @@ export async function setLanguageMap(code) { return; } - frappe.languages[code] = languageMap; + setLanguageMapOnTranslationString(languageMap); } -export function getLanguageCode() { - const { language } = frappe.SystemSettings; +export function getLanguageCode(initLanguage) { + const { language } = initLanguage ?? frappe.SystemSettings; return languageCodeMap[language || 'English']; }