Files
floci/docs/services/translate.md
T
Vũ Thế Huy 10192f05d5 feat(translate): add Amazon Translate mock service (#2879)
* feat(translate): add Amazon Translate mock service

JSON 1.1 stub for Amazon Translate covering the three self-contained sync
operations: TranslateText, TranslateDocument, ListLanguages. Follows the
existing Comprehend/Textract stub pattern (stateless, echoes input,
AI_MOCK_CONFIG override for TranslateText).

- TranslateText echoes Text, enforces the 10,000-byte limit, allows any
  language pair; an auto source is reported back as en.
- TranslateDocument echoes the base64 Content and enforces the English-pivot
  rule, the ContentType allow-list, valid base64, and the 100 KB cap
  (InvalidRequestException, matching the operation's error model). The
  encoded length is checked before decoding so an oversized payload cannot
  force a large allocation.
- ListLanguages returns a fixed ~35-language catalog; DisplayLanguageCode is
  validated, pagination inputs are accepted but ignored.

Untrusted request values are truncated before being echoed into error
messages.

* chore(translate): remove em-dashes per AGENTS.md style guide

Replace em-dashes in the Translate doc page and Java comments with colons,
commas, or periods, and the ellipsis in the error-message preview helper
with "...". Addresses the Greptile review note on PR #2879.
2026-09-01 14:47:33 -04:00

4.7 KiB

Translate

Protocol: JSON 1.1 (X-Amz-Target: AWSShineFrontendService_20170701.*) Endpoint: POST http://localhost:4566/

Supported Actions

Action Description
TranslateText Translate a UTF-8 text string between two languages
TranslateDocument Translate a document (≤100 KB) supplied as a base64 blob
ListLanguages List the language codes and names Floci recognizes

Emulation Behavior

  • No machine translation: TranslateText returns the request Text verbatim as TranslatedText, and TranslateDocument returns the request Document.Content bytes unchanged as TranslatedDocument.Content. To exercise logic that branches on a specific translation, use the mock config below.
  • Language detection: a SourceLanguageCode of auto is accepted and reported back as en (the stub's fixed "detected" language); no Comprehend call is made.
  • Real input validation: Text / Document.Content / Document.ContentType and both language codes are required. SourceLanguageCode must be auto or a code from the catalog returned by ListLanguages; TargetLanguageCode must be a catalog code. An unknown code returns UnsupportedLanguagePairException.
    • TranslateText rejects a Text longer than 10,000 UTF-8 bytes with TextSizeLimitExceededException, matching AWS. Any language pair is allowed (TranslateText is any-to-any).
    • TranslateDocument requires an English pivot: either the source or the target language must resolve to en, otherwise it returns UnsupportedLanguagePairException. It also rejects a ContentType other than text/plain, text/html, or the Word (.docx) MIME type, a Content that is not valid base64, and a decoded document over 100 KB, all with InvalidRequestException (TranslateDocument does not model TextSizeLimitExceededException).
  • Fixed catalog: ListLanguages returns a curated ~35-language subset of Translate's full list, enough to drive an i18n pipeline. MaxResults / NextToken are accepted but ignored (the whole catalog fits in one page, so no NextToken is returned). DisplayLanguageCode is validated against the AWS enum (de, en, es, fr, it, ja, ko, pt, zh, zh-TW) but every LanguageName is returned in English regardless.
  • Out of scope: custom terminology, parallel data, asynchronous batch translation jobs (StartTextTranslationJob and friends), and tagging, all needing persistent state.
  • Deviation: ListLanguages always returns LanguageNames in English regardless of DisplayLanguageCode (real Translate localizes them), and the Formality / Profanity / Brevity translation settings are accepted but ignored.

Configuration

Variable Default Description
FLOCI_SERVICES_TRANSLATE_ENABLED true Enable or disable the service
AI_MOCK_CONFIG unset Path to a shared mock-response config file; see "Mock Responses" below

Mock Responses

To return a real translated string instead of the echoed input, point AI_MOCK_CONFIG at a JSON file shared across Translate, Comprehend, Textract, and Rekognition:

{
  "translate": {
    "Hello world": {
      "TranslateText": { "TranslatedText": "Hola mundo", "SourceLanguageCode": "en", "TargetLanguageCode": "es" }
    }
  }
}

The lookup key is the exact Text value sent in the request, so only TranslateText can be mocked; TranslateDocument carries a binary payload with no stable key. The file is re-read when its modification time changes, so it can be edited without restarting the emulator. A missing file, an unset AI_MOCK_CONFIG, or no matching entry all fall back to the echoed-input default. Mocking is opt-in and never breaks a call that isn't using it.

Examples

export AWS_ENDPOINT_URL=http://localhost:4566

aws translate translate-text \
  --text "Floci makes local AWS testing painless" \
  --source-language-code en \
  --target-language-code fr \
  --endpoint-url $AWS_ENDPOINT_URL

aws translate list-languages \
  --endpoint-url $AWS_ENDPOINT_URL

SDK Example (Java)

TranslateClient translate = TranslateClient.builder()
    .endpointOverride(URI.create("http://localhost:4566"))
    .region(Region.US_EAST_1)
    .credentialsProvider(StaticCredentialsProvider.create(
        AwsBasicCredentials.create("test", "test")))
    .build();

TranslateTextResponse response = translate.translateText(req -> req
    .text("Floci makes local AWS testing painless")
    .sourceLanguageCode("en")
    .targetLanguageCode("fr"));

System.out.println("Translated: " + response.translatedText());