TranslatorService.java

/*
 * Copyright 2024 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.server.service;

import java.util.Map;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.genesys.spring.validation.javax.SupportedLanguage;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

/**
 * Translate things!
 */
public interface TranslatorService {

  /**
   * Translate with Blabel
   * @param translate Source data
   * @return Translated data
   * @throws TranslatorException
   */
  TranslationStructuredResponse translate(@Valid TranslationStructuredRequest translate) throws TranslatorException;

  @NoArgsConstructor
  public static class TranslatorException extends Exception {
    public TranslatorException(String message) {
      super(message);
    }
    public TranslatorException(String message, Throwable cause) {
      super(message, cause);
    }
  }

  public static enum TextFormat {
    html, text, markdown;
  }

  /**
   * A request for translation of text in source language to a target language.
   */
  @Data
  @SuperBuilder
  public static class TranslationStructuredRequest {

    /** Source language */
    @NotBlank
    @Size(max = 2)
    private String sourceLang;

    /** Context text */
    private String context;

    /** Target language */
    @NotBlank
    @Size(max = 2)
    @SupportedLanguage
    private String targetLang;


    /**
     * The text to translate. The maximum length supported is 100,000 characters.
     * 
     * For DeepL the total request body size must not exceed 128 KiB (128 x 1024bytes)
     */
    private Map<String, FormattedText> texts;
  }

  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public static class FormattedText {
    /** The format of the source text */
    @NotNull
    private TextFormat format = TextFormat.text;
    /** The text to translate. The maximum length supported is 100,000 characters. */
    @NotBlank
    @Size(max = 100000) // For DeepL the total request body size must not exceed 128 KiB (128 ยท 1024 bytes).
    private String text;
  }

  /**
   * Translation result
   */
  @Data
  public static class TranslationStructuredResponse {
    // /** The format of the source text */
    // private TextFormat format;
    // /** Source language */
    // private String sourceLang;
    // /** Original source text */
    // private String sourceText;
    /** Target language */
    private String targetLang;
    /** The source text translated into target language */
    private Map<String, String> texts;
  }

}