Package org.opencms.ai
Class CmsAiTranslator
java.lang.Object
org.opencms.ai.CmsAiTranslator
Translates OpenCms XML content using an external AI provider.
- Since:
- 21.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHolder for parsed HTML text nodes and helper operations. -
Constructor Summary
ConstructorsConstructorDescriptionCmsAiTranslator(CmsObject cms, CmsAiProviderConfig config, CmsXmlContent xmlContent) Creates a new translator for the given provider configuration. -
Method Summary
Modifier and TypeMethodDescriptionGets the fields which couldn't be updated with a translation because of conflicts.intGets the number of fields which were successfully updated with their translation.static booleanhasHtmlMarkup(String text) Returns true if the given text contains HTML markup.parseHtmlTextNodes(String text) Parses HTML text and collects its text nodes.parseTranslationResult(String jsonText) Parses the translation response JSON into a map of xpath to text.translateXmlContent(Locale srcLocale, Locale targetLocale) translateXmlContent(Locale srcLocale, Locale targetLocale, dev.langchain4j.model.chat.response.StreamingChatResponseHandler handler) Translates XML content and applies the results to the target locale.protected StringtranslateXmlContentRaw(Locale srcLocale, Locale targetLocale, dev.langchain4j.model.chat.response.StreamingChatResponseHandler handler) Translates XML content with the AI provider and returns the raw response.
-
Constructor Details
-
CmsAiTranslator
Creates a new translator for the given provider configuration.- Parameters:
config- the provider configuration
-
-
Method Details
-
hasHtmlMarkup
Returns true if the given text contains HTML markup.- Parameters:
text- the text to check- Returns:
- true if markup is detected
-
parseHtmlTextNodes
Parses HTML text and collects its text nodes.- Parameters:
text- the input text- Returns:
- the parse result
-
parseTranslationResult
Parses the translation response JSON into a map of xpath to text.- Parameters:
jsonText- the JSON text to parse- Returns:
- the translated values mapped by id
- Throws:
JSONException- if the JSON is invalid
-
getConflictFields
Gets the fields which couldn't be updated with a translation because of conflicts.- Returns:
- the list of fields which couldn't be updated due to conflicts
-
getNumSuccessfulFieldUpdates
Gets the number of fields which were successfully updated with their translation.- Returns:
- the number of updated fields
-
translateXmlContent
public CmsXmlContent translateXmlContent(Locale srcLocale, Locale targetLocale) throws JSONException, CmsXmlException, CmsAiException -
translateXmlContent
public CmsXmlContent translateXmlContent(Locale srcLocale, Locale targetLocale, dev.langchain4j.model.chat.response.StreamingChatResponseHandler handler) throws JSONException, CmsXmlException, CmsAiException Translates XML content and applies the results to the target locale.- Parameters:
srcLocale- the source localetargetLocale- the target locale- Returns:
- the updated XML content
- Throws:
JSONException- if the JSON request or response is invalidCmsXmlException- in case of problems accessing the XML contentCmsAiException- if the AI translation result does not match the required structure
-
translateXmlContentRaw
protected String translateXmlContentRaw(Locale srcLocale, Locale targetLocale, dev.langchain4j.model.chat.response.StreamingChatResponseHandler handler) throws JSONException Translates XML content with the AI provider and returns the raw response.- Parameters:
srcLocale- the source localetargetLocale- the target locale- Returns:
- the raw AI response
- Throws:
JSONException- in the unlikely case of problems generating a JSON object for the translation request
-