@vtex/api 7.2.5 → 7.2.6-beta.0
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.
|
@@ -2,13 +2,15 @@ import { Behavior } from '../../../../../clients';
|
|
|
2
2
|
import { IOContext } from '../../typings';
|
|
3
3
|
export declare const CONTEXT_REGEX: RegExp;
|
|
4
4
|
export declare const FROM_REGEX: RegExp;
|
|
5
|
+
export declare const STATE_REGEX: RegExp;
|
|
5
6
|
export declare const CONTENT_REGEX: RegExp;
|
|
6
7
|
export interface TranslatableMessageV2 {
|
|
7
8
|
from?: string;
|
|
8
9
|
content: string;
|
|
9
10
|
context?: string;
|
|
11
|
+
state?: 'original' | 'translated';
|
|
10
12
|
}
|
|
11
13
|
export type TranslationDirectiveType = 'translatableV2' | 'translateTo';
|
|
12
14
|
export declare const parseTranslatableStringV2: (rawMessage: string) => TranslatableMessageV2;
|
|
13
|
-
export declare const formatTranslatableStringV2: ({ from, content, context }: TranslatableMessageV2) => string;
|
|
15
|
+
export declare const formatTranslatableStringV2: ({ from, content, context, state }: TranslatableMessageV2) => string;
|
|
14
16
|
export declare const handleSingleString: (ctx: IOContext, loader: import("dataloader")<import("../../../../../clients").Message, string>, behavior: Behavior, directiveName: TranslationDirectiveType) => (rawMessage: string | null) => Promise<string | null>;
|
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleSingleString = exports.formatTranslatableStringV2 = exports.parseTranslatableStringV2 = exports.CONTENT_REGEX = exports.FROM_REGEX = exports.CONTEXT_REGEX = void 0;
|
|
3
|
+
exports.handleSingleString = exports.formatTranslatableStringV2 = exports.parseTranslatableStringV2 = exports.CONTENT_REGEX = exports.STATE_REGEX = exports.FROM_REGEX = exports.CONTEXT_REGEX = void 0;
|
|
4
4
|
exports.CONTEXT_REGEX = /\(\(\((?<context>(.)*)\)\)\)/;
|
|
5
5
|
exports.FROM_REGEX = /\<\<\<(?<from>(.)*)\>\>\>/;
|
|
6
|
-
exports.
|
|
6
|
+
exports.STATE_REGEX = /\[\[\[(?<state>(.)*)\]\]\]/;
|
|
7
|
+
exports.CONTENT_REGEX = /\(\(\((?<context>(.)*)\)\)\)|\<\<\<(?<from>(.)*)\>\>\>|\[\[\[(?<state>(.)*)\]\]\]/g;
|
|
7
8
|
const parseTranslatableStringV2 = (rawMessage) => {
|
|
8
|
-
var _a, _b, _c, _d;
|
|
9
|
+
var _a, _b, _c, _d, _e, _f;
|
|
9
10
|
const context = (_b = (_a = rawMessage.match(exports.CONTEXT_REGEX)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.context;
|
|
10
11
|
const from = (_d = (_c = rawMessage.match(exports.FROM_REGEX)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.from;
|
|
12
|
+
const state = (_f = (_e = rawMessage.match(exports.STATE_REGEX)) === null || _e === void 0 ? void 0 : _e.groups) === null || _f === void 0 ? void 0 : _f.state;
|
|
11
13
|
const content = rawMessage.replace(exports.CONTENT_REGEX, '');
|
|
12
14
|
return {
|
|
13
15
|
content: content === null || content === void 0 ? void 0 : content.trim(),
|
|
14
16
|
context: context === null || context === void 0 ? void 0 : context.trim(),
|
|
15
17
|
from: from === null || from === void 0 ? void 0 : from.trim(),
|
|
18
|
+
state: ((state === null || state === void 0 ? void 0 : state.trim()) === 'translated' ? 'translated' : 'original'),
|
|
16
19
|
};
|
|
17
20
|
};
|
|
18
21
|
exports.parseTranslatableStringV2 = parseTranslatableStringV2;
|
|
19
|
-
const formatTranslatableStringV2 = ({ from, content, context }) => `${content} ${context ? `(((${context})))` : ''} ${from ? `<<<${from}>>>` : ''}`;
|
|
22
|
+
const formatTranslatableStringV2 = ({ from, content, context, state }) => `${content} ${context ? `(((${context})))` : ''} ${from ? `<<<${from}>>>` : ''} ${state ? `[[[${state}]]]` : ''}`;
|
|
20
23
|
exports.formatTranslatableStringV2 = formatTranslatableStringV2;
|
|
21
24
|
const handleSingleString = (ctx, loader, behavior, directiveName) => async (rawMessage) => {
|
|
22
25
|
// Messages only know how to process non empty strings.
|
|
23
26
|
if (rawMessage == null) {
|
|
24
27
|
return rawMessage;
|
|
25
28
|
}
|
|
26
|
-
const { content, context, from: maybeFrom } = (0, exports.parseTranslatableStringV2)(rawMessage);
|
|
29
|
+
const { content, context, from: maybeFrom, state } = (0, exports.parseTranslatableStringV2)(rawMessage);
|
|
27
30
|
const { binding, tenant } = ctx;
|
|
28
31
|
if (content == null) {
|
|
29
32
|
throw new Error(`@${directiveName} directive needs a content to translate, but received ${JSON.stringify(rawMessage)}`);
|
|
@@ -32,6 +35,9 @@ const handleSingleString = (ctx, loader, behavior, directiveName) => async (rawM
|
|
|
32
35
|
if (from == null) {
|
|
33
36
|
throw new Error(`@${directiveName} directive needs a source language to translate from. You can do this by either setting ${'`ctx.vtex.tenant`'} variable, call this app with the header ${'`x-vtex-tenant`'} or format the string with the ${'`formatTranslatableStringV2`'} function with the ${'`from`'} option set`);
|
|
34
37
|
}
|
|
38
|
+
if (state === 'translated') {
|
|
39
|
+
return content;
|
|
40
|
+
}
|
|
35
41
|
return loader.load({
|
|
36
42
|
behavior,
|
|
37
43
|
content,
|