//Pass the language code.
function custom_translation_create_js($language) {
// Get information about the locale.
$languages = language_list();
$language = $languages[$language];
// 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 INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.language = '%s' ORDER BY t.plural DESC", $language->language);
$translations = $plurals = array();
while ($data = db_fetch_object($result)) {
// 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;
}
}
}
// Only operate when there are translations.
if (!empty($translations)) {
// Construct the JavaScript file.
$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 directory 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'));
// Only create a new file if the content has changed.
if ($language->javascript != $data_hash) {
if (!empty($language->javascript)) {
// We are recreating the new file, so delete the old one.
file_delete(file_create_path($dir .'/'. $language->language .'_'. $language->javascript .'.js'));
$language->javascript = '';
}
else {
// The directory might not exist when there has not been a translation
// file before, so create it.
file_check_directory($dir, TRUE);
}
$str = "trans";
// The file name of the new JavaScript translation file.
$dest = $dir .'/'. $language->language .'_'. $str .'.js';
$path = file_save_data($data, $dest);
db_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $path ? $data_hash : '', $language->language);
// Only report updated or creation when there is actually a file created.
if ($path) {
// There has already been a translation file before.
if (!empty($language->javascript)) {
watchdog('locale', t('Updated JavaScript translation file for the language %language.', array('%language' => t($language->name))));
}
else {
watchdog('locale', t('Created JavaScript translation file for the language %language.', array('%language' => t($language->name))));
}
}
else {
watchdog('locale', t('An error occured during creation of the JavaScript translation file for the language %language.', array('%language' => t($language->name))));
}
}
}
// There are no strings for JavaScript translation. However, if there is a file,
// delete it and reset the database.
elseif (!empty($language->javascript)) {
// Delete the old JavaScript file
file_delete(file_create_path(variable_get('locale_js_directory', 'languages') .'/'. $language->language .'_'. $language->javascript .'.js'));
db_query("UPDATE {languages} SET javascript = '' WHERE language = '%s'", $language->language);
watchdog('locale', t('Deleted JavaScript translation file for the locale %language.', array('%language' => t($language->name))));
}
}
No comments:
Post a Comment