ai_translate submodule

Created on 26 June 2024, 17 days ago
Updated 12 July 2024, about 9 hours ago


I think AI powered translations could be a core (for editors) feature.
So it makes sense to have this as a submodule I think (and I can port this from the β†’ module)

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request






Created by

πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @wouters_f
  • πŸ‡±πŸ‡ΉLithuania mindaugasd

    Related issue where this was discussed in the past: ✨ Add support for language translation, and non-English languages Active

  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

    So it works now.

    This is the translation screen if translations is enabled for the Content type.

    This is how it looks after the Translation.


    tested with

    • Openai: Works
    • Mistral: Does not work
  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

    @Marcus: I'm not sure why the Mistral integration throws this error

    PHP message: Uncaught PHP Exception TypeError: "array_map(): Argument #2 ($array) must be of type array, null given" at /var/www/html/vendor/openai-php/client/src/Responses/Chat/CreateResponse.php line 49
  • Status changed to Needs review 17 days ago
  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven
  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven
  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven
  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

    The Mistral API errors were not caught by the underlying library.
    It did not have to do with the translations.

  • πŸ‡¬πŸ‡§United Kingdom yautja_cetanu

    Very cool! I wonder if it should be under an examples or just a submodule

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    I would vote for submodule (and agree with @wouters_f that this should be the core feature for editors)

  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

    I created it as a submodule.
    In my opinion this is a very valid separate module :D.
    I organised my code also like this.

  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    Hey @wouters_f,
    First of, thanks for the submodule, that would help a lot of people!
    Some notes/questions though:

    1. Passing both lang_code and lang_name in ai_translate.translate_content route seems redundant. Can be only lang_code?
    2. Instead of lang_name, I would pass entity_type parameter, allowing to translate not only nodes, but other content entities as well. Logic can be similar to core's Drupal\field_ui\Routing\RouteSubscriber::alterRoutes(), that basically attaches to most content entity types.
    3. Another parameter that I would add is source_lang. For longer texts this may be less relevant, since it's easier to guess from the context, but for shorter ones would be less easier to prompt "as a helpful translator, translate from ... to ..."
    4. Permission "access chatgpt translation" should have more generic name, i.e. "access ai translation" or (following core pattern) "create ai content translations". There are some other mentions of chatgpt in the code, but given AiTranslateController uses default provider, I assume the code is provider-agnostic

    What do you think?
    If you agree with any point, I'd be glad to help (then I'll need permissions on the issue branch)

  • πŸ‡©πŸ‡ͺGermany Marcus_Johansson

    To add to @valthebald great comments - if we add source language we could also add a generic operation type for translation. Since deepl, Grammarly and some other services offers none-LLM, but AI powered translations these would be very simple to add support for these services for any module to use.

    This makes this module more flexible and the AI module more comprehensive.

    So you would do:

    $input = new \Drupal\ai\OperationType\Translate\TranslateInput('How do you do?', 'de', 'en');
    $translation = \Drupal::service('ai.provider')->createInstance('deepl')->translate($input)->getNormalized();
    // $translation would be a string "Wie geht es dir?".

    This would take 1 hour to add and maybe 1-2 hours to add those two plugins.

    Another comment on this in general is that we should perhaps notice which LLM's actually are ok with translating and to which languages. If you do gemma-2b for instance it can't really translate almost anything, while OpenAI is generally even better than Deepl at certrain languages.

  • πŸ‡¬πŸ‡§United Kingdom yautja_cetanu

    I wonder if it's worth putting in a setting for "Context" for specific languages so that the pre-prompt can be edited at all for a specific language. So when you are translating from American English to Swedish, the site owner can choose to make it translate the tone or make it a word for word translation?

    This could be optional settings to make it a little more useful?

    I would eventually like any modules like this in core to work with the future evaluations module.

  • Status changed to Needs work 4 days ago
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    Moving to Needs work because of comments

  • πŸ‡±πŸ‡ΉLithuania mindaugasd
  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

    I agree those would be nice things.
    I just wanted to provide a lift and shift from the chatgpt_plugin to this module to provide them an easy switch with similar behavior.
    Personally I think supporting paragraphs ( ✨ Add support for paragraphs in translations Needs work ) which is a feature they request is more important for the adoption (and even adding layout builder to that too).

    From what I see the 141 sites that use chatgpt_plugin have no issues with the detection of language, there's no bugs regarding this (yet?).
    I would log these things as separate improvements to the translation module which we could work on later (if found of importance).
    Also I personally don't know (yet) how to add these plugins (coding is rusty sorry).

    So I think if we lift and shift the as is, we're already doing a great job and we could pull in the 141 sites using chatgpt_plugin.
    Anything extra is nice, but in my opinion extra.

  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven
  • Status changed to Needs review 4 days ago
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    As I said, I'd be glad to help when it comes to the coding part :)
    Suggested changes are not that big to postpone them

  • Status changed to Needs work 4 days ago
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    I will submit some changes later tonight

  • Pipeline finished with Success
    3 days ago
    Total: 142s
  • Pipeline finished with Success
    2 days ago
    Total: 164s
  • Pipeline finished with Success
    2 days ago
  • Status changed to Needs review 2 days ago
  • πŸ‡§πŸ‡¬Bulgaria valthebald Sofia

    I've submitted my suggestions from #12:

    • Removed node as hard-coded entity type, expecting ContentEntityInterface now. AiTranslateRouteSubscriber::alterRoutes() was already adding "Translate with AI" links to all supported entity types.
    • Changed signature of AiTranslateController::translate() to AiTranslateController::translate($entity_type, $entity_id, $lang_from, $lang_to)
    • Changed permission name to "create ai content translation" and added module's .permissions.yml
    • Enabled translation from original to default language (entity is not necessarily created in site default language, but it would be good to have an option to translate from original language to default one)

    Performed several tests locally, module worked for me for both nodes and taxonomy terms

    Things that are not working yet:

    • The only supported base field is label
    • All fields are treated as text_format (value + format), including plain text

    Both issues do not look to me as deal breakers to include the module to HEAD, but up to you to decide.

  • Pipeline finished with Success
    2 days ago
    Total: 163s
  • Pipeline finished with Success
    2 days ago
    Total: 147s
  • Pipeline finished with Success
    2 days ago
  • Pipeline finished with Success
    2 days ago
    Total: 169s
  • Pipeline finished with Success
    about 15 hours ago
    Total: 139s
  • Pipeline finished with Success
    about 14 hours ago
    Total: 172s
  • Pipeline finished with Success
    about 13 hours ago
    Total: 245s
  • Status changed to RTBC about 12 hours ago
  • πŸ‡©πŸ‡ͺGermany Marcus_Johansson

    Thank @valthebald and wouters_f - the code in the module looks great now and I have tested it on combinations of Swedish, English, German and Japanese and with OpenAI GPT-4o it works great.

    I would say we merge it with dev, unless someone is opposed to it being part of AI and should be a contrib module?

  • πŸ‡§πŸ‡ͺBelgium wouters_f Leuven

    Lets do it!

Production build 0.69.0 2024