-
Notifications
You must be signed in to change notification settings - Fork 16
Added new param state to translation parses #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,63 +4,68 @@ import { MessagesLoaderV2 } from '../schema/messagesLoaderV2' | |||||
|
||||||
export const CONTEXT_REGEX = /\(\(\((?<context>(.)*)\)\)\)/ | ||||||
export const FROM_REGEX = /\<\<\<(?<from>(.)*)\>\>\>/ | ||||||
export const CONTENT_REGEX = /\(\(\((?<context>(.)*)\)\)\)|\<\<\<(?<from>(.)*)\>\>\>/g | ||||||
export const STATE_REGEX = /\[\[\[(?<state>(.)*)\]\]\]/ | ||||||
export const CONTENT_REGEX = /\(\(\((?<context>(.)*)\)\)\)|\<\<\<(?<from>(.)*)\>\>\>|\[\[\[(?<state>(.)*)\]\]\]/g | ||||||
|
||||||
export interface TranslatableMessageV2 { | ||||||
from?: string | ||||||
content: string | ||||||
context?: string | ||||||
state?: 'original' | 'translated' | ||||||
} | ||||||
|
||||||
export type TranslationDirectiveType = 'translatableV2' | 'translateTo' | ||||||
|
||||||
export const parseTranslatableStringV2 = (rawMessage: string): TranslatableMessageV2 => { | ||||||
const context = rawMessage.match(CONTEXT_REGEX)?.groups?.context | ||||||
const from = rawMessage.match(FROM_REGEX)?.groups?.from | ||||||
const state = rawMessage.match(STATE_REGEX)?.groups?.state | ||||||
const content = rawMessage.replace(CONTENT_REGEX, '') | ||||||
|
||||||
return { | ||||||
content: content?.trim(), | ||||||
context: context?.trim(), | ||||||
from: from?.trim(), | ||||||
state: (state?.trim() === 'translated' ? 'translated' : 'original') as 'translated' | 'original', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type assertion is redundant since the ternary operator already ensures the return type matches the union type. Consider simplifying to:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
} | ||||||
|
||||||
export const formatTranslatableStringV2 = ({ from, content, context }: TranslatableMessageV2): string => | ||||||
`${content} ${context ? `(((${context})))` : ''} ${from ? `<<<${from}>>>` : ''}` | ||||||
export const formatTranslatableStringV2 = ({ from, content, context, state }: TranslatableMessageV2): string => | ||||||
`${content} ${context ? `(((${context})))` : ''} ${from ? `<<<${from}>>>` : ''} ${state ? `[[[${state}]]]` : ''}` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The formatting function includes the state marker even when state is 'original', which may add unnecessary markers to strings. Consider only including the state marker when it's 'translated' to keep the output cleaner for the default case.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
export const handleSingleString = ( | ||||||
ctx: IOContext, | ||||||
loader: MessagesLoaderV2, | ||||||
behavior: Behavior, | ||||||
directiveName: TranslationDirectiveType | ||||||
) => async (rawMessage: string | null) => { | ||||||
// Messages only know how to process non empty strings. | ||||||
if (rawMessage == null) { | ||||||
return rawMessage | ||||||
} | ||||||
export const handleSingleString = | ||||||
(ctx: IOContext, loader: MessagesLoaderV2, behavior: Behavior, directiveName: TranslationDirectiveType) => | ||||||
async (rawMessage: string | null) => { | ||||||
// Messages only know how to process non empty strings. | ||||||
if (rawMessage == null) { | ||||||
return rawMessage | ||||||
} | ||||||
|
||||||
const { content, context, from: maybeFrom } = parseTranslatableStringV2(rawMessage) | ||||||
const { binding, tenant } = ctx | ||||||
const { content, context, from: maybeFrom, state } = parseTranslatableStringV2(rawMessage) | ||||||
const { binding, tenant } = ctx | ||||||
|
||||||
if (content == null) { | ||||||
throw new Error( | ||||||
`@${directiveName} directive needs a content to translate, but received ${JSON.stringify(rawMessage)}` | ||||||
) | ||||||
} | ||||||
if (content == null) { | ||||||
throw new Error( | ||||||
`@${directiveName} directive needs a content to translate, but received ${JSON.stringify(rawMessage)}` | ||||||
) | ||||||
} | ||||||
|
||||||
const from = maybeFrom || binding?.locale || tenant?.locale | ||||||
const from = maybeFrom || binding?.locale || tenant?.locale | ||||||
|
||||||
if (from == null) { | ||||||
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` | ||||||
) | ||||||
} | ||||||
if (from == null) { | ||||||
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` | ||||||
) | ||||||
} | ||||||
|
||||||
return loader.load({ | ||||||
behavior, | ||||||
content, | ||||||
context, | ||||||
from, | ||||||
}) | ||||||
} | ||||||
if (state === 'translated') { | ||||||
return content | ||||||
} | ||||||
renatoalmeidav marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
return loader.load({ | ||||||
behavior, | ||||||
content, | ||||||
context, | ||||||
from, | ||||||
}) | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.