Index: includes/locale.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/locale.inc,v retrieving revision 1.226 diff -u -p -r1.226 locale.inc --- includes/locale.inc 22 Aug 2009 14:34:17 -0000 1.226 +++ includes/locale.inc 28 Aug 2009 16:11:10 -0000 @@ -2420,6 +2420,7 @@ function _locale_rebuild_js($langcode = } // Construct the JavaScript file, if there are translations. + $data_hash = NULL; $data = $status = ''; if (!empty($translations)) { @@ -2438,22 +2439,31 @@ function _locale_rebuild_js($langcode = $dir = 'public://' . variable_get('locale_js_directory', 'languages'); // Delete old file, if we have no translations anymore, or a different file to be saved. - if (!empty($language->javascript) && (!$data || $language->javascript != $data_hash)) { + $changed_hash = $language->javascript != $data_hash; + if (!empty($language->javascript) && (!$data || $changed_hash)) { file_unmanaged_delete($dir . '/' . $language->language . '_' . $language->javascript . '.js'); $language->javascript = ''; $status = 'deleted'; } - // Only create a new file if the content has changed. - if ($data && $language->javascript != $data_hash) { + // Only create a new file if the content has changed or the original file got lost. + $dest = $dir . '/' . $language->language . '_' . $data_hash . '.js'; + if ($data && ($changed_hash || !file_exists($dest))) { // Ensure that the directory exists and is writable, if possible. file_prepare_directory($dir, FILE_CREATE_DIRECTORY); // Save the file. - $dest = $dir . '/' . $language->language . '_' . $data_hash . '.js'; if (file_unmanaged_save_data($data, $dest)) { $language->javascript = $data_hash; - $status = ($status == 'deleted') ? 'updated' : 'created'; + if ($status == 'deleted') { + $status = 'updated'; + } + else if ($changed_hash) { + $status = 'created'; + } + else { + $status = 'rebuilt'; + } } else { $language->javascript = ''; @@ -2462,8 +2472,9 @@ function _locale_rebuild_js($langcode = } // Save the new JavaScript hash (or an empty value if the file - // just got deleted). Act only if some operation was executed. - if ($status) { + // just got deleted). Act only if some operation was executed that + // changed the hash code. + if ($status && $changed_hash) { db_update('languages') ->fields(array( 'javascript' => $language->javascript, @@ -2486,6 +2497,9 @@ function _locale_rebuild_js($langcode = case 'updated': watchdog('locale', 'Updated JavaScript translation file for the language %language.', array('%language' => t($language->name))); return TRUE; + case 'rebuilt': + watchdog('locale', 'JavaScript translation file %file.js was lost.', array('%file' => $language->javascript), WATCHDOG_WARNING); + // Proceed to the 'created' case and log the JavaScript translation file (re)creation. case 'created': watchdog('locale', 'Created JavaScript translation file for the language %language.', array('%language' => t($language->name))); return TRUE; Index: modules/locale/locale.test =================================================================== RCS file: /cvs/drupal/drupal/modules/locale/locale.test,v retrieving revision 1.40 diff -u -p -r1.40 locale.test --- modules/locale/locale.test 28 Aug 2009 14:40:12 -0000 1.40 +++ modules/locale/locale.test 28 Aug 2009 16:11:16 -0000 @@ -300,6 +300,53 @@ class LocaleTranslationFunctionalTest ex $this->assertNoText($name, t('Search now can not find the name.')); } + /* + * Adds a language and checks that the JavaScript translation files are properly created + * and rebuilt on deletion. + */ + function testJavaScriptTranslation() { + $user = $this->drupalCreateUser(array('translate interface', 'administer languages', 'access administration pages')); + $this->drupalLogin($user); + + $langcode = 'xx'; + // The English name for the language. This will be translated. + $name = $this->randomName(16); + // The native name for the language. + $native = $this->randomName(16); + // The domain prefix. + $prefix = $langcode; + + // Add custom language. + $edit = array( + 'langcode' => $langcode, + 'name' => $name, + 'native' => $native, + 'prefix' => $prefix, + 'direction' => '0', + ); + $this->drupalPost('admin/international/language/add', $edit, t('Add custom language')); + + // Build the JavaScript translation file. + $this->drupalGet('admin/international/translate/translate'); + + $string = db_fetch_object(db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE \'%.js%\' AND textgroup = \'default\'')); + $url = 'admin/international/translate/edit/' . $string->lid; + $this->drupalGet($url); + $edit = array('translations['. $langcode .']' => 'string translation'); + $this->drupalPost($url, $edit, t('Save translations')); + locale_inc_callback('_locale_rebuild_js', $langcode); + $file = db_query('SELECT javascript FROM {languages} WHERE language = \'%s\'', $langcode)->fetchObject(); + $js_file = file_create_path(variable_get('locale_js_directory', 'languages')) . '/'. $langcode .'_' . $file->javascript . '.js'; + $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('not found')))); + + // Test JavaScript translation rebuilding. + file_unmanaged_delete($js_file); + $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found')))); + cache_clear_all(); + locale_inc_callback('_locale_rebuild_js', $langcode); + $this->assertTrue($result = file_exists($js_file), t('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : t('not found')))); + } + /** * Tests the validation of the translation input. */