Verified Commit a977482f authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3383888 by poker10: Drupal.t() does not respect locale_custom_strings

(cherry picked from commit 0e6eec07)
parent 0a0cb1b4
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Link;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -1230,6 +1231,14 @@ function _locale_rebuild_js($langcode = NULL) {
    $translations[$data->context][$data->source] = $data->translation;
  }

  // Include custom string overrides.
  $custom_strings = Settings::get('locale_custom_strings_' . $language->getId(), []);
  foreach ($custom_strings as $context => $strings) {
    foreach ($strings as $source => $translation) {
      $translations[$context][$source] = $translation;
    }
  }

  // Construct the JavaScript file, if there are translations.
  $data_hash = NULL;
  $data = $status = '';
+13 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\locale\Functional;

use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\Core\Database\Database;
use Drupal\language\Entity\ConfigurableLanguage;
@@ -310,6 +311,18 @@ public function testJavaScriptTranslation() {
    $this->assertFileDoesNotExist($js_file);
    _locale_rebuild_js($langcode);
    $this->assertFileExists($js_file);

    // Test if JavaScript translation contains a custom string override.
    $string_override = $this->randomMachineName();
    $settings = Settings::getAll();
    $settings['locale_custom_strings_' . $langcode] = ['' => [$string_override => $string_override]];
    // Recreate the settings static.
    new Settings($settings);
    _locale_rebuild_js($langcode);
    $locale_javascripts = \Drupal::state()->get('locale.translation.javascript', []);
    $js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
    $content = file_get_contents($js_file);
    $this->assertStringContainsString('"' . $string_override . '":"' . $string_override . '"', $content);
  }

  /**