public function testJavaScriptTranslation()
 {
     $user = $this->drupalCreateUser(array('translate interface', 'administer languages', 'access administration pages'));
     $this->drupalLogin($user);
     $config = \Drupal::config('locale.settings');
     $langcode = 'xx';
     // The English name for the language. This will be translated.
     $name = $this->randomMachineName(16);
     // Add custom language.
     $edit = array('predefined_langcode' => 'custom', 'langcode' => $langcode, 'label' => $name, 'direction' => LanguageInterface::DIRECTION_LTR);
     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
     $this->container->get('language_manager')->reset();
     // Build the JavaScript translation file.
     // Retrieve the source string of the first string available in the
     // {locales_source} table and translate it.
     $source = db_select('locales_source', 'l')->fields('l', array('source'))->condition('l.source', '%.js%', 'LIKE')->range(0, 1)->execute()->fetchField();
     $search = array('string' => $source, 'langcode' => $langcode, 'translation' => 'all');
     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
     $textarea = current($this->xpath('//textarea'));
     $lid = (string) $textarea[0]['name'];
     $edit = array($lid => $this->randomMachineName());
     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
     // Trigger JavaScript translation parsing and building.
     _locale_rebuild_js($langcode);
     $locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: array();
     $js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
     $this->assertTrue($result = file_exists($js_file), String::format('JavaScript file created: %file', array('%file' => $result ? $js_file : 'not found')));
     // Test JavaScript translation rebuilding.
     file_unmanaged_delete($js_file);
     $this->assertTrue($result = !file_exists($js_file), String::format('JavaScript file deleted: %file', array('%file' => $result ? $js_file : 'found')));
     _locale_rebuild_js($langcode);
     $this->assertTrue($result = file_exists($js_file), String::format('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : 'not found')));
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function languageDelete(\stdClass $language)
 {
     $langcode = $language->langcode;
     // Do not remove English or the default language.
     if (!in_array($langcode, array(language_default('language'), 'en'))) {
         // @see locale_languages_delete_form_submit().
         $languages = language_list();
         if (isset($languages[$langcode])) {
             // Remove translations first.
             db_delete('locales_target')->condition('language', $langcode)->execute();
             cache_clear_all('locale:' . $langcode, 'cache');
             // With no translations, this removes existing JavaScript translations
             // file.
             _locale_rebuild_js($langcode);
             // Remove the language.
             db_delete('languages')->condition('language', $langcode)->execute();
             db_update('node')->fields(array('language' => ''))->condition('language', $langcode)->execute();
             if ($languages[$langcode]->enabled) {
                 variable_set('language_count', variable_get('language_count', 1) - 1);
             }
             module_invoke_all('multilingual_settings_changed');
             drupal_static_reset('language_list');
         }
         // Changing the language settings impacts the interface:
         cache_clear_all('*', 'cache_page', TRUE);
     }
 }