@koine/i18n 2.0.0-beta.22

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.
@@ -0,0 +1,5 @@
1
+ import { I18nIndexedFile } from "./i18nGetFsData.js";
2
+ export declare function i18nGenerateTypes(options: {
3
+ defaultLocale: string;
4
+ files: I18nIndexedFile[];
5
+ }): Promise<string>;
@@ -0,0 +1,67 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import { format } from "prettier";
3
+ var filterTranslationKey = function (key) {
4
+ return key.endsWith("_one") || key.endsWith("_zero") || key.endsWith("_other");
5
+ };
6
+ function getType(value, options) {
7
+ if (options === void 0) { options = {}; }
8
+ var out = "";
9
+ var primitiveType = "";
10
+ if (typeof value === "boolean") {
11
+ primitiveType = "boolean";
12
+ }
13
+ else if (typeof value === "string") {
14
+ primitiveType = "string";
15
+ }
16
+ if (primitiveType) {
17
+ out += primitiveType + ";";
18
+ }
19
+ else if (!value) {
20
+ out += "";
21
+ }
22
+ else if (Array.isArray(value)) {
23
+ var firstValue = value[0];
24
+ out += "".concat(getType(firstValue, options), "[];");
25
+ }
26
+ else if (typeof value === "object") {
27
+ out += "{";
28
+ for (var _key in value) {
29
+ var key = _key;
30
+ if (!filterTranslationKey(key)) {
31
+ var single = value[key];
32
+ out += "'".concat(key, "': ").concat(getType(single, options));
33
+ }
34
+ }
35
+ out += "};";
36
+ }
37
+ out = out.replace(/;\[\];/g, "[];");
38
+ out = out.replace(/;+/g, ";");
39
+ return out;
40
+ }
41
+ export function i18nGenerateTypes(options) {
42
+ return __awaiter(this, void 0, void 0, function () {
43
+ var defaultLocale, files, defaultLocaleFiles, header, footer, out, i, _a, path, data, namespace;
44
+ return __generator(this, function (_b) {
45
+ switch (_b.label) {
46
+ case 0:
47
+ defaultLocale = options.defaultLocale, files = options.files;
48
+ defaultLocaleFiles = files.filter(function (f) { return f.locale === defaultLocale; });
49
+ header = "\n/* eslint-disable @typescript-eslint/ban-types */\n/* eslint-disable @typescript-eslint/no-namespace */\n\ndeclare namespace Koine {\n interface Translations {\n";
50
+ footer = "\n }\n}\n";
51
+ out = header;
52
+ for (i = 0; i < defaultLocaleFiles.length; i++) {
53
+ _a = defaultLocaleFiles[i], path = _a.path, data = _a.data;
54
+ namespace = path.replace(".json", "");
55
+ out += "\"".concat(namespace, "\": ").concat(getType(data), "\n");
56
+ }
57
+ out += footer;
58
+ return [4, format(out, {
59
+ parser: "typescript",
60
+ })];
61
+ case 1:
62
+ out = _b.sent();
63
+ return [2, out];
64
+ }
65
+ });
66
+ });
67
+ }
@@ -0,0 +1,17 @@
1
+ import { I18nIndexedLocale } from "./i18nGetLocalesFolders.js";
2
+ export type I18nIndexedFile = {
3
+ path: string;
4
+ locale: string;
5
+ data: {
6
+ [key: string]: any;
7
+ };
8
+ };
9
+ type I18nGetFsDataOutput = {
10
+ locales: I18nIndexedLocale[];
11
+ files: I18nIndexedFile[];
12
+ };
13
+ export declare function i18nGetFsData(options: {
14
+ cwd: string;
15
+ onlyFilesForLocales?: string[];
16
+ }): Promise<I18nGetFsDataOutput>;
17
+ export {};
@@ -0,0 +1,62 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import { readFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { glob } from "glob";
5
+ import { i18nGetLocalesFolders, } from "./i18nGetLocalesFolders.js";
6
+ export function i18nGetFsData(options) {
7
+ return __awaiter(this, void 0, void 0, function () {
8
+ var cwd, _a, onlyFilesForLocales, locales, dataOutput;
9
+ var _this = this;
10
+ return __generator(this, function (_b) {
11
+ switch (_b.label) {
12
+ case 0:
13
+ cwd = options.cwd, _a = options.onlyFilesForLocales, onlyFilesForLocales = _a === void 0 ? [] : _a;
14
+ return [4, i18nGetLocalesFolders({ cwd: cwd })];
15
+ case 1:
16
+ locales = _b.sent();
17
+ dataOutput = { locales: locales, files: [] };
18
+ if (onlyFilesForLocales)
19
+ locales = locales.filter(function (l) { return onlyFilesForLocales.includes(l.code); });
20
+ return [4, Promise.all(locales.map(function (locale) { return __awaiter(_this, void 0, void 0, function () {
21
+ var jsonFiles;
22
+ var _this = this;
23
+ return __generator(this, function (_a) {
24
+ switch (_a.label) {
25
+ case 0: return [4, glob("**/*.json", {
26
+ cwd: locale.path,
27
+ })];
28
+ case 1:
29
+ jsonFiles = _a.sent();
30
+ return [4, Promise.all(jsonFiles.map(function (relativePath) { return __awaiter(_this, void 0, void 0, function () {
31
+ var fullPath, rawContent;
32
+ return __generator(this, function (_a) {
33
+ switch (_a.label) {
34
+ case 0:
35
+ fullPath = join(locale.path, relativePath);
36
+ return [4, readFile(fullPath, "utf8")];
37
+ case 1:
38
+ rawContent = _a.sent();
39
+ if (rawContent) {
40
+ dataOutput.files.push({
41
+ path: relativePath,
42
+ data: JSON.parse(rawContent),
43
+ locale: locale.code,
44
+ });
45
+ }
46
+ return [2];
47
+ }
48
+ });
49
+ }); }))];
50
+ case 2:
51
+ _a.sent();
52
+ return [2];
53
+ }
54
+ });
55
+ }); }))];
56
+ case 2:
57
+ _b.sent();
58
+ return [2, dataOutput];
59
+ }
60
+ });
61
+ });
62
+ }
@@ -0,0 +1,7 @@
1
+ export type I18nIndexedLocale = {
2
+ path: string;
3
+ code: string;
4
+ };
5
+ export declare function i18nGetLocalesFolders(options: {
6
+ cwd: string;
7
+ }): Promise<I18nIndexedLocale[]>;
@@ -0,0 +1,29 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import { join } from "node:path";
3
+ import { glob } from "glob";
4
+ var ignoredFolderNames = ["node_modules"];
5
+ export function i18nGetLocalesFolders(options) {
6
+ return __awaiter(this, void 0, void 0, function () {
7
+ var cwd, folders, output;
8
+ return __generator(this, function (_a) {
9
+ switch (_a.label) {
10
+ case 0:
11
+ cwd = options.cwd;
12
+ return [4, glob("*", {
13
+ cwd: cwd,
14
+ withFileTypes: true,
15
+ })];
16
+ case 1:
17
+ folders = (_a.sent())
18
+ .filter(function (folder) { return folder.isDirectory(); })
19
+ .map(function (path) { return path.relative(); })
20
+ .filter(function (path) { return !ignoredFolderNames.includes(path); });
21
+ output = folders.map(function (locale) { return ({
22
+ path: join(cwd, locale),
23
+ code: locale,
24
+ }); });
25
+ return [2, output];
26
+ }
27
+ });
28
+ });
29
+ }
@@ -0,0 +1,8 @@
1
+ export declare function i18nWriteTypes(options: {
2
+ cwd: string;
3
+ defaultLocale: string;
4
+ outputTypes: string;
5
+ }): Promise<{
6
+ locales: import("./i18nGetLocalesFolders").I18nIndexedLocale[];
7
+ files: import("./i18nGetFsData").I18nIndexedFile[];
8
+ }>;
@@ -0,0 +1,29 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import { join } from "node:path";
3
+ import { fsWrite } from "@koine/node";
4
+ import { i18nGenerateTypes } from "./i18nGenerateTypes.js";
5
+ import { i18nGetFsData } from "./i18nGetFsData.js";
6
+ export function i18nWriteTypes(options) {
7
+ return __awaiter(this, void 0, void 0, function () {
8
+ var cwd, defaultLocale, outputTypes, data, types;
9
+ return __generator(this, function (_a) {
10
+ switch (_a.label) {
11
+ case 0:
12
+ cwd = options.cwd, defaultLocale = options.defaultLocale, outputTypes = options.outputTypes;
13
+ return [4, i18nGetFsData({
14
+ cwd: cwd,
15
+ onlyFilesForLocales: [defaultLocale],
16
+ })];
17
+ case 1:
18
+ data = _a.sent();
19
+ return [4, i18nGenerateTypes({ files: data.files, defaultLocale: defaultLocale })];
20
+ case 2:
21
+ types = _a.sent();
22
+ return [4, fsWrite(join(cwd, outputTypes), types)];
23
+ case 3:
24
+ _a.sent();
25
+ return [2, data];
26
+ }
27
+ });
28
+ });
29
+ }
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { i18nGenerateTypes } from "./i18nGenerateTypes.js";
2
+ export { i18nGetFsData } from "./i18nGetFsData.js";
3
+ export { i18nGetLocalesFolders } from "./i18nGetLocalesFolders.js";
4
+ export { i18nWriteTypes } from "./i18nWriteTypes.js";
package/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { i18nGenerateTypes } from "./i18nGenerateTypes.js";
2
+ export { i18nGetFsData } from "./i18nGetFsData.js";
3
+ export { i18nGetLocalesFolders } from "./i18nGetLocalesFolders.js";
4
+ export { i18nWriteTypes } from "./i18nWriteTypes.js";
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@koine/i18n",
3
+ "sideEffects": false,
4
+ "dependencies": {
5
+ "@koine/node": "2.0.0-beta.22"
6
+ },
7
+ "module": "./index.js",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./index.js"
12
+ },
13
+ "./i18nGenerateTypes": {
14
+ "import": "./i18nGenerateTypes.js"
15
+ },
16
+ "./i18nGetFsData": {
17
+ "import": "./i18nGetFsData.js"
18
+ },
19
+ "./i18nGetLocalesFolders": {
20
+ "import": "./i18nGetLocalesFolders.js"
21
+ },
22
+ "./i18nWriteTypes": {
23
+ "import": "./i18nWriteTypes.js"
24
+ }
25
+ },
26
+ "peerDependencies": {},
27
+ "version": "2.0.0-beta.22"
28
+ }