Index: includes/locale.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/locale.inc,v retrieving revision 1.219 diff -u -p -r1.219 locale.inc --- includes/locale.inc 8 Jun 2009 05:00:11 -0000 1.219 +++ includes/locale.inc 1 Jul 2009 16:36:14 -0000 @@ -2422,6 +2422,7 @@ function _locale_rebuild_js($langcode = } // Construct the JavaScript file, if there are translations. + $data_hash = NULL; $data = $status = ''; if (!empty($translations)) { @@ -2440,22 +2441,23 @@ function _locale_rebuild_js($langcode = $dir = file_create_path(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(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 = ''; @@ -2464,8 +2466,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, @@ -2488,6 +2491,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); + // 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.28 diff -u -p -r1.28 locale.test --- modules/locale/locale.test 30 Jun 2009 18:21:06 -0000 1.28 +++ modules/locale/locale.test 1 Jul 2009 16:36:14 -0000 @@ -305,6 +305,49 @@ class LocaleTranslationFunctionalTest ex $this->assertNoText($name, t('Search now can not find the name.')); } + 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_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')))); + } + /** * Tests the validation of the translation input. */