strtr(): Passing null to parameter #1 ($string) of type string is deprecated in locale_translation_build_server_pattern()

Created on 30 October 2022, about 2 years ago
Updated 26 August 2024, 3 months ago

Problem/Motivation

When using PHP 8.1 the following warnings are displayed/logged.

Deprecated function: strtr(): Passing null to parameter #1 ($string) of type string is deprecated in locale_translation_build_server_pattern() (line 323 of core/modules/locale/locale.translation.inc).

Steps to reproduce

open status report for a sub site in a multisite project

Proposed resolution

Prevent passing in NULL by converting it to string.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Localeย  โ†’

Last updated 7 days ago

Created by

๐Ÿ‡ฏ๐Ÿ‡ดJordan hanii.gerges

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Hiding the file to avoid confusion.

    Not sure if this is the correct solution though. Shouldn't we figure out what is passing null?

    Whatever the solution it will need a test case to show the issue is solved.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น

    Passing an empty string to strtr() is useless, as no replacement will be done. Type hinting the first parameter of strtr() to string would help more; at least PHP would throw an exception that allows to understand what causes the problem.

    (I agree: We should figure out what exactly is causing this issue; at least we are interested to know if it can reproduced on a plain Drupal installation.)

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States les lim

    In our case, we're using Locale module, but the configuration in locale.settings.yml is not installed. Just looking through our git history, I'm not sure it ever has been.

    This hadn't been causing us any problems that we knew of until the PHP 8.1 upgrade. During locale_requirements(), the function locale_translation_source_build() is called for each project and attempts to build a filepath to a local .po file for the project. To do this, it uses a default filename pattern that is defined in locale.settings.yml. If you don't have any locale.settings configuration, strtr() gets passed NULL instead of a filename pattern.

    Not having any locale.settings is clearly a bug on our side, but it sounds like we're not the only ones this has happened to. I'm happy to resolve our bad config state, but should there be safeguards here to account for this edge case?

  • leymannx Berlin

    Missing locale.settings.yml in the config/sync dir here too. Not sure how it ended up like that. Even going to /admin/config/regional/translate/settings and saving the form to create the locale.settings.yml doesn't fix the problem. Some keys were still missing from it.

    Easiest was to simply run cp web/core/modules/locale/config/install/locale.settings.yml config/sync/locale.settings.yml && drush cim and the error was gone.

  • I have faced exactly the same issue described above, and can confirm it's a config issue where not all keys were in the yml file.
    Just as #10 mentioned, the fix is to use the base config as a starting point - either copy it in your config folder or just single-file import it and adjust as necessary.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain qpro Spain

    Same issue as #10 and #12

    Apart from error

    "Deprecated function: strtr(): Passing null to parameter #1 ($string) of type string is deprecated in locale_translation_build_server_pattern() (line 323 of core/modules/locale/locale.translation.inc)"

    also this one

    "TypeError: Drupal\locale\Form\LocaleSettingsForm::Drupal\locale\Form\{closure}(): Argument #1 ($overwrite_customized) must be of type bool, null given, called in /var/www/html/comg/web/core/lib/Drupal/Core/Form/ConfigTarget.php on line 187 in Drupal\locale\Form\LocaleSettingsForm->Drupal\locale\Form\{closure}() (line 82 of core/modules/locale/src/Form/LocaleSettingsForm.php)."

    Based on #10 & #12, I fixed with:
    drush cim --partial --source=/var/www/html/site/<custom directory>
    with locale.settings.yml inside (without --partial option you could delete the rest of configurations)

    (Drupal 10.2.5 & PHP 8.1.2)

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States uri_frazier Portland, Oregon

    I got the same error when visiting /admin/config/regional/translate/settings:

    "TypeError: Drupal\locale\Form\LocaleSettingsForm::Drupal\locale\Form\{closure}(): Argument #1 ($overwrite_customized) must be of type bool, null given, called in /var/www/html/comg/web/core/lib/Drupal/Core/Form/ConfigTarget.php on line 187 in Drupal\locale\Form\LocaleSettingsForm->Drupal\locale\Form\{closure}() (line 82 of core/modules/locale/src/Form/LocaleSettingsForm.php)."

    Was able to resolve using the fix mentioned in #10 ๐Ÿ› strtr(): Passing null to parameter #1 ($string) of type string is deprecated in locale_translation_build_server_pattern() Needs work
    cp web/core/modules/locale/config/install/locale.settings.yml config/sync/locale.settings.yml && drush cim

    (Drupal 10.2.6 & PHP 8.1.2)

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vinmayiswamy

    Hi, I attempted to reproduce this issue by following the steps below:

    1. Installed Drupal version 10.2.8-dev (cloned from 10.2.x).
    2. PHP version 8.1.29.
    3. Enabled the "locale" module.
    4. Noticed that the locale.settings.yml file is not created automatically after enabling the module.
    5. Was able to visit the /admin/config/regional/translate/settings page and save the settings form without encountering any issues.
    6. As mentioned in previous comments (#10, #12, #13 and #14), I copied the locale.settings.yml file with the command cp core/modules/locale/config/install/locale.settings.yml sites/default/files/sync/locale.settings.yml and executed drush cim.
    7. Revisited the /admin/config/regional/translate/settings page and saved the settings form without any issues.

    I also tested the same steps in Drupal versions 10.3.2 and 11.x with PHP 8.3 and did not replicate the issue.

    If there are any specific steps or additional details I might have missed, please let me know. I would greatly appreciate any guidance.

    Thanks!

  • leymannx Berlin

    Yes, a config file does not get created automagically just by enabling a module.

Production build 0.71.5 2024