MenuQueries LibraryTranslate all properties of a post using ChatGPTTranslate all properties of a post using ChatGPTThis query fetches the properties from a single post, and translates those strings to any language using ChatGPT. To connect to the OpenAI API, you must provide variables $openAIAPIKey with the API key. You can override the $model variable ("gpt-4o-mini" by default) with the OpenAI model you desire to use. query GetPostProperties($postId: ID!) { post(by: { id: $postId }) { title content excerpt @export( as: "postProperties", affectAdditionalFieldsUnderPos: [1, 2] ) } } query TranslatePostPropertiesWithChatGPT( $fromLang: String! $toLang: String! $openAIAPIKey: String! $systemMessage: String! = "You are a language translator" $promptTemplate: String! = """ I'm working on internationalizing my application. I've created a JSON with sentences in {$fromLang}. Please translate the sentences to {$toLang}. Keep the object properties identical, translate the values only. If a sentence contains HTML, do not translate inside the HTML tags. This is the JSON: {$encodedPostProperties} """ $model: String! = "gpt-4o-mini" ) @depends(on: "GetPostProperties") { postProperties: _echo(value: $postProperties) encodedPostProperties: _objectEncodeAsJSONString(object: $postProperties) prompt: _strReplaceMultiple( search: ["{$fromLang}", "{$toLang}", "{$encodedPostProperties}"], replaceWith: [$fromLang, $toLang, $__encodedPostProperties], in: $promptTemplate ) openAIResponse: _sendJSONObjectItemHTTPRequest(input: { url: "https://api.openai.com/v1/chat/completions", method: POST, options: { auth: { password: $openAIAPIKey }, json: { model: $model, messages: [ { role: "system", content: $systemMessage }, { role: "user", content: $__prompt }, ], response_format: { type: "json_schema", json_schema: { name: "translation_response", strict: true, schema: { type: "object", properties: { translations: { type: "array", items: { type: "object", properties: { property: { type: "string" }, translation: { type: "string" } }, required: ["property", "translation"], additionalProperties: false } } }, required: ["translations"], additionalProperties: false } } } } } }) @underJSONObjectProperty(by: { key: "choices" }) @underArrayItem(index: 0) @underJSONObjectProperty(by: { path: "message.content" }) @export(as: "jsonEncodedTranslatedPostProperties") } query ExtractTranslatedPostProperties @depends(on: "TranslatePostPropertiesWithChatGPT") { jsonEncodedTranslatedPostProperties: _echo(value: $jsonEncodedTranslatedPostProperties) @remove decodedTranslatedPostProperties: _strDecodeJSONObject(string: $jsonEncodedTranslatedPostProperties) @remove translatedPostProperties: _objectProperty( object: $__decodedTranslatedPostProperties, by: { key: "translations" } ) translatedPostPropertiesAsJSON: _arrayOfJSONObjectsExtractPropertiesAndConvertToObject( array: $__translatedPostProperties, key: "property", value: "translation" ) }CopyPrevTranslate a post from the WordPress editor (Gutenberg)NextTranslate content from URL
This query fetches the properties from a single post, and translates those strings to any language using ChatGPT. To connect to the OpenAI API, you must provide variables $openAIAPIKey with the API key. You can override the $model variable ("gpt-4o-mini" by default) with the OpenAI model you desire to use. query GetPostProperties($postId: ID!) { post(by: { id: $postId }) { title content excerpt @export( as: "postProperties", affectAdditionalFieldsUnderPos: [1, 2] ) } } query TranslatePostPropertiesWithChatGPT( $fromLang: String! $toLang: String! $openAIAPIKey: String! $systemMessage: String! = "You are a language translator" $promptTemplate: String! = """ I'm working on internationalizing my application. I've created a JSON with sentences in {$fromLang}. Please translate the sentences to {$toLang}. Keep the object properties identical, translate the values only. If a sentence contains HTML, do not translate inside the HTML tags. This is the JSON: {$encodedPostProperties} """ $model: String! = "gpt-4o-mini" ) @depends(on: "GetPostProperties") { postProperties: _echo(value: $postProperties) encodedPostProperties: _objectEncodeAsJSONString(object: $postProperties) prompt: _strReplaceMultiple( search: ["{$fromLang}", "{$toLang}", "{$encodedPostProperties}"], replaceWith: [$fromLang, $toLang, $__encodedPostProperties], in: $promptTemplate ) openAIResponse: _sendJSONObjectItemHTTPRequest(input: { url: "https://api.openai.com/v1/chat/completions", method: POST, options: { auth: { password: $openAIAPIKey }, json: { model: $model, messages: [ { role: "system", content: $systemMessage }, { role: "user", content: $__prompt }, ], response_format: { type: "json_schema", json_schema: { name: "translation_response", strict: true, schema: { type: "object", properties: { translations: { type: "array", items: { type: "object", properties: { property: { type: "string" }, translation: { type: "string" } }, required: ["property", "translation"], additionalProperties: false } } }, required: ["translations"], additionalProperties: false } } } } } }) @underJSONObjectProperty(by: { key: "choices" }) @underArrayItem(index: 0) @underJSONObjectProperty(by: { path: "message.content" }) @export(as: "jsonEncodedTranslatedPostProperties") } query ExtractTranslatedPostProperties @depends(on: "TranslatePostPropertiesWithChatGPT") { jsonEncodedTranslatedPostProperties: _echo(value: $jsonEncodedTranslatedPostProperties) @remove decodedTranslatedPostProperties: _strDecodeJSONObject(string: $jsonEncodedTranslatedPostProperties) @remove translatedPostProperties: _objectProperty( object: $__decodedTranslatedPostProperties, by: { key: "translations" } ) translatedPostPropertiesAsJSON: _arrayOfJSONObjectsExtractPropertiesAndConvertToObject( array: $__translatedPostProperties, key: "property", value: "translation" ) }Copy