- Patch #115047 by mindless: token replacement with format_plural.

6.x
Dries Buytaert 2007-03-07 13:08:04 +00:00
parent 0be18f09ed
commit ae093d1386
3 changed files with 40 additions and 13 deletions

View File

@ -927,34 +927,63 @@ function format_xml_elements($array) {
* This function ensures that the string is pluralized correctly. Since t() is
* called by this function, make sure not to pass already-localized strings to it.
*
* For example:
* @code
* $output = format_plural($node->comment_count, '1 comment', '@count comments');
* @endcode
*
* Example with additional replacements:
* @code
* $output = format_plural($update_count,
* 'Changed the content type of 1 post from %old-type to %new-type.',
* 'Changed the content type of @count posts from %old-type to %new-type.',
* array('%old-type' => $info->old_type, '%new-type' => $info->new_type)));
* @endcode
*
* @param $count
* The item count to display.
* @param $singular
* The string for the singular case. Please make sure it is clear this is
* singular, to ease translation (e.g. use "1 new comment" instead of "1 new").
* Do not use @count in the singluar string.
* @param $plural
* The string for the plural case. Please make sure it is clear this is plural,
* to ease translation. Use @count in place of the item count, as in "@count
* new comments".
* @param $args
* An associative array of replacements to make after translation. Incidences
* of any key in this array are replaced with the corresponding value.
* Based on the first character of the key, the value is escaped and/or themed:
* - !variable: inserted as is
* - @variable: escape plain text to HTML (check_plain)
* - %variable: escape text and theme as a placeholder for user-submitted
* content (check_plain + theme_placeholder)
* Note that you do not need to include @count in this array.
* This replacement is done automatically for the plural case.
* @return
* A translated string.
*/
function format_plural($count, $singular, $plural) {
if ($count == 1) return t($singular, array("@count" => $count));
function format_plural($count, $singular, $plural, $args = array()) {
if ($count == 1) {
return t($singular, $args);
}
$args['@count'] = $count;
// get the plural index through the gettext formula
$index = (function_exists('locale_get_plural')) ? locale_get_plural($count) : -1;
if ($index < 0) { // backward compatibility
return t($plural, array("@count" => $count));
return t($plural, $args);
}
else {
switch ($index) {
case "0":
return t($singular, array("@count" => $count));
return t($singular, $args);
case "1":
return t($plural, array("@count" => $count));
return t($plural, $args);
default:
return t(strtr($plural, array("@count" => '@count['. $index .']')), array('@count['. $index .']' => $count));
unset($args['@count']);
$args['@count['. $index .']'] = $count;
return t(strtr($plural, array('@count' => '@count['. $index .']')), $args);
}
}
}

View File

@ -351,9 +351,7 @@ function node_node_type($op, $info) {
$update_count = node_type_update_nodes($info->old_type, $info->type);
if ($update_count) {
$substr_pre = 'Changed the content type of ';
$substr_post = strtr(' from %old-type to %type.', array('%old-type' => theme('placeholder', $info->old_type), '%type' => theme('placeholder', $info->type)));
drupal_set_message(format_plural($update_count, $substr_pre .'@count post'. $substr_post, $substr_pre .'@count posts'. $substr_post));
drupal_set_message(format_plural($update_count, 'Changed the content type of 1 post from %old-type to %type.', 'Changed the content type of @count posts from %old-type to %type.', array('%old-type' => $info->old_type, '%type' => $info->type)));
}
}
}
@ -391,7 +389,7 @@ function node_type_delete_confirm($type) {
$num_nodes = db_num_rows(db_query("SELECT * FROM {node} WHERE type = '%s'", $type->type));
if ($num_nodes) {
$caption .= '<p>'. strtr(format_plural($num_nodes, '<strong>Warning:</strong> there is currently @count %type post on your site. It may not be able to be displayed or edited correctly, once you have removed this content type.', '<strong>Warning:</strong> there are currently @count %type posts on your site. They may not be able to be displayed or edited correctly, once you have removed this content type.'), array('%type' => theme('placeholder', $type->name))) .'</p>';
$caption .= '<p>'. format_plural($num_nodes, '<strong>Warning:</strong> there is currently 1 %type post on your site. It may not be able to be displayed or edited correctly, once you have removed this content type.', '<strong>Warning:</strong> there are currently @count %type posts on your site. They may not be able to be displayed or edited correctly, once you have removed this content type.', array('%type' => $type->name)) .'</p>';
}
$caption .= '<p>'. t('This action cannot be undone.') .'</p>';

View File

@ -1398,10 +1398,10 @@ function system_modules_confirm_form($modules, $form_values = array()) {
$missing_dependencies[$k] = $info['name'] ? $info['name'] : drupal_ucfirst($dependency);
}
$t_argument = array(
'%module' => $modules[$name]->info['name'],
'%dependencies' => implode(', ', $missing_dependencies),
'@module' => $modules[$name]->info['name'],
'@dependencies' => implode(', ', $missing_dependencies),
);
$items[] = strtr(format_plural(count($missing_dependencies), 'You must enable the %dependencies module to install %module.', 'You must enable the %dependencies modules to install %module.'), $t_argument);
$items[] = format_plural(count($missing_dependencies), 'You must enable the @dependencies module to install @module.', 'You must enable the @dependencies modules to install @module.', $t_argument);
}
$form['text'] = array('#value' => theme('item_list', $items));
}