@itrocks/translate 0.0.4 → 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.
@@ -5,7 +5,7 @@ export declare function lang(): string;
5
5
  export type Options = {
6
6
  ucFirst?: boolean;
7
7
  };
8
- export { tr };
9
- export default function tr(text: string, options: Options): string;
10
- export default function tr(text: string, parts?: string[], options?: Options): string;
11
- export declare function trInit(lang: string, file: string): void;
8
+ export declare function tr(text: string, options: Options): string;
9
+ export declare function tr(text: string, parts?: string[], options?: Options): string;
10
+ export declare function trInit(lang: string): void;
11
+ export declare function trLoad(file: string): Promise<void>;
package/cjs/translate.js CHANGED
@@ -2,12 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.translations = exports.expressions = exports.DefaultOptions = void 0;
4
4
  exports.lang = lang;
5
- exports.default = tr;
6
- exports.tr = tr;
7
- exports.default = tr;
8
5
  exports.tr = tr;
9
6
  exports.trInit = trInit;
7
+ exports.trLoad = trLoad;
10
8
  const promises_1 = require("node:fs/promises");
9
+ const promises_2 = require("node:fs/promises");
11
10
  const parseCsv = require('papaparse').parse;
12
11
  exports.DefaultOptions = {
13
12
  ucFirst: true
@@ -51,11 +50,19 @@ function tr(text, parts, options) {
51
50
  }
52
51
  return firstSpaces + translated + lastSpaces;
53
52
  }
54
- function trInit(lang, file) {
53
+ function trInit(lang) {
55
54
  language = lang;
56
55
  exports.expressions.clear();
57
56
  exports.translations.clear();
58
- (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')
59
66
  .then((data) => parseCsv(data, { delimiter: ';' }).data)
60
67
  .then(data => data.forEach(row => {
61
68
  exports.translations.set(row[0], row[1]);
@@ -5,7 +5,7 @@ export declare function lang(): string;
5
5
  export type Options = {
6
6
  ucFirst?: boolean;
7
7
  };
8
- export { tr };
9
- export default function tr(text: string, options: Options): string;
10
- export default function tr(text: string, parts?: string[], options?: Options): string;
11
- export declare function trInit(lang: string, file: string): void;
8
+ export declare function tr(text: string, options: Options): string;
9
+ export declare function tr(text: string, parts?: string[], options?: Options): string;
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 = {
@@ -11,8 +12,7 @@ let language = 'en-US';
11
12
  export function lang() {
12
13
  return language;
13
14
  }
14
- export { tr };
15
- export default function tr(text, parts, options) {
15
+ export function tr(text, parts, options) {
16
16
  if (!parts) {
17
17
  parts = [];
18
18
  }
@@ -45,11 +45,19 @@ export default function tr(text, parts, options) {
45
45
  }
46
46
  return firstSpaces + translated + lastSpaces;
47
47
  }
48
- export function trInit(lang, file) {
48
+ export function trInit(lang) {
49
49
  language = lang;
50
50
  expressions.clear();
51
51
  translations.clear();
52
- 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')
53
61
  .then((data) => parseCsv(data, { delimiter: ';' }).data)
54
62
  .then(data => data.forEach(row => {
55
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.4"
53
+ "version": "0.0.6"
54
54
  }