2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

incr: type translation

This commit is contained in:
18alantom 2022-04-08 12:29:42 +05:30
parent 0201844fd0
commit d69dec1ca7
9 changed files with 86 additions and 103 deletions

View File

@ -1,4 +1,4 @@
import NumberSeries from './NumberSeries.js';
import NumberSeries from './NumberSeries';
import SystemSettings from './SystemSettings';
export default {

View File

@ -1,78 +0,0 @@
import { pesa } from 'pesa';
export function slug(str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
})
.replace(/\s+/g, '');
}
export function getRandomString() {
return Math.random().toString(36).substr(3);
}
export async function sleep(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
export function getQueryString(params) {
if (!params) return '';
let parts = [];
for (let key in params) {
if (key != null && params[key] != null) {
parts.push(
encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
);
}
}
return parts.join('&');
}
export function asyncHandler(fn) {
return (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch((err) => {
console.log(err);
// handle error
res.status(err.statusCode || 500).send({ error: err.message });
});
}
/**
* Returns array from 0 to n - 1
* @param {Number} n
*/
export function range(n) {
return Array(n)
.fill()
.map((_, i) => i);
}
export function unique(list, key = (it) => it) {
var seen = {};
return list.filter((item) => {
var k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
});
}
export function getDuplicates(array) {
let duplicates = [];
for (let i in array) {
let previous = array[i - 1];
let current = array[i];
if (current === previous) {
if (!duplicates.includes(current)) {
duplicates.push(current);
}
}
}
return duplicates;
}
export function isPesa(value) {
return value instanceof pesa().constructor;
}

52
frappe/utils/index.ts Normal file
View File

@ -0,0 +1,52 @@
import { pesa } from 'pesa';
export function slug(str: string) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
})
.replace(/\s+/g, '');
}
export function getRandomString() {
return Math.random().toString(36).substr(3);
}
export async function sleep(seconds: number) {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
export function range(n: number) {
return Array(n)
.fill(null)
.map((_, i) => i);
}
export function unique(list: unknown[], key = (it: unknown) => String(it)) {
const seen: Record<string, boolean> = {};
return list.filter((item) => {
const k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
});
}
export function getDuplicates(array: unknown[]) {
const duplicates: unknown[] = [];
for (const i in array) {
const previous = array[parseInt(i) - 1];
const current = array[i];
if (current === previous) {
if (!duplicates.includes(current)) {
duplicates.push(current);
}
}
}
return duplicates;
}
export function isPesa(value: unknown): boolean {
return value instanceof pesa().constructor;
}

View File

@ -1,3 +1,4 @@
import { LanguageMap } from 'utils/types';
import {
getIndexFormat,
getIndexList,
@ -7,7 +8,13 @@ import {
import { ValueError } from './errors';
class TranslationString {
constructor(...args) {
args: TemplateStringsArray;
argList?: string[];
strList?: string[];
context?: string;
languageMap?: LanguageMap;
constructor(...args: TemplateStringsArray) {
this.args = args;
}
@ -15,12 +22,12 @@ class TranslationString {
return this.toString();
}
ctx(context) {
ctx(context?: string) {
this.context = context;
return this;
}
#formatArg(arg) {
#formatArg(arg: string) {
return arg ?? '';
}
@ -29,16 +36,16 @@ class TranslationString {
indexFormat = getWhitespaceSanitized(indexFormat);
const translatedIndexFormat =
this.languageMap[indexFormat]?.translation ?? indexFormat;
this.languageMap![indexFormat]?.translation ?? indexFormat;
this.argList = getIndexList(translatedIndexFormat).map(
(i) => this.argList[i]
(i) => this.argList![i]
);
this.strList = getSnippets(translatedIndexFormat);
}
#stitch() {
if (!(this.args[0] instanceof Array)) {
if (!((this.args[0] as any) instanceof Array)) {
throw new ValueError(
`invalid args passed to TranslationString ${
this.args
@ -46,15 +53,14 @@ class TranslationString {
);
}
this.strList = this.args[0];
this.strList = this.args[0] as any as string[];
this.argList = this.args.slice(1);
if (this.languageMap) {
this.#translate();
}
return this.strList
.map((s, i) => s + this.#formatArg(this.argList[i]))
return this.strList!.map((s, i) => s + this.#formatArg(this.argList![i]))
.join('')
.replace(/\s+/g, ' ')
.trim();
@ -73,14 +79,16 @@ class TranslationString {
}
}
export function T(...args) {
export function T(...args: TemplateStringsArray): TranslationString {
// @ts-ignore
return new TranslationString(...args);
}
export function t(...args) {
export function t(...args: TemplateStringsArray): string {
// @ts-ignore
return new TranslationString(...args).s;
}
export function setLanguageMapOnTranslationString(languageMap) {
export function setLanguageMapOnTranslationString(languageMap: LanguageMap) {
TranslationString.prototype.languageMap = languageMap;
}

View File

@ -39,6 +39,7 @@
"@types/assert": "^1.5.6",
"@types/electron-devtools-installer": "^2.2.0",
"@types/lodash": "^4.14.179",
"@types/luxon": "^2.3.1",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^4.15.1",

View File

@ -11,16 +11,14 @@ const fs = require('fs/promises');
const path = require('path');
const fetch = require('node-fetch').default;
const { splitCsvLine } = require('../scripts/helpers');
import { LanguageMap } from '../utils/types';
const VALENTINES_DAY = 1644796800000;
type Translation = { translation: string; context?: string };
type TranslationMap = Record<string, Translation>;
export async function getLanguageMap(
code: string,
isDevelopment: boolean = false
): Promise<TranslationMap> {
): Promise<LanguageMap> {
const contents = await getContents(code, isDevelopment);
return getMapFromContents(contents);
}
@ -46,7 +44,7 @@ async function getContents(code: string, isDevelopment: boolean) {
return contents;
}
function getMapFromContents(contents: string): TranslationMap {
function getMapFromContents(contents: string): LanguageMap {
const lines: string[] = contents.split('\n').slice(1);
return lines
.map((l) => splitCsvLine(l) as string[])
@ -62,7 +60,7 @@ function getMapFromContents(contents: string): TranslationMap {
}
return acc;
}, {} as TranslationMap);
}, {} as LanguageMap);
}
async function getContentsIfExists(code: string): Promise<string> {

2
utils/types.ts Normal file
View File

@ -0,0 +1,2 @@
export type Translation = { translation: string; context?: string };
export type LanguageMap = Record<string, Translation>;

View File

@ -1348,6 +1348,11 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.179.tgz#490ec3288088c91295780237d2497a3aa9dfb5c5"
integrity sha512-uwc1x90yCKqGcIOAT6DwOSuxnrAbpkdPsUOZtwrXb4D/6wZs+6qG7QnIawDuZWg0sWpxl+ltIKCaLoMlna678w==
"@types/luxon@^2.3.1":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-2.3.1.tgz#e34763178b46232e4c5f079f1706e18692415519"
integrity sha512-nAPUltOT28fal2eDZz8yyzNhBjHw1NEymFBP7Q9iCShqpflWPybxHbD7pw/46jQmT+HXOy1QN5hNTms8MOTlOQ==
"@types/mime@^1":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
@ -7887,12 +7892,7 @@ minimatch@4.2.1:
dependencies:
brace-expansion "^1.1.7"
minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5:
version "1.2.6"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==
minimist@^1.2.6:
minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6:
version "1.2.6"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==