2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03: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,
} = require('./utils/consts');
const { markRaw } = require('vue');
const { ipcRenderer } = require('electron');
const { IPC_ACTIONS } = require('@/messages');
module.exports = {
initializeAndRegister(customModels = {}, force = false) {
@ -89,6 +91,7 @@ module.exports = {
this.flags = {};
this.methods = {};
this.errorLog = [];
this.languages = {};
// temp params while calling routes
this.params = {};
},

View File

@ -16,6 +16,7 @@ import fs from 'fs/promises';
import path from 'path';
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import { sendError } from './contactMothership';
import { getLanguageMap } from './getLanguageMap';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from './messages';
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
* ------------------------------*/

View File

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

View File

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

View File

@ -460,3 +460,17 @@ window.showToast = showToast;
export function checkForUpdates(force = false) {
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;
}