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

Created on 30 October 2022, over 1 year ago
Updated 8 April 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 4 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?

  • 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)

Production build 0.69.0 2024