Index: includes/locale.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/locale.inc,v retrieving revision 1.205 diff -u -r1.205 locale.inc --- includes/locale.inc 22 Feb 2009 17:55:29 -0000 1.205 +++ includes/locale.inc 1 Mar 2009 22:44:13 -0000 @@ -2264,6 +2264,7 @@ } // Construct the JavaScript file, if there are translations. + $data_hash = NULL; $data = $status = ''; if (!empty($translations)) { @@ -2281,23 +2282,26 @@ // There is (on purpose) no front end to edit that variable. $dir = file_create_path(variable_get('locale_js_directory', 'languages')); + // Has the hash code changed? + $changed_hash = $language->javascript != $data_hash; + // 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)) { + if (!empty($language->javascript) && (!$data || $changed_hash)) { file_unmanaged_delete(file_create_path($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_check_directory($dir, TRUE); // 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'; + $status = ($status == 'deleted') ? 'updated' : ($changed_hash ? 'created' : 'rebuilt'); } else { $language->javascript = ''; @@ -2306,8 +2310,9 @@ } // 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_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $language->javascript, $language->language); // Update the default language variable if the default language has been altered. @@ -2325,6 +2330,9 @@ 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); + // let slip through 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.19 diff -u -r1.19 locale.test --- modules/locale/locale.test 24 Feb 2009 16:46:52 -0000 1.19 +++ modules/locale/locale.test 1 Mar 2009 22:44:14 -0000 @@ -522,6 +522,31 @@ $this->drupalPost('admin/build/translate/translate', $search, t('Filter')); $this->assertText(t('No strings found for your search.'), t("Search didn't find the invalid string.")); } + + function testJavaScriptTranslation() { + $user = $this->drupalCreateUser(array('translate interface', 'administer languages', 'access administration pages')); + $this->drupalLogin($user); + + // Add French language. + $this->drupalPost('admin/settings/language/add', array('langcode' => $langcode = 'fr'), t('Add language')); + + // Build the JavaScript translation file for French. + $this->drupalGet('admin/build/translate/translate'); + $string = db_fetch_object(db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE \'%.js%\' AND textgroup = \'default\'')); + $edit = array('translations['. $langcode .']' => 'french translation'); + $this->drupalPost('admin/build/translate/edit/' . $string->lid, $edit, t('Save translations')); + locale_inc_callback('_locale_rebuild_js', $langcode); + $file = db_fetch_object(db_query('SELECT javascript FROM {languages} WHERE language = \'%s\'', $langcode)); + $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')))); + } } /**