Problem/Motivation

Some blocks in the block layout library are grouped under the label "System", some under the translation of "System"

Steps to reproduce

  1. Install in another language than english
  2. Login as admin
  3. Go to the block layout page admin/structure/block

Alternative:

  1. Login as admin
  2. Enable multilanguage modules
  3. Add another language
  4. Interface-translate "System"
  5. Go to the block layout page admin/structure/block

Multiple instances of "System" are not translated while some are. See the screenshots below, where "System" was translated to "TEST" in German.

Noticed in #2019511: Explain why the language switcher would not show under some configurations

Proposed resolution

  • Add translatable category to all core block plugins
  • Test coverage to ensure all core blocks have a category
  • Translate the fallback category (module name) for other blocks

Remaining tasks

User interface changes

Before


After


All system blocks are now in translatable categories.

API changes

No.

Issue fork drupal-2500607

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

yesct’s picture

Issue summary: View changes
Status: Needs work » Active
StatusFileSize
new614.41 KB

Maybe there is a t() missing from some?

yesct’s picture

Those two that go into Systema, have explicit category set in their annotations with @Translation

For example, in LanguageBlock.php

/**
 * Provides a 'Language switcher' block.
 *
 * @Block(
 *   id = "language_block",
 *   admin_label = @Translation("Language switcher"),
 *   category = @Translation("System"),
 *   deriver = "Drupal\language\Plugin\Derivative\LanguageBlock"
 * )
 */
class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface {

compare to SystemMailBlock.php

/**
 * Provides a 'Main page content' block.
 *
 * @Block(
 *   id = "system_main_block",
 *   admin_label = @Translation("Main page content")
 * )
 */
class SystemMainBlock extends BlockBase implements MainContentBlockPluginInterface {

but... how does it know that is system? is system the default category?

\Drupal\Core\Block\Annotation\Block

says

  /**
   * The category in the admin UI where the block will be listed.
   *
   * @var \Drupal\Core\Annotation\Translation
   *
   * @ingroup plugin_translatable
   */
  public $category = '';

which looks like it is not setting a default category...

----

Here is one not in system, just for additional comparison, SearchBlock.php:

/**
 * Provides a 'Search form' block.
 *
 * @Block(
 *   id = "search_form_block",
 *   admin_label = @Translation("Search form"),
 *   category = @Translation("Forms")
 * )
 */
class SearchBlock extends BlockBase {
axooh’s picture

@dschenk and me are working on this, while sprinting at DrupalCon in Barcelona.

dschenk’s picture

Issue summary: View changes
StatusFileSize
new158.73 KB
new101.5 KB
axooh’s picture

Component: language.module » system.module
Status: Active » Needs review
StatusFileSize
new2.95 KB

In this patch we added the category explicitly to the corresponding blocks in the system module, to make them translatable.

dschenk’s picture

Issue summary: View changes
StatusFileSize
new331.22 KB

I helped in the creation of the patch.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dschenk’s picture

Version: 8.1.x-dev » 8.2.x-dev
maxocub’s picture

Status: Needs review » Reviewed & tested by the community

I was able to reproduce the issue and confirm that the patch in #5 works.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Nice find! I noticed this once before but did not track down why it was happening. The fix seems correct.

I think we could provide test coverage to this to make sure future system blocks also get the correct category. The test might load the translated admin listing, assert that expected blocks are displayed, and assert that the untranslated word "system" does not appear.

Alternately (or additionally), is declaring a block without a category even valid? Should we warn developers of this somehow?

maxocub’s picture

I think that declaring a block without a category is valid, at least that's what I understand from the default empty value in the annotation. I haven't found out why and how (and I would like know if someone can illuminate me), but when we don't declare a category, it uses the module's name.

What's happening here is that the 5 blocks from the system module don't declared a category (so it uses 'System' as a fall back), and that two blocks from other modules declared there category as 'System' and made it translatable. I think the module's name fall back is OK.

On a different note, the admin label too is optional, and it doesn't have a fall back, so when you don't declare one it stays blank. Shouldn't we use the block's ID in that case for the admin label?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

gábor hojtsy’s picture

Sounds like a backwards incompatible change to require categories. But fixing this problem by adding categories sounds fine. So maybe we should test that all core blocks provide a category as a best practice, but not as a requirement.

gábor hojtsy’s picture

Issue tags: +sprint

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

maxocub’s picture

Title: some blocks in system are in system, some in translated-system label when install in another language » Some blocks don't have a category, and thus we cannot translate it
Component: system.module » block.module
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new962 bytes
new5.53 KB
new2.59 KB

Here's a test to check that every core block have a category.

The last submitted patch, 16: 2500607-test-only.patch, failed testing.

maxocub’s picture

Title: Some blocks don't have a category, and thus we cannot translate it » Some block categories are not translatable
StatusFileSize
new7.72 KB
new2.19 KB

Hmm, it seems like my test does not catch all blocks without a category.
I guess it's because the namespaces array is only filled with enabled modules.
Is there a way to get ALL namespaces?

maxocub’s picture

StatusFileSize
new8.32 KB
new1.44 KB

Here's a way to find all block plugins: loading every (non-test) modules to fill the namespaces array.

maxocub’s picture

StatusFileSize
new8.2 KB
new1.45 KB

Just found a better way to get a list of all modules, thanks to @heddn!

maxocub’s picture

Issue tags: +Baltimore2017
john cook’s picture

StatusFileSize
new185.34 KB
new186.06 KB
new78.57 KB
new8.94 KB
new755 bytes

I started out by testing the patch from #20 using the Korean translation.

Before:

After:

So I would say that the patch works as designed.

Then I followed the code a bit down the call stack and made a new patch. The only change is in core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php:processDefinitionCategory() where I've passed the retrieved module name into t(). This would allow all categories that haven't been set to be translatable.

Status: Needs review » Needs work

The last submitted patch, 22: 2500607-22.patch, failed testing.

john cook’s picture

StatusFileSize
new9.46 KB
new1.56 KB

Changed the test to expect a TranslatableMarkup object instead of a string as default category.

john cook’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 24: 2500607-24.patch, failed testing.

john cook’s picture

Status: Needs work » Needs review
StatusFileSize
new9.97 KB
new1.08 KB

The category was accidentally removed from PageTitleBlock.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ethomas08’s picture

Uploading re-rolled patch. It fixes the bug (see attached screenshot), but I haven't ran the tests to see if anything breaks. Lots of changes between 8.4.x and 8.7.x!

ethomas08’s picture

Status: Needs review » Reviewed & tested by the community

Ran tests -- the 2 that had been altered passed. All the reroll changes were the same as in the 8.4.x patch so marking this RTBC. Great job on this, looks great!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Plugin/CategorizingPluginManagerTrait.php
@@ -31,7 +31,7 @@ protected function processDefinitionCategory(&$definition) {
-      $definition['category'] = $this->getProviderName($definition['provider']);
+      $definition['category'] = $this->t($this->getProviderName($definition['provider'])); ¶

I'm not sure this is correct. The @translation should pass a TranslationMarkup object into the definition.

Also we should have the test that ensures all the blocks have categories that was in #27.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue tags: +Bug Smash Initiative

This is still relevant and the patch applies to 9.5.x.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan made their first commit to this issue’s fork.

mstrelan’s picture

Status: Needs work » Needs review

Spun up an MR and started with the (slightly refactored) test from #27. Can see the test-only results at https://git.drupalcode.org/issue/drupal-2500607/-/jobs/5680572

1) Drupal\Tests\block\Kernel\BlockDefinitionsTest::testBlockCategory
Block plugins missing categories: views_exposed_filter_block, views_block, system_powered_by_block, system_messages_block, system_main_block, system_breadcrumb_block, system_branding_block, system_clear_cache_block, navigation_user, navigation_shortcuts, navigation_link, field_block, extra_field_block, help_block, announce_block, local_tasks_block, local_actions_block, page_title_block
Failed asserting that an array is empty.

I then applied all the same categories from the existing patches, but of course we have attributes instead of annotations now, so had to be done manually. There were some new plugins revealed by the failing test so I added categories for those as well.

Then I added in the changes to CategorizingPluginManagerTrait and CategorizingPluginManagerTraitTest, but didn't quite follow @alexpott's comment in #33. Maybe it wasn't clear that this is a fallback, so we get the "provider" and try to translate that.

Guessing the IS needs an update, maybe some new screenshots.

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

mstrelan’s picture

Status: Needs work » Needs review
Issue tags: +no-needs-review-bot

Ok thanks bot. Hiding patches, even though I think they are useful for confirming that the MR is on the right track.

xjm’s picture

Amending attribution.

smustgrave’s picture

Status: Needs review » Needs work

Can we update the summary with a proposed solution.

Also this brings up a question about attributes, if we are adding a new key how do we ensure contrib/custom modules update theirs too? WOuld have to be done in the attribute itself?

mstrelan’s picture

Issue summary: View changes
Status: Needs work » Needs review

Can we update the summary with a proposed solution.

Done

Also this brings up a question about attributes, if we are adding a new key how do we ensure contrib/custom modules update theirs too? WOuld have to be done in the attribute itself?

The key already exists in the attribute, but it's optional. We're ensuring all core blocks have a category set. Contrib/custom will fallback to the module name, which we're now translating. Although I was under the impression we're only meant to translate string literals, so not sure if that part is suitable. We could always deprecate not passing a category, but that could be pretty annoying.

smustgrave’s picture

Status: Needs review » Needs work

Did a search for #[Block and there appear to still be other instances

Mainly test blocks, if those aren't needed can we least update the block.api

If you are another contributor eager to jump in, please allow the previous poster(s) at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

mstrelan’s picture

Status: Needs work » Needs review

I don't think we need to do the test blocks. Category is still optional, we're just ensuring core blocks set the category. I've updated the example in block.api.php

I'm still unsure about the fallback translation in CategorizingPluginManagerTrait. If we have that, why bother enforcing all the core blocks have a category?

smustgrave’s picture

idk if a good search but I looked for $definition['provider'] and didn't see any other spots that are doing translations.

smustgrave’s picture

what you say that's a blocker for this moving forward?

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

MR still applies and don't think my comment should hold things

nod_ made their first commit to this issue’s fork.

nod_’s picture

Status: Reviewed & tested by the community » Needs work

I have a problem with this line, There is not a finite amount of values for $definition['provider'] passing that through t() is not a good idea.

      $definition['category'] = $this->t($this->getProviderName($definition['provider']));

The rest is fine.

mstrelan’s picture

Issue summary: View changes
Status: Needs work » Needs review

Updated the proposed resolution to delete "Translate the fallback category (module name) for other blocks". Updated the MR as per #58.

dcam’s picture

Status: Needs review » Needs work

Since the change to CategorizingPluginManagerTrait was reverted now CategorizingPluginManagerTraitTest is failing.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.