_locale_rebuild_js
- Versions
- 6 – 7
_locale_rebuild_js($langcode = NULL)
(Re-)Creates the JavaScript translation file for a language.
Parameters
$language The language, the translation file should be (re)created for.
Related topics
Code
includes/locale.inc, line 2865
<?php
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.
// We sort on plural so that we have all plural forms before singular forms.
$result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.location LIKE '%.js%' AND s.textgroup = :textgroup ORDER BY t.plural DESC", array(':language' => $language->language, ':textgroup' => 'default'));
$translations = $plurals = array();
foreach ($result as $data) {
// Only add this to the translations array when there is actually a translation.
if (!empty($data->translation)) {
if ($data->plural) {
// When the translation is a plural form, first add it to another array and
// wait for the singular (parent) translation.
if (!isset($plurals[$data->plid])) {
$plurals[$data->plid] = array($data->plural => $data->translation);
}
else {
$plurals[$data->plid] += array($data->plural => $data->translation);
}
}
elseif (isset($plurals[$data->lid])) {
// There are plural translations for this translation, so get them from
// the plurals array and add them to the final translations array.
$translations[$data->source] = array($data->plural => $data->translation) + $plurals[$data->lid];
unset($plurals[$data->lid]);
}
else {
// There are no plural forms for this translation, so just add it to
// the translations array.
$translations[$data->source] = $data->translation;
}
}
}
// Construct the JavaScript file, if there are translations.
$data = $status = '';
if (!empty($translations)) {
$data = "Drupal.locale = { ";
if (!empty($language->formula)) {
$data .= "'pluralFormula': function (\$n) { return Number({$language->formula}); }, ";
}
$data .= "'strings': " . drupal_json_encode($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 = '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)) {
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) {
// 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';
}
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.
if ($status) {
db_update('languages')
->fields(array(
'javascript' => $language->javascript,
))
->condition('language', $language->language)
->execute();
// 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_query("SELECT * FROM {languages} WHERE language = :language", array(':language' => $default_langcode))->fetchObject();
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 '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;
}
}
?>Login or register to post comments 