@itrocks/translate 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,4 +7,5 @@ export type Options = {
7
7
  };
8
8
  export declare function tr(text: string, options: Options): string;
9
9
  export declare function tr(text: string, parts?: string[], options?: Options): string;
10
- export declare function trInit(lang: string, file: string): void;
10
+ export declare function trInit(lang: string): void;
11
+ export declare function trLoad(file: string): Promise<void>;
package/cjs/translate.js CHANGED
@@ -4,7 +4,9 @@ exports.translations = exports.expressions = exports.DefaultOptions = void 0;
4
4
  exports.lang = lang;
5
5
  exports.tr = tr;
6
6
  exports.trInit = trInit;
7
+ exports.trLoad = trLoad;
7
8
  const promises_1 = require("node:fs/promises");
9
+ const promises_2 = require("node:fs/promises");
8
10
  const parseCsv = require('papaparse').parse;
9
11
  exports.DefaultOptions = {
10
12
  ucFirst: true
@@ -48,11 +50,19 @@ function tr(text, parts, options) {
48
50
  }
49
51
  return firstSpaces + translated + lastSpaces;
50
52
  }
51
- function trInit(lang, file) {
53
+ function trInit(lang) {
52
54
  language = lang;
53
55
  exports.expressions.clear();
54
56
  exports.translations.clear();
55
- (0, promises_1.readFile)(file, 'utf-8')
57
+ }
58
+ async function trLoad(file) {
59
+ try {
60
+ await (0, promises_1.access)(file);
61
+ }
62
+ catch {
63
+ return;
64
+ }
65
+ return (0, promises_2.readFile)(file, 'utf-8')
56
66
  .then((data) => parseCsv(data, { delimiter: ';' }).data)
57
67
  .then(data => data.forEach(row => {
58
68
  exports.translations.set(row[0], row[1]);
@@ -7,4 +7,5 @@ export type Options = {
7
7
  };
8
8
  export declare function tr(text: string, options: Options): string;
9
9
  export declare function tr(text: string, parts?: string[], options?: Options): string;
10
- export declare function trInit(lang: string, file: string): void;
10
+ export declare function trInit(lang: string): void;
11
+ export declare function trLoad(file: string): Promise<void>;
package/esm/translate.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createRequire } from 'module'
2
2
  const require = createRequire(import.meta.url)
3
+ import { access } from 'node:fs/promises';
3
4
  import { readFile } from 'node:fs/promises';
4
5
  const parseCsv = require('papaparse').parse;
5
6
  export const DefaultOptions = {
@@ -44,11 +45,19 @@ export function tr(text, parts, options) {
44
45
  }
45
46
  return firstSpaces + translated + lastSpaces;
46
47
  }
47
- export function trInit(lang, file) {
48
+ export function trInit(lang) {
48
49
  language = lang;
49
50
  expressions.clear();
50
51
  translations.clear();
51
- readFile(file, 'utf-8')
52
+ }
53
+ export async function trLoad(file) {
54
+ try {
55
+ await access(file);
56
+ }
57
+ catch {
58
+ return;
59
+ }
60
+ return readFile(file, 'utf-8')
52
61
  .then((data) => parseCsv(data, { delimiter: ';' }).data)
53
62
  .then(data => data.forEach(row => {
54
63
  translations.set(row[0], row[1]);
package/package.json CHANGED
@@ -50,5 +50,5 @@
50
50
  "build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
51
51
  },
52
52
  "types": "esm/translate.js",
53
- "version": "0.0.5"
53
+ "version": "0.0.6"
54
54
  }