Problem/Motivation

Before being able to use the Language system, it needs to be bootstrapped by calling drupal_language_initialize(), or drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE). This causes many problems as in some cases we don't know whether the language system has been initialized or not.

Proposed resolution

Use Dependency Injection to lazy-initialization the language bootstrap for us, when we need it. This means we'll get rid of DRUPAL_BOOTSTRAP_LANGUAGE, and initialize the bootstrap when we try to retrieve the language objects.

Remaining tasks

User interface changes

None.

API changes

We wouldn't need to call DRUPAL_BOOTSTRAP_LANGUAGE to use the language system, as it will be done for us, whenever we need to use the language system.

Comments

robloach’s picture

robloach’s picture

Status: Active » Needs work
StatusFileSize
new13.36 KB
timmillwood’s picture

It's not just the language system, we should do this with everything!

gábor hojtsy’s picture

I think its fare to do it first for languages since we did DI for that first too.

cosmicdreams’s picture

given the goal of this issue we should include the changes to eliminate the use of $GLOBALS for the language system

cosmicdreams’s picture

Status: Needs work » Needs review

curious to see how this patch tests.

Status: Needs review » Needs work

The last submitted patch, no_language_bootstrap.patch, failed testing.

cosmicdreams’s picture

Status: Needs work » Needs review
StatusFileSize
new12.83 KB

Looks like it needs a reroll

Status: Needs review » Needs work

The last submitted patch, 1550866_8_language_di.patch, failed testing.

cosmicdreams’s picture

Status: Needs work » Needs review
StatusFileSize
new23.77 KB

See if combining this patch with #1539614: Modify language module to use Dependency Injection for language makes this handle the exceptions about language_content better.

Status: Needs review » Needs work

The last submitted patch, 1550866_10_lanague_di.patch, failed testing.

cosmicdreams’s picture

Status: Needs work » Needs review
StatusFileSize
new28.44 KB

Found some $GLOBALS still in use for language dependencies:

cosmicdreams’s picture

StatusFileSize
new29.43 KB

Found two more instances of variables that need to use the DI instead of $GLOBALS

cosmicdreams’s picture

In looking at some of the exceptions this is generating I found one code error that I don't know the solution for:

In Language.php, lines 67 - 81 we have this:

// Check if we are to bootstrap the Language system.
      if (!$container->has('language.bootstrap')) {
        // Bootstrap modules should have been loaded when this function is called, so
        // we don't need to tell module_list() to reset its internal list (and we
        // therefore leave the first parameter at its default value of FALSE). We
        // still pass in TRUE for the second parameter, though; in case this is the
        // first time during the bootstrap that module_list() is called, we want to
        // make sure that its internal cache is primed with the bootstrap modules
        // only.
        foreach (module_list(FALSE, TRUE) as $module) {
          drupal_load('module', $module);
          module_invoke($module, $hook);
        }
        $container->set('language.bootstrap', TRUE);
      }

In that code block $hook is undefined. Should it be a string? what should it be? Should we get the value through reflection?

How should we proceed?

Status: Needs review » Needs work

The last submitted patch, 1550866_14_language_di.patch, failed testing.

cosmicdreams’s picture

Status: Needs work » Needs review
StatusFileSize
new30.53 KB

I found 5 or 6 more GLOBALS to eliminate. Let's see if this patch score better

Status: Needs review » Needs work
Issue tags: -D8MI, -language-base, -Dependency Injection (DI)

The last submitted patch, 1550866_16_language_di.patch, failed testing.

cosmicdreams’s picture

Status: Needs work » Needs review

#16: 1550866_16_language_di.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +D8MI, +language-base, +Dependency Injection (DI)

The last submitted patch, 1550866_16_language_di.patch, failed testing.

jair’s picture

Issue tags: +Needs reroll

Needs reroll

jair’s picture

Issue summary: View changes

Updated issue summary.

balintcsaba’s picture

Assigned: Unassigned » balintcsaba
Issue summary: View changes
sun’s picture

Title: Replace the Language Bootstrap entirely with Dependency Injection » Remove obsolete drupal_language_initialize()
Assigned: balintcsaba » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll +@deprecated
StatusFileSize
new4.74 KB

The bootstrap phase is gone already. The language system is initialized on-demand.

Technically, I don't see why attached patch should not work.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Manually installed d8 in russian, added english and content translation - everything works fine!

+++ b/core/includes/bootstrap.inc
@@ -2009,15 +2009,6 @@ function drupal_installation_attempted() {
- * Initializes all the defined language types and sets the default langcode.
...
-function drupal_language_initialize() {
-  $language_manager = \Drupal::languageManager();
-  $language_manager->init();
-  \Drupal::translation()->setDefaultLangcode($language_manager->getCurrentLanguage()->id);
-}

Seems this $language_manager->init(); obsolete now so filed follow-up #2214057: Remove obsolete LanguageManagerInterface::init() method

plach’s picture

Status: Reviewed & tested by the community » Needs review
+++ /dev/null
@@ -1,15 +0,0 @@
-function language_uninstall() {
-  // Re-initialize the language system so successive calls to t() and other
-  // functions will not expect languages to be present.
-  drupal_language_initialize();
-}
diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageDependencyInjectionTest.php

I am not sure about this hunk: shouldn't we reset the language manager so language is re-negotiated when needed?

andypost’s picture

Suppose on uninstall the container rebuild happens, so none-configurable language manager is used

cosmicdreams’s picture

so back to RTBC?

sun’s picture

I agree with @andypost, ModuleHandler::uninstall() reboots the kernel + rebuilds the container, which means that LanguageManager is replaced with a new instance already.

hook_modules_uninstalled() is only invoked later on, so a reset in there would actually reset it "twice."

plach’s picture

Status: Needs review » Reviewed & tested by the community

Works for me, thanks :)

yesct’s picture

Issue tags: +Needs reroll

Doesn't apply. I'll reroll this now.

yesct’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs reroll
StatusFileSize
new4.81 KB

easy reroll (automatic 3-way merge, no conflicts).

was a change in context lines:
< \Drupal::setContainer($container);
---
> @@ -376,8 +376,6 @@ function install_begin_request(&$install_state) {
> $container->get('string_translation')
> ->addTranslator($container->get('string_translator.file_translation'));

probably rtbc if green.

andypost’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.