@@ -4,63 +4,68 @@ import { MessagesLoaderV2 } from '../schema/messagesLoaderV2'
44
55export const CONTEXT_REGEX = / \( \( \( (?< context > ( .) * ) \) \) \) /
66export const FROM_REGEX = / \< \< \< (?< from > ( .) * ) \> \> \> /
7- export const CONTENT_REGEX = / \( \( \( (?< context > ( .) * ) \) \) \) | \< \< \< (?< from > ( .) * ) \> \> \> / g
7+ export const STATE_REGEX = / \[ \[ \[ (?< state > ( .) * ) \] \] \] /
8+ export const CONTENT_REGEX = / \( \( \( (?< context > ( .) * ) \) \) \) | \< \< \< (?< from > ( .) * ) \> \> \> | \[ \[ \[ (?< state > ( .) * ) \] \] \] / g
89
910export interface TranslatableMessageV2 {
1011 from ?: string
1112 content : string
1213 context ?: string
14+ state ?: 'original' | 'translated'
1315}
1416
1517export type TranslationDirectiveType = 'translatableV2' | 'translateTo'
1618
1719export const parseTranslatableStringV2 = ( rawMessage : string ) : TranslatableMessageV2 => {
1820 const context = rawMessage . match ( CONTEXT_REGEX ) ?. groups ?. context
1921 const from = rawMessage . match ( FROM_REGEX ) ?. groups ?. from
22+ const state = rawMessage . match ( STATE_REGEX ) ?. groups ?. state
2023 const content = rawMessage . replace ( CONTENT_REGEX , '' )
2124
2225 return {
2326 content : content ?. trim ( ) ,
2427 context : context ?. trim ( ) ,
2528 from : from ?. trim ( ) ,
29+ state : state ?. trim ( ) as 'original' | 'translated' | undefined ,
2630 }
2731}
2832
29- export const formatTranslatableStringV2 = ( { from, content, context } : TranslatableMessageV2 ) : string =>
30- `${ content } ${ context ? `(((${ context } )))` : '' } ${ from ? `<<<${ from } >>>` : '' } `
33+ export const formatTranslatableStringV2 = ( { from, content, context, state } : TranslatableMessageV2 ) : string =>
34+ `${ content } ${ context ? `(((${ context } )))` : '' } ${ from ? `<<<${ from } >>>` : '' } ${ state ? `[[[ ${ state } ]]]` : '' } `
3135
32- export const handleSingleString = (
33- ctx : IOContext ,
34- loader : MessagesLoaderV2 ,
35- behavior : Behavior ,
36- directiveName : TranslationDirectiveType
37- ) => async ( rawMessage : string | null ) => {
38- // Messages only know how to process non empty strings.
39- if ( rawMessage == null ) {
40- return rawMessage
41- }
36+ export const handleSingleString =
37+ ( ctx : IOContext , loader : MessagesLoaderV2 , behavior : Behavior , directiveName : TranslationDirectiveType ) =>
38+ async ( rawMessage : string | null ) => {
39+ // Messages only know how to process non empty strings.
40+ if ( rawMessage == null ) {
41+ return rawMessage
42+ }
4243
43- const { content, context, from : maybeFrom } = parseTranslatableStringV2 ( rawMessage )
44- const { binding, tenant } = ctx
44+ const { content, context, from : maybeFrom , state } = parseTranslatableStringV2 ( rawMessage )
45+ const { binding, tenant } = ctx
4546
46- if ( content == null ) {
47- throw new Error (
48- `@${ directiveName } directive needs a content to translate, but received ${ JSON . stringify ( rawMessage ) } `
49- )
50- }
47+ if ( content == null ) {
48+ throw new Error (
49+ `@${ directiveName } directive needs a content to translate, but received ${ JSON . stringify ( rawMessage ) } `
50+ )
51+ }
5152
52- const from = maybeFrom || binding ?. locale || tenant ?. locale
53+ const from = maybeFrom || binding ?. locale || tenant ?. locale
5354
54- if ( from == null ) {
55- throw new Error (
56- `@${ 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`
57- )
58- }
55+ if ( from == null ) {
56+ throw new Error (
57+ `@${ 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`
58+ )
59+ }
5960
60- return loader . load ( {
61- behavior,
62- content,
63- context,
64- from,
65- } )
66- }
61+ if ( state === 'translated' ) {
62+ return content
63+ }
64+
65+ return loader . load ( {
66+ behavior,
67+ content,
68+ context,
69+ from,
70+ } )
71+ }
0 commit comments