@lingui/format-json 4.0.0-next.2

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.
package/README.md ADDED
@@ -0,0 +1,95 @@
1
+ [![License][badge-license]][license]
2
+ [![Version][badge-version]][package]
3
+ [![Downloads][badge-downloads]][package]
4
+
5
+ # @lingui/format-json
6
+
7
+ > Read and write message catalogs in JSON
8
+
9
+ `@lingui/format-json` is part of [LinguiJS][linguijs]. See the
10
+ [documentation][documentation] for all information, tutorials and examples.
11
+
12
+ ## Installation
13
+
14
+ ```sh
15
+ npm install --save-dev @lingui/format-json
16
+ # yarn add --dev @lingui/format-json
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```js
22
+ // lingui.config.{js,ts}
23
+ import {formatter} from "@lingui/format-json"
24
+
25
+ export default {
26
+ [...]
27
+ format: formatter({style: "lingui"}),
28
+ }
29
+ ```
30
+
31
+ Possible options:
32
+
33
+ ```ts
34
+ export type JsonFormatterOptions = {
35
+ /**
36
+ * Print places where message is used
37
+ *
38
+ * @default true
39
+ */
40
+ origins?: boolean
41
+
42
+ /**
43
+ * Print line numbers in origins
44
+ *
45
+ * @default true
46
+ */
47
+ lineNumbers?: boolean
48
+
49
+ /**
50
+ * Different styles of how information could be printed
51
+ *
52
+ * @default "lingui"
53
+ */
54
+ style?: "lingui" | "minimal"
55
+ }
56
+ ```
57
+
58
+
59
+ ### Style: minimal
60
+
61
+ Simple JSON with message ID -> translation mapping. All metadata (default message, comments for translators, message origin, etc) are stripped:
62
+
63
+ ```json
64
+ {
65
+ "MessageID": "Translated Message"
66
+ }
67
+ ```
68
+
69
+ ### Style: lingui
70
+
71
+ Raw catalog data serialized to JSON:
72
+
73
+ ```json
74
+ {
75
+ "MessageID": {
76
+ "translation": "Translated Message",
77
+ "message": "Default string (from source code)",
78
+ "origin": [
79
+ ["path/to/src.js", 42]
80
+ ]
81
+ }
82
+ }
83
+ ```
84
+
85
+ ## License
86
+
87
+ This package is licensed under [MIT][license] license.
88
+
89
+ [license]: https://github.com/lingui/js-lingui/blob/main/LICENSE
90
+ [linguijs]: https://github.com/lingui/js-lingui
91
+ [documentation]: https://lingui.dev
92
+ [package]: https://www.npmjs.com/package/@lingui/format-json
93
+ [badge-downloads]: https://img.shields.io/npm/dw/@lingui/format-json.svg
94
+ [badge-version]: https://img.shields.io/npm/v/@lingui/format-json.svg
95
+ [badge-license]: https://img.shields.io/npm/l/@lingui/format-json.svg
package/dist/json.cjs ADDED
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ const R = require('ramda');
4
+
5
+ function _interopNamespaceDefault(e) {
6
+ const n = Object.create(null);
7
+ if (e) {
8
+ for (const k in e) {
9
+ n[k] = e[k];
10
+ }
11
+ }
12
+ n.default = e;
13
+ return n;
14
+ }
15
+
16
+ const R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
17
+
18
+ const serializeMinimal = R__namespace.map(
19
+ (message) => message.translation || ""
20
+ );
21
+ const deserializeMinimal = R__namespace.map((translation) => ({
22
+ translation,
23
+ obsolete: false,
24
+ message: null,
25
+ origin: []
26
+ }));
27
+ const removeOrigins = R__namespace.map(({ origin, ...message }) => message);
28
+ const removeLineNumbers = R__namespace.map((message) => {
29
+ if (message.origin) {
30
+ message.origin = message.origin.map(([file]) => [file]);
31
+ }
32
+ return message;
33
+ });
34
+ function formatter(options = {}) {
35
+ options = {
36
+ origins: true,
37
+ lineNumbers: true,
38
+ ...options
39
+ };
40
+ return {
41
+ catalogExtension: ".json",
42
+ serialize(catalog, { existing }) {
43
+ let outputCatalog = catalog;
44
+ if (options.origins === false) {
45
+ outputCatalog = removeOrigins(outputCatalog);
46
+ }
47
+ if (options.origins !== false && options.lineNumbers === false) {
48
+ outputCatalog = removeLineNumbers(outputCatalog);
49
+ }
50
+ const shouldUseTrailingNewline = existing === null || existing?.endsWith("\n");
51
+ const trailingNewLine = shouldUseTrailingNewline ? "\n" : "";
52
+ if (options.style === "minimal") {
53
+ outputCatalog = serializeMinimal(outputCatalog);
54
+ }
55
+ return JSON.stringify(outputCatalog, null, 2) + trailingNewLine;
56
+ },
57
+ parse(content) {
58
+ const catalog = JSON.parse(content);
59
+ if (options.style === "minimal") {
60
+ return deserializeMinimal(catalog);
61
+ }
62
+ return catalog;
63
+ }
64
+ };
65
+ }
66
+
67
+ exports.formatter = formatter;
package/dist/json.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { CatalogFormatter } from '@lingui/conf';
2
+
3
+ type JsonFormatterOptions = {
4
+ /**
5
+ * Print places where message is used
6
+ *
7
+ * @default true
8
+ */
9
+ origins?: boolean;
10
+ /**
11
+ * Print line numbers in origins
12
+ *
13
+ * @default true
14
+ */
15
+ lineNumbers?: boolean;
16
+ /**
17
+ * Different styles of how information could be printed
18
+ *
19
+ * @default "lingui"
20
+ */
21
+ style?: "lingui" | "minimal";
22
+ };
23
+ declare function formatter(options?: JsonFormatterOptions): CatalogFormatter;
24
+
25
+ export { JsonFormatterOptions, formatter };
package/dist/json.mjs ADDED
@@ -0,0 +1,52 @@
1
+ import * as R from 'ramda';
2
+
3
+ const serializeMinimal = R.map(
4
+ (message) => message.translation || ""
5
+ );
6
+ const deserializeMinimal = R.map((translation) => ({
7
+ translation,
8
+ obsolete: false,
9
+ message: null,
10
+ origin: []
11
+ }));
12
+ const removeOrigins = R.map(({ origin, ...message }) => message);
13
+ const removeLineNumbers = R.map((message) => {
14
+ if (message.origin) {
15
+ message.origin = message.origin.map(([file]) => [file]);
16
+ }
17
+ return message;
18
+ });
19
+ function formatter(options = {}) {
20
+ options = {
21
+ origins: true,
22
+ lineNumbers: true,
23
+ ...options
24
+ };
25
+ return {
26
+ catalogExtension: ".json",
27
+ serialize(catalog, { existing }) {
28
+ let outputCatalog = catalog;
29
+ if (options.origins === false) {
30
+ outputCatalog = removeOrigins(outputCatalog);
31
+ }
32
+ if (options.origins !== false && options.lineNumbers === false) {
33
+ outputCatalog = removeLineNumbers(outputCatalog);
34
+ }
35
+ const shouldUseTrailingNewline = existing === null || existing?.endsWith("\n");
36
+ const trailingNewLine = shouldUseTrailingNewline ? "\n" : "";
37
+ if (options.style === "minimal") {
38
+ outputCatalog = serializeMinimal(outputCatalog);
39
+ }
40
+ return JSON.stringify(outputCatalog, null, 2) + trailingNewLine;
41
+ },
42
+ parse(content) {
43
+ const catalog = JSON.parse(content);
44
+ if (options.style === "minimal") {
45
+ return deserializeMinimal(catalog);
46
+ }
47
+ return catalog;
48
+ }
49
+ };
50
+ }
51
+
52
+ export { formatter };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@lingui/format-json",
3
+ "version": "4.0.0-next.2",
4
+ "description": "JSON format for Lingui Catalogs",
5
+ "main": "./dist/json.cjs",
6
+ "module": "./dist/json.mjs",
7
+ "types": "./dist/json.d.ts",
8
+ "license": "MIT",
9
+ "keywords": [
10
+ "i18n",
11
+ "lingui-format",
12
+ "lingui-formatter",
13
+ "json",
14
+ "internationalization",
15
+ "i10n",
16
+ "localization",
17
+ "i9n",
18
+ "translation"
19
+ ],
20
+ "scripts": {
21
+ "build": "rimraf ./dist && unbuild",
22
+ "stub": "unbuild --stub"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/lingui/js-lingui.git"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/lingui/js-lingui/issues"
30
+ },
31
+ "engines": {
32
+ "node": ">=16.0.0"
33
+ },
34
+ "files": [
35
+ "LICENSE",
36
+ "README.md",
37
+ "dist/"
38
+ ],
39
+ "dependencies": {
40
+ "@lingui/conf": "4.0.0-next.2",
41
+ "ramda": "^0.28.0"
42
+ },
43
+ "devDependencies": {
44
+ "unbuild": "^1.1.2"
45
+ }
46
+ }