import { z } from 'zod'
import useTranslate from '../useTranslate'
import { createOutputSchema, createSuccessResponse, createErrorResponse } from './commonSchema'
const inputSchema = z.object({
key: z
.string()
.optional()
.describe(
'The unique key for the i18n entry to retrieve (optional). If provided, returns specific entry; if omitted, returns all entries.'
)
})
const i18nDataSchema = z.object({
entries: z.record(z.any()).describe('I18n entries object containing key-value pairs'),
count: z.number().describe('Total number of entries returned')
})
const outputSchema = createOutputSchema(i18nDataSchema)
export const getI18n = {
name: 'get_i18n',
title: '获取 I18n 词条',
description:
'Retrieve i18n entries from the current TinyEngine low-code application. Can get a specific entry by key or all entries if no key is provided.',
inputSchema: inputSchema.shape,
outputSchema: outputSchema.shape,
annotations: {
title: 'Get I18n Entries',
readOnlyHint: true,
openWorldHint: false
},
callback: async (args: z.infer<typeof inputSchema>) => {
const { key } = args
try {
const { getLangs } = useTranslate()
const langs = getLangs() as Record<string, any>
if (key) {
if (!langs[key]) {
return createErrorResponse('I18n key not found', `Key "${key}" does not exist in the i18n dictionary`)
}
const singleEntryData = {
entries: { [key]: langs[key] },
count: 1
}
return createSuccessResponse(`I18n entry for key "${key}" retrieved successfully`, singleEntryData)
}
const entryCount = Object.keys(langs).length
if (!entryCount) {
return createSuccessResponse('No i18n entries found', {
entries: {},
count: 0
})
}
const res = createSuccessResponse(`Retrieved ${entryCount} i18n entries`, {
entries: langs,
count: entryCount
})
return res
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
return createErrorResponse('Failed to retrieve i18n entries', errorMessage)
}
}
}