Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
We currently use t() in some exceptions, but we don't translate exceptions. In the installer there are some
Examples:
./includes/common.inc: throw new Exception(t('Archivers can only operate on local files: %file not supported', array('%file' => $file)));
./includes/install.core.inc: throw new InstallerException(t('Missing profile parameter.'));
./includes/install.core.inc: throw new InstallerException(t('Sorry, you must select a language to continue the installation.'));
./includes/install.inc: throw new Exception(t('Failed to modify %settings. Verify the file permissions.', array('%settings' => $settings_file)));
./includes/install.inc: throw new Exception(t('Failed to open %settings. Verify the file permissions.', array('%settings' => $settings_file)));
./includes/install.inc: throw new Exception(t('The directory %directory could not be created or could not be made writable. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see the <a href="@handbook_url">online handbook</a>.', array(
./includes/update.inc: * throw new UpdateException(t('Description of what went wrong'));
./lib/Drupal/Core/Archiver/Zip.php: throw new ArchiverException(t('Cannot open %file_path', array('%file_path' => $file_path)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot rename @table to @table_new: table @table doesn't exist.", array('@table' => $table, '@table_new' => $new_name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot rename @table to @table_new: table @table_new already exists.", array('@table' => $table, '@table_new' => $new_name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add field @table.@field: table doesn't exist.", array('@field' => $field, '@table' => $table)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add field @table.@field: field already exists.", array('@field' => $field, '@table' => $table)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add primary key to table @table: table doesn't exist.", array('@table' => $table)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add primary key to table @table: primary key already exists.", array('@table' => $table)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add unique key @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add unique key @name to table @table: unique key already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaIndexNotInSpecException(t("The index @name doesn't exist in the @table table specification.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot change the definition of field @table.@name: field doesn't exist.", array('@table' => $table, '@name' => $field)));
./lib/Drupal/Core/Database/Driver/mysql/Schema.php: throw new SchemaObjectExistsException(t("Cannot rename field @table.@name to @name_new: target field already exists.", array('@table' => $table, '@name' => $field, '@name_new' => $field_new)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot rename @table to @table_new: table @table doesn't exist.", array('@table' => $table, '@table_new' => $new_name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot rename @table to @table_new: table @table_new already exists.", array('@table' => $table, '@table_new' => $new_name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add field @table.@field: table doesn't exist.", array('@field' => $field, '@table' => $table)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add field @table.@field: field already exists.", array('@field' => $field, '@table' => $table)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add primary key to table @table: table doesn't exist.", array('@table' => $table)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add primary key to table @table: primary key already exists.", array('@table' => $table)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add unique key @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add unique key @name to table @table: unique key already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaIndexNotInSpecException(t("The index @name doesn't exist in the @table table specification.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot change the definition of field @table.@name: field doesn't exist.", array('@table' => $table, '@name' => $field)));
./lib/Drupal/Core/Database/Driver/pgsql/Schema.php: throw new SchemaObjectExistsException(t("Cannot rename field @table.@name to @name_new: target field already exists.", array('@table' => $table, '@name' => $field, '@name_new' => $field_new)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot rename @table to @table_new: table @table doesn't exist.", array('@table' => $table, '@table_new' => $new_name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot rename @table to @table_new: table @table_new already exists.", array('@table' => $table, '@table_new' => $new_name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add field @table.@field: table doesn't exist.", array('@field' => $field, '@table' => $table)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot add field @table.@field: field already exists.", array('@field' => $field, '@table' => $table)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot change the definition of field @table.@name: field doesn't exist.", array('@table' => $table, '@name' => $field)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot rename field @table.@name to @name_new: target field already exists.", array('@table' => $table, '@name' => $field, '@name_new' => $field_new)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaIndexNotInSpecException(t("The index @name doesn't exist in the @table table specification.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add unique key @name to table @table: table doesn't exist.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot add unique key @name to table @table: unique key already exists.", array('@table' => $table, '@name' => $name)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot add primary key to table @table: table doesn't exist.", array('@table' => $table)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectExistsException(t("Cannot add primary key to table @table: primary key already exists.", array('@table' => $table)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
./lib/Drupal/Core/Database/Driver/sqlite/Schema.php: throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
./lib/Drupal/Core/Database/Install/Tasks.php: throw new TaskException(t("Failed to run all tasks against the database server. The task %task wasn't found.", array('%task' => $task['function'])));
./lib/Drupal/Core/Database/Query/Merge.php: throw new InvalidMergeQueryException(t('Invalid merge query: no conditions'));
./lib/Drupal/Core/Database/Schema.php: throw new SchemaObjectExistsException(t('Table @name already exists.', array('@name' => $name)));
./lib/Drupal/Core/Template/TwigExtension.php: throw new \Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg))));
./lib/Drupal/Core/Template/TwigExtension.php: throw new \Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg))));
./lib/Drupal/Core/Theme/ThemeManager.php: throw new \Exception(t('_theme() may not be called until all modules are loaded.'));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterException(t('Unable to determine the type of the source directory.'));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterException(t('Cannot determine the type of project.'));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterException(t('Unable to parse info file: %info_file.', array('%info_file' => $info_file)));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
./lib/Drupal/Core/Updater/Updater.php: throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
./modules/contextual/src/ContextualController.php: throw new BadRequestHttpException(t('No contextual ids specified.'));
./modules/dblog/src/Plugin/rest/resource/DBLogResource.php: throw new NotFoundHttpException(t('Log entry with ID @id was not found', array('@id' => $id)));
./modules/dblog/src/Plugin/rest/resource/DBLogResource.php: throw new HttpException(t('No log entry ID was provided'));
./modules/field/field.api.php: throw new \Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field_storage->getName())));
./modules/field/field.purge.inc: throw new FieldException(t('Attempt to purge a field storage @field_name that still has fields.', array('@field_name' => $field_storage->getName())));
./modules/node/node.module: throw new Exception(t('Query tagged for node access but there is no node table, specify the base_table using meta data.'));
./modules/options/options.module: throw new FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field_storage->getName())));
./modules/rdf/rdf.module: throw new Exception(t('Tried to map @prefix to @namespace, but @prefix is already mapped to @orig_namespace.', array('@prefix' => $prefix, '@namespace' => $namespace, '@orig_namespace' => $namespaces[$prefix])));
./modules/system/tests/modules/common_test/common_test.module: throw new Exception(t('Uncaught exception'));
./modules/update/update.manager.inc: throw new Exception(t('Cannot extract %file, not a valid archive.', array ('%file' => $file)));
Proposed resolution
Don't use t() in exceptions.
Also, per @dawehner:
There is a limited subset of exceptions which get t() in order to translate error message in the installer. Ideally we would have classed exceptions for all of them, so we could translate them at a more outer level.
Remaining tasks
Review patch
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#17 | do_not_use_t_in_exceptions-2538814-17.patch | 44.69 KB | afi13 |
Comments
Comment #1
tim.plunkettYou should use neither:
#2514044: Do not use SafeMarkup::format in exceptions
Comment #2
pguillard CreditAttribution: pguillard commentedI take it
Comment #3
dawehner@pguillard
There is a limited subset of exceptions which get t() in order to translate error message in the installer. Ideally we would have classed exceptions for all of them, so we could translate them at a more outer level.
Comment #4
pguillard CreditAttribution: pguillard commentedOoops Thank you @dawehner.
Just in case it can be usefull, I uploaded the patch, I had not finished yet, but stopped there.
Comment #5
afi13 CreditAttribution: afi13 commentedI want to help with this issue. How can i do it?
Comment #6
afi13 CreditAttribution: afi13 commentedComment #7
dawehnerIt is always great to see early failures.
Comment #9
afi13 CreditAttribution: afi13 commentedComment #10
longwaveComment #12
stefan.r CreditAttribution: stefan.r commentedreroll
Comment #13
stefan.r CreditAttribution: stefan.r commentedComment #14
afi13 CreditAttribution: afi13 commentedComment #15
dawehnerSo these needs discussion, do we want to be able to translate those basic error messages during the installer? You could implement that by having custom exception classes for those different errors and listen to the
Do you mind opening a follow for that? This should be an assertion IMHO?
Some of them seems to be user oriented, aren't they?
Comment #16
afi13 CreditAttribution: afi13 commentedAnd that
So, we can use InstallerException class for all install exception and translate it messages?
Comment #17
afi13 CreditAttribution: afi13 commentedSomething like this?
Comment #19
andypostComment #26
xjmThis has been solved in #2055851: Remove translation of exception messages. Thanks!