Problem/Motivation

JavaScript string translation, and particularly the discovery of the translatable strings, was designed for the D5/D6/D7 asset handling system's lowest common denominator, which was: drupal_add_js(). This function allowed you to add any JS asset to be loaded at any time during the request.

Consequently, the system to discover translatable strings had to be equally dynamic: detect all JS files on the fly, track which ones we've already seen, if we encounter one we haven't seen yet, rebuild the JS translation file.

In Drupal 8, that has changed: all assets must be defined in asset libraries, and to have an asset be present in the HTML/loaded by the browser, you must "attach asset libraries". (Already supported and recommended in Drupal 7, but not enforced; as of Drupal 8, this is the only supported mechanism.)

Proposed resolution

In Drupal 8, we can generate the translation files (one per language) all at once, without having to do that iterative building! That will mean, after a cache clear or a deployment:

fewer I/O (no need to first build a translation file for the strings on page A, then expand it for the additional strings discovered in page B, etc.)
no need to update the entry in the State system in sync with 1, hence avoiding race conditions/stampedes

_locale_rebuild_js() and friends can go away.

Issue fork drupal-2607376

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

catch created an issue. See original summary.

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.

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.

catch’s picture

Issue summary: View changes

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

fago’s picture

fago’s picture

We ran into another, similar issue - see #3034990: Race condition when removing old JS translation files.

maximpodorov’s picture

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

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.

gapple’s picture

I think because hook_js_alter can modify the files added to the page when it is rendered, it may still be necessary to check for additional files not defined in library definitions and update the translation file if necessary. Having an initial build that parses all files listed in library definitions would still reduce rebuilds and race conditions.

gapple’s picture

Title: Remove on-demand JavaScript translation parsing and do everything on rebuild » 2607376-translate-javascript-rebuild
Status: Active » Needs review

A couple things about this MR feel a little hacky (e.g. relying on AssetResolver::getJsAssets() to call hook_js_alter which parses for source strings), but it's a functional start without requiring a lot of refactoring.
There's plenty of room for optimization - for example AssetResolver does dependency resolution on the list of libraries, which isn't necessary since the list is know to contain every defined library.

A couple warnings pop up during rebuild because of dependency resolution when certain modules are not enabled:

  • Editor module defines quickedit.inPlaceEditor.formattedText with a dependency on quickedit/quickedit. If Quickedit module is not enabled though, LibraryDiscoveryParser::buildByExtension() will throw a warning (it also assumes it's a theme since it's not in the module list...)
  • The placeholder translation.js file in core/ckeditor5.translations is intended to be removed by ckeditor5_js_alter(), but if CKEditor 5 module is not enabled, it gets passed to _locale_parse_js_file and throws a file not found warning
gapple’s picture

Title: 2607376-translate-javascript-rebuild » Remove on-demand JavaScript translation parsing and do everything on rebuild

oops. fix title.

catch’s picture

The editor/quickedit bug will be fixed by #3267258: Remove Quick Edit support from editor.module.

The placeholder translation.js file in core/ckeditor5.translations is intended to be removed by ckeditor5_js_alter(), but if CKEditor 5 module is not enabled, it gets passed to _locale_parse_js_file and throws a file not found warning

This seems like a ckeditor bug, we should move that file into ckeditor5 itself I would think.

A couple things about this MR feel a little hacky (e.g. relying on AssetResolver::getJsAssets() to call hook_js_alter which parses for source strings), but it's a functional start without requiring a lot of refactoring.

Yeah I think we'll want to factor out some logic into something that both the locale_rebuild() and locale_js_alter() can use. I was hoping we could remove locale_js_alter() altogether in this issue, but the ability to add arbitrary js files from there probably means we can't and will have to keep both mechanisms around.

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.

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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

At this time we will need an MR for D10

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.

joelstein’s picture

Rerolling the patch.

_utsavsharma’s picture

Status: Needs work » Needs review
FileSize
3.47 KB
2.71 KB

Fixed failures in #26.

Status: Needs review » Needs work

The last submitted patch, 27: 2607376-27.patch, failed testing. View results

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

alexpott changed the visibility of the branch 2607376-translate-javascript-rebuild to hidden.

alexpott’s picture

Hiding all the patches and other MR to leave the 11.x MR easy to find as that is where we need to start.