function _locale_rebuild_js in Drupal 6
Same name and namespace in other branches
- 8 core/modules/locale/locale.module \_locale_rebuild_js()
- 7 includes/locale.inc \_locale_rebuild_js()
- 9 core/modules/locale/locale.module \_locale_rebuild_js()
(Re-)Creates the JavaScript translation file for a language.
Parameters
$language: The language, the translation file should be (re)created for.
Related topics
1 call to _locale_rebuild_js()
- locale_languages_delete_form_submit in includes/
locale.inc - Process language deletion submissions.
1 string reference to '_locale_rebuild_js'
- locale_update_js_files in modules/
locale/ locale.module - Update JavaScript translation file, if required, and add it to the page.
File
- includes/
locale.inc, line 2147 - Administration functions for locale.module.
Code
function _locale_rebuild_js($langcode = NULL) {
if (!isset($langcode)) {
global $language;
}
else {
// Get information about the locale.
$languages = language_list();
$language = $languages[$langcode];
}
// Construct the array for JavaScript translations.
// Only add strings with a translation to the translations array.
$result = db_query("SELECT s.lid, s.source, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.location LIKE '%%.js%%' AND s.textgroup = 'default'", $language->language);
$translations = array();
while ($data = db_fetch_object($result)) {
$translations[$data->source] = $data->translation;
}
// Construct the JavaScript file, if there are translations.
$data_hash = NULL;
$data = $status = '';
if (!empty($translations)) {
$data = "Drupal.locale = { ";
if (!empty($language->formula)) {
$data .= "'pluralFormula': function(\$n) { return Number({$language->formula}); }, ";
}
$data .= "'strings': " . drupal_to_js($translations) . " };";
$data_hash = md5($data);
}
// Construct the filepath where JS translation files are stored.
// There is (on purpose) no front end to edit that variable.
$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.
$changed_hash = $language->javascript != $data_hash;
if (!empty($language->javascript) && (!$data || $changed_hash)) {
file_delete(file_create_path($dir . '/' . $language->language . '_' . $language->javascript . '.js'));
$language->javascript = '';
$status = 'deleted';
}
// 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.
if (file_save_data($data, $dest)) {
$language->javascript = $data_hash;
// If we deleted a previous version of the file and we replace it with a
// new one we have an update.
if ($status == 'deleted') {
$status = 'updated';
}
elseif ($changed_hash) {
$status = 'created';
}
else {
$status = 'rebuilt';
}
}
else {
$language->javascript = '';
$status = 'error';
}
}
// Save the new JavaScript hash (or an empty value if the file 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.
// This is necessary to keep the variable consistent with the database
// version of the language and to prevent checking against an outdated hash.
$default_langcode = language_default('language');
if ($default_langcode == $language->language) {
$default = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $default_langcode));
variable_set('language_default', $default);
}
}
// Log the operation and return success flag.
switch ($status) {
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 as the JavaScript translation file has
// been created again.
case 'created':
watchdog('locale', 'Created JavaScript translation file for the language %language.', array(
'%language' => t($language->name),
));
return TRUE;
case 'deleted':
watchdog('locale', 'Removed JavaScript translation file for the language %language, because no translations currently exist for that language.', array(
'%language' => t($language->name),
));
return TRUE;
case 'error':
watchdog('locale', 'An error occurred during creation of the JavaScript translation file for the language %language.', array(
'%language' => t($language->name),
), WATCHDOG_ERROR);
return FALSE;
default:
// No operation needed.
return TRUE;
}
}