2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

feat: code to set languages on frappe

This commit is contained in:
18alantom 2022-02-16 16:11:08 +05:30
parent 6088569915
commit 76d1f3c7f5
5 changed files with 47 additions and 10 deletions

View File

@ -7,6 +7,8 @@ const {
DEFAULT_DISPLAY_PRECISION, DEFAULT_DISPLAY_PRECISION,
} = require('./utils/consts'); } = require('./utils/consts');
const { markRaw } = require('vue'); const { markRaw } = require('vue');
const { ipcRenderer } = require('electron');
const { IPC_ACTIONS } = require('@/messages');
module.exports = { module.exports = {
initializeAndRegister(customModels = {}, force = false) { initializeAndRegister(customModels = {}, force = false) {
@ -89,6 +91,7 @@ module.exports = {
this.flags = {}; this.flags = {};
this.methods = {}; this.methods = {};
this.errorLog = []; this.errorLog = [];
this.languages = {};
// temp params while calling routes // temp params while calling routes
this.params = {}; this.params = {};
}, },

View File

@ -16,6 +16,7 @@ import fs from 'fs/promises';
import path from 'path'; import path from 'path';
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'; import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import { sendError } from './contactMothership'; import { sendError } from './contactMothership';
import { getLanguageMap } from './getLanguageMap';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from './messages'; import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from './messages';
import saveHtmlAsPdf from './saveHtmlAsPdf'; import saveHtmlAsPdf from './saveHtmlAsPdf';
@ -220,6 +221,18 @@ ipcMain.handle(IPC_ACTIONS.CHECK_FOR_UPDATES, (event, force) => {
} }
}); });
ipcMain.handle(IPC_ACTIONS.GET_LANGUAGE_MAP, async (event, code) => {
let obj = { languageMap: {}, success: true, message: '' };
try {
obj.languageMap = await getLanguageMap(code, isDevelopment);
} catch (err) {
obj.success = false;
obj.message = err.message;
}
return obj;
});
/* ------------------------------ /* ------------------------------
* Register autoUpdater events lis * Register autoUpdater events lis
* ------------------------------*/ * ------------------------------*/

View File

@ -9,7 +9,7 @@
const fs = require('fs/promises'); const fs = require('fs/promises');
const path = require('path'); const path = require('path');
const fetch = require('node-fetch'); const fetch = require('node-fetch').default;
const { splitCsvLine } = require('../scripts/helpers'); const { splitCsvLine } = require('../scripts/helpers');
async function getLanguageMap(code, isDevelopment = false) { async function getLanguageMap(code, isDevelopment = false) {
@ -19,8 +19,9 @@ async function getLanguageMap(code, isDevelopment = false) {
async function getContents(code, isDevelopment) { async function getContents(code, isDevelopment) {
if (isDevelopment) { if (isDevelopment) {
const filePath = path.resolve('..', 'translations', `${code}.csv`); const filePath = path.resolve('translations', `${code}.csv`);
return await fs.readFile(filePath); const contents = await fs.readFile(filePath, { encoding: 'utf-8' });
return ['', contents].join('\n');
} }
let contents = await getContentsIfExists(); let contents = await getContentsIfExists();
@ -52,7 +53,7 @@ function getMapFromContents(contents) {
}, {}); }, {});
} }
await function getContentsIfExists(code) { async function getContentsIfExists(code) {
const filePath = getFilePath(code); const filePath = getFilePath(code);
try { try {
return await fs.readFile(filePath, { encoding: 'utf-8' }); return await fs.readFile(filePath, { encoding: 'utf-8' });
@ -63,7 +64,7 @@ await function getContentsIfExists(code) {
return ''; return '';
} }
}; }
async function fetchAndStoreFile(code, date) { async function fetchAndStoreFile(code, date) {
const url = `https://api.github.com/repos/frappe/books/contents/translations/${code}.csv`; const url = `https://api.github.com/repos/frappe/books/contents/translations/${code}.csv`;
@ -72,11 +73,15 @@ async function fetchAndStoreFile(code, date) {
throwTranslationFileNotFound(code); throwTranslationFileNotFound(code);
} }
if (!date) {
date = await getLastUpdated(code);
}
const resJson = await res.json(); const resJson = await res.json();
let contents = Buffer.from(resJson.content, 'base64').toString(); let contents = Buffer.from(resJson.content, 'base64').toString();
contents = [date.toISOString(), contents].join('\n'); contents = [date.toISOString(), contents].join('\n');
await storeFile(code, content); await storeFile(code, contents);
return contents; return contents;
} }
@ -97,10 +102,9 @@ async function shouldUpdateFile(code, contents) {
async function getLastUpdated(code) { async function getLastUpdated(code) {
const url = `https://api.github.com/repos/frappe/books/commits?path=translations%2F${code}.csv&page=1&per_page=1`; const url = `https://api.github.com/repos/frappe/books/commits?path=translations%2F${code}.csv&page=1&per_page=1`;
let resJson; const resJson = await fetch(url).then((res) => res.json());
resJson = await fetch(url).then((res) => res.json());
if (res.Json.length === 0) { if (resJson.length === 0) {
throwTranslationFileNotFound(code); throwTranslationFileNotFound(code);
} }
@ -115,8 +119,10 @@ function throwTranslationFileNotFound(code) {
throw new Error(`translation file not found for ${code}`); throw new Error(`translation file not found for ${code}`);
} }
async function storeFile(contents, code) { async function storeFile(code, contents) {
const filePath = getFilePath(code); const filePath = getFilePath(code);
const dirname = path.dirname(filePath);
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filePath, contents, { encoding: 'utf-8' }); await fs.writeFile(filePath, contents, { encoding: 'utf-8' });
} }

View File

@ -23,6 +23,7 @@ export const IPC_ACTIONS = {
SAVE_DATA: 'save-data', SAVE_DATA: 'save-data',
SHOW_ERROR: 'show-error', SHOW_ERROR: 'show-error',
SEND_ERROR: 'send-error', SEND_ERROR: 'send-error',
GET_LANGUAGE_MAP: 'get-language-map',
CHECK_FOR_UPDATES: 'check-for-updates', CHECK_FOR_UPDATES: 'check-for-updates',
}; };

View File

@ -460,3 +460,17 @@ window.showToast = showToast;
export function checkForUpdates(force = false) { export function checkForUpdates(force = false) {
ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES, force); ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES, force);
} }
export async function setLanguageMap(code) {
const { success, message, languageMap } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_LANGUAGE_MAP,
code
);
if (!success) {
showToast({ type: 'error', message });
return;
}
frappe.languages[code] = languageMap;
}