InvalidArgumentException: Invalid translation language (en) specified

Created on 13 September 2023, almost 2 years ago

Problem/Motivation

This error occurred when we enabled language field in content types and switching languages in node edit by using language field (node/2/edit):

InvalidArgumentException: Invalid translation language (en) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 870 of /app/docroot/core/lib/Drupal/Core/Entity/ContentEntityBase.php).

Steps to reproduce

  1. Install using standard profile in English
  2. Log in as a admin user
  3. Install Content Translation module
  4. Install content moderation module
  5. Add English, German language
  6. Enable ‘Show language selector on create and edit pages’ and ‘Enable translation’ options in article content type ‘Language Settings’
  7. Enable ‘Language’ field in article content type from ‘Manage form display’ tab
  8. Create new field with entity reference and select any of the paragraph
  9. Create a new article node with selection of ‘German’ in Language field
  10. Edit recent article node and try to select ‘English’ language in ‘Language’ field and try to save that node
  11. You will get a fatal error

Proposed resolution

Article node should be save with English language without any fatal error.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

10.1

Component
Language module 

Last updated 9 days ago

  • Maintained by
  • 🇩🇪Germany @sun
Created by

🇮🇳India ramesh.tellamekala

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

Merge Requests

Comments & Activities

  • Issue created by @ramesh.tellamekala
  • Assigned to shubham_jain
  • Issue was unassigned.
  • 🇮🇳India shubham_jain

    Hi @ramesh.tellamekala, I followed your steps in Drupal 9.5.10 using php 8.1 but was unable to reproduce the error.
    See attched screenshot for refrence.

  • 🇮🇳India ramesh.tellamekala

    This error is thrown under the following circumstances in a multilingual site:

    1. Site have content translation, moderation enabled.
    2. User creates a node and sets the language to German.
    3. User updates the node and set the language to English.
  • 🇮🇳India shubham_jain

    Hi @ramesh.tellamekala, the screenshots that I shared are of the same node. let me explain you in detail what did I do.

    Firstly as you said enabled Content Translation and Content moderation module.
    Added German language because English was default.
    Enabled ‘Show language selector on create and edit pages’ and ‘Enable translation’ options in article content type ‘Language Settings’.
    The ‘Language’ field was visible in ‘Manage form display’ tab of article content type.
    Created new field with entity reference version and selected accordian paragraph type.
    Created a new article node with selection of ‘German’ in Language field
    Edited the same article node and try to select ‘English’ language in ‘Language’ field and saved that node.

    No Error occoured.

    Please correct me if I forgot something.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update almost 2 years ago
    29,462 pass, 1 fail
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update almost 2 years ago
    29,462 pass, 1 fail
  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    Have not reviewed.

    As a bug will need a test case showing the issue. Updated remaining tasks

    Also proposed solution sounds like more "expected results" but not mentioning how the solution will be achieved.

  • 🇨🇳China eason xu

    1: Upload an image in media.
    2: Reference this image in a paragraph.
    3: Reference this paragraph to a node.
    4: Access the usage list of image.
    Result: Report an error. InvalidArgumentException: Invalid translation language (en) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 874 of /mnt/www/html/terumo01live/docroot/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
    This patch helped me solve this error.

  • 🇪🇸Spain rcodina Barcelona

    Patch on #6 fixes the error for me on Drupal 10.1.6.

  • First commit to issue fork.
  • 🇻🇳Vietnam phannphong Ho Chi Minh City

    I faced exactly the issue here and the patch #6 fixes the error on Drupal 10.1.6.

  • 🇨🇭Switzerland handkerchief

    Same issue, same solution, patch #6 works for me for Drupal 10.2.2.

  • 🇮🇳India pooja saraah Chennai

    Added Condition for Active Language Check:
    The elseif condition checks if there is no translation object for the switched language used by the language field in the entity ($this->activeLangcode) and if the requested language ($langcode) is different.
    If this condition is met, it initializes the translation using the default active translation.
    Attached modified patch.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    Composer error. Unable to continue.
  • 🇬🇷Greece bserem

    None of the provided patches work for me. In my case I am trying to add translations to blocks using Drupal 10.2

  • 🇷🇸Serbia levmyshkin Novi Sad, Serbia

    I also had the problem with blocks translation, but this helped me #2137815:
    https://www.drupal.org/project/drupal/issues/2137815 🐛 InvalidArgumentException: Invalid translation language specified. Needs work

  • I created a patch for Drupal 10.2.3

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.6
    last update about 1 year ago
    Custom Commands Failed
  • #6 works for us. Thank you very much!

  • #6 worked for us. Thank you very much!

  • 🇺🇸United States apmsooner

    #6 makes my patch for image/file translations work for the AI translate module as mentioned here: https://www.drupal.org/project/ai/issues/3531717#comment-16168671 Add text extractor plugins for image and link field types Active

  • 🇺🇸United States apmsooner

    Re-roll #6 for 10.5.x

  • First commit to issue fork.
  • 🇺🇸United States nicxvan

    nicxvan changed the visibility of the branch 11.x to hidden.

  • 🇺🇸United States nicxvan

    nicxvan changed the visibility of the branch 3386915-invalidargumentexception-invalid-translation to hidden.

  • 🇺🇸United States nicxvan

    nicxvan changed the visibility of the branch 3386915-Fix-saving-languages to hidden.

  • 🇺🇸United States nicxvan

    nicxvan changed the visibility of the branch 3386915-Fix-saving-languages to active.

  • 🇺🇸United States nicxvan

    I got this when turning on auto saving translations in tmgmt.

    Drupal core tests do not run with patches anymore, if you see an older issue please create an MR from it so that tests can run and collaboration can happen more easily.

    This still needs tests and a full review, but testing this with tmgmt it works.

  • Merge request !12596Add fallback translation fix → (Open) created by nicxvan
  • Pipeline finished with Failed
    13 days ago
    Total: 549s
    #537533
  • First commit to issue fork.
  • Pipeline finished with Failed
    13 days ago
    Total: 238s
    #537866
  • Pipeline finished with Failed
    13 days ago
    Total: 505s
    #537896
  • 🇮🇳India immaculatexavier

    Hi all,

    I've added a functional test to cover the scenario mentioned in this issue. The test checks that when a user creates an article node in German and then switches the language to English using the language selector on the edit form, it saves without any errors.

    This covers the exact steps described in the issue and ensures the language switch functionality works as expected. Happy to make adjustments if needed.

    Thanks!

  • 🇺🇸United States smustgrave

    Do we not have an existing test we can expand vs adding a brand new one

  • Pipeline finished with Failed
    12 days ago
    Total: 553s
    #538304
  • Pipeline finished with Failed
    12 days ago
    Total: 405s
    #538313
  • 🇮🇳India immaculatexavier

    Hi,

    Now added the test case testSwitchLangcodeWithoutExistingTranslation() to the existing ContentTranslationLanguageChangeTest class. This expands the current test coverage instead of creating a new test class, aligning with the feedback to reuse existing tests where possible. The test validates the fallback logic when switching the langcode on a node that doesn't yet have a translation.

    Kindly let me know if any further adjustments are needed.

  • 🇺🇸United States apmsooner

    The patch applies clean for 10.5.1. I followed the steps and it works without error. Additionally, combined with patch for AI translate module ( https://www.drupal.org/project/ai/issues/3531717 Add text extractor plugins for image and link field types Active ) and following the steps outlined there, all translatable properties on image/file fields get translated as the the batch process is allowed to finish due to this core fix.

  • 🇺🇸United States nicxvan

    This still needs work.

    There are other tests failing.

    We also have to confirm this is the appropriate solution.

  • 🇺🇸United States apmsooner

    FYI, The errors are on a different test and seem to be related to this line from what i can tell: https://git.drupalcode.org/issue/drupal-3386915/-/blob/3386915-Fix-savin...

  • 🇺🇸United States nicxvan

    Yep exactly, which means that we may be breaking something else.

    Let me preface this by saying I'm about to drop a bunch of questions I ask myself when reviewing issues.
    I recognize it is a lot, but adding these to your own process can help prevent things from being knocked back and increase the chances of issues being fixed quicker.

    Here is a list of questions that core committers ask before committing and they are good to keep in mind when reviewing.
    https://www.drupal.org/about/core/policies/maintainers/commit-guide#prec...

    If you read further up:

    You don't have to know everything! :)

    So don't worry if you can't answer every last one yourself.

    In addition to that before marking something RTBC here are some things I check myself, not in a particular order.

    • Have I reviewed the code (sometimes including surrounding code) enough to understand what is changing
    • Are all tests passing. (This will prevent marking when there are known issues like here)
    • Is there a test written for this issue specifically (not all issues need tests, but almost all do)
    • Does the test only job fail (there is a special test that runs just the test and not the fix to confirm it will help prevent regressions)
    • Are there more edge cases that are missing a fix
    • Are there follow up issues mentioned in comments or code that should be created
    • Does this need comments explaining what is happening
    • Have other user's comments on the MR or in the issue been adequately addressed and answered
    • Does this need a Change Record
Production build 0.71.5 2024