@nimpl/i18n 0.0.0-experimental-0b32faf → 0.0.0-experimental-47e75fe

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.
@@ -1,10 +1,9 @@
1
1
  import React from "react";
2
2
  import { type I18nOptions } from "./types";
3
+ import { type TranslationProps } from "./lib/Translation";
3
4
  type ClientTranslationProps = {
4
5
  term: string;
5
- components?: {
6
- [key: string]: React.ReactElement;
7
- };
6
+ components?: TranslationProps["components"];
8
7
  query?: I18nOptions["query"];
9
8
  removeUnusedQueries?: I18nOptions["removeUnusedQueries"];
10
9
  };
@@ -1,10 +1,9 @@
1
1
  import React from "react";
2
2
  import { type I18nOptions } from "./types";
3
+ import { type TranslationProps } from "./lib/Translation";
3
4
  type ServerTranslationProps = {
4
5
  term: string;
5
- components?: {
6
- [key: string]: React.ReactElement;
7
- };
6
+ components?: TranslationProps["components"];
8
7
  query?: I18nOptions["query"];
9
8
  removeUnusedQueries?: I18nOptions["removeUnusedQueries"];
10
9
  language?: string;
@@ -5,25 +5,59 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const react_1 = __importDefault(require("react"));
8
+ const parseTag = (tag) => {
9
+ const isSelfClosing = tag.endsWith("/>");
10
+ const isClosing = tag[1] === "/";
11
+ const start = isClosing ? 2 : 1;
12
+ const end = isSelfClosing
13
+ ? tag.indexOf(" ") !== -1 && tag.indexOf(" ") < tag.length - 2
14
+ ? tag.indexOf(" ")
15
+ : tag.length - 2
16
+ : tag.length - 1;
17
+ return {
18
+ name: tag.slice(start, end),
19
+ type: isSelfClosing ? "self" : isClosing ? "close" : "open",
20
+ };
21
+ };
22
+ const splitByTags = (text) => {
23
+ const parts = [];
24
+ const tags = [];
25
+ let current = "";
26
+ let i = 0;
27
+ while (i < text.length) {
28
+ if (text[i] === "<") {
29
+ const closeIndex = text.indexOf(">", i);
30
+ if (closeIndex !== -1) {
31
+ parts.push(current);
32
+ current = "";
33
+ tags.push(text.slice(i, closeIndex + 1));
34
+ i = closeIndex + 1;
35
+ continue;
36
+ }
37
+ }
38
+ current += text[i];
39
+ i++;
40
+ }
41
+ parts.push(current);
42
+ return { parts, tags };
43
+ };
8
44
  const Translation = ({ term, text, components }) => {
9
- const parts = text
10
- .split(/<\/?[a-zA-Z0-9]+>|<[a-zA-Z0-9]+ ?\/>/gm)
11
- .map((el, i) => react_1.default.createElement(react_1.default.Fragment, { key: `p-${i}` }, el));
45
+ const { parts: textParts, tags } = splitByTags(text);
46
+ const parts = textParts.map((el, i) => react_1.default.createElement(react_1.default.Fragment, { key: `p-${i}` }, el));
12
47
  if (components) {
13
- const tags = text.match(/<\/?[a-zA-Z0-9]+>|<[a-zA-Z0-9]+ ?\/>/gm);
14
48
  const openedTags = [];
15
- tags?.forEach((tag, tagIndex) => {
16
- const tagName = tag.match(/[a-zA-Z0-9]+/)[0];
17
- if (tag.match(/<[a-zA-Z0-9]+ ?\/>/)) {
18
- const component = components[tagName];
19
- if (component) {
20
- parts.splice(tagIndex + 1, 1, react_1.default.cloneElement(component, { key: `c-${tagIndex}` }, component.props.children));
49
+ tags.forEach((tag, tagIndex) => {
50
+ const { name: tagName, type: tagType } = parseTag(tag);
51
+ if (tagType === "self") {
52
+ const Component = components[tagName];
53
+ if (Component) {
54
+ parts.splice(tagIndex + 1, 1, react_1.default.createElement(Component, { key: `c-${tagIndex}` }));
21
55
  }
22
56
  else {
23
57
  console.warn(`Unknown component for term "${term}" - ${tagName}`);
24
58
  }
25
59
  }
26
- else if (tag.match(/<\/[a-zA-Z0-9]+>/)) {
60
+ else if (tagType === "close") {
27
61
  const openedTagIndex = openedTags.findIndex((i) => i.tag === tagName);
28
62
  if (openedTagIndex !== -1) {
29
63
  const lastOpenedIndex = openedTags.length - 1 - openedTagIndex;
@@ -31,13 +65,15 @@ const Translation = ({ term, text, components }) => {
31
65
  for (let i = openedTagsLength; i > lastOpenedIndex; i--) {
32
66
  const targetIndex = i - 1;
33
67
  const targetTag = openedTags[targetIndex];
34
- const component = components[targetTag.tag];
35
- if (component) {
68
+ const Component = components[targetTag.tag];
69
+ if (Component) {
36
70
  const children = parts
37
71
  .slice(targetTag.position + 1, tagIndex + 1)
38
- .filter((c) => Boolean(c && typeof c === "object" && "props" in c && c.props.children));
39
- parts.splice(targetTag.position + 1, // parts на 1 больше
40
- tagIndex - targetTag.position, react_1.default.cloneElement(component, { key: `${tagIndex}-${targetIndex}` }, children.length ? children : component.props.children));
72
+ .filter((child) => Boolean(child &&
73
+ typeof child === "object" &&
74
+ "props" in child &&
75
+ child.props.children));
76
+ parts.splice(targetTag.position + 1, tagIndex - targetTag.position, react_1.default.createElement(Component, { key: `${tagIndex}-${targetIndex}` }, children));
41
77
  }
42
78
  else {
43
79
  console.warn(`Unknown component for term "${term}" - ${targetTag}`);
@@ -1,17 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const injectQuery = ({ text, query, removeUnusedQueries }) => {
4
- const newText = text.replace(/{{([a-zA-Z0-9_-]+)}}/gm, (matched, g1) => {
5
- if (query[g1]) {
6
- return query[g1].toString();
4
+ let result = "";
5
+ let i = 0;
6
+ while (i < text.length) {
7
+ if (text[i] === "{" && text[i + 1] === "{") {
8
+ const closeIndex = text.indexOf("}}", i + 2);
9
+ if (closeIndex !== -1) {
10
+ const key = text.slice(i + 2, closeIndex);
11
+ const value = query[key];
12
+ if (value !== undefined) {
13
+ result += value.toString();
14
+ }
15
+ else {
16
+ console.warn(`Query key "${key}" not found in options for "${text}"`);
17
+ if (!removeUnusedQueries) {
18
+ result += text.slice(i, closeIndex + 2);
19
+ }
20
+ }
21
+ i = closeIndex + 2;
22
+ continue;
23
+ }
7
24
  }
8
- if (removeUnusedQueries) {
9
- return "";
10
- }
11
- else {
12
- return matched;
13
- }
14
- });
15
- return newText;
25
+ result += text[i];
26
+ i++;
27
+ }
28
+ return result;
16
29
  };
17
30
  exports.default = injectQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nimpl/i18n",
3
- "version": "0.0.0-experimental-0b32faf",
3
+ "version": "0.0.0-experimental-47e75fe",
4
4
  "description": "i18n library for working with translations in server and client components",
5
5
  "exports": {
6
6
  "./ClientTranslation": {