Feature Request #4626
Add Text::readable_list() method
| Status: | Feedback | Start date: | 10/24/2012 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 100% |
||
| Category: | Core | |||
| Target version: | v3.4.0 | |||
| Resolution: | fixed | Points: | 1 |
Description
This is a method I added to a subclass of the Text class in my own project, but I think it'd be worth adding it to the core file. The Text class is currently host to a number of string formatting methods, and I think this would compliment them nicely.
It basically takes an array of strings or integers and converts them into a human readable list. For example: array('eggs', 'milk', 'cheese') => "eggs, milk and cheese".
I've inadvertently added the change to the following pull request: https://github.com/kohana/core/pull/312
Let met know what I need to do if I've messed things up with that. I don't know whether these things should be separate? It didn't seem appropriate to create a new feature branch.
History
Updated by Robert-Jan Dreu -rjd22- 7 months ago
I think it should support translations. I would put the word "and" in a public static so we can translate that one really easy. Like an dutch example: eieren, melk en kaas
Updated by Mark Locker 7 months ago
Robert-Jan Dreu rjd22 wrote:
I think it should support translations. I would put the word "and" in a public static so we can translate that one really easy. Like an dutch example: eieren, melk en kaas
When you say a "public static", what are you referring to exactly? I'm struggling to understand how you see it being translated.
Updated by Robert-Jan Dreu -rjd22- 7 months ago
Updated by Mark Locker 7 months ago
Robert-Jan Dreu rjd22 wrote:
On line #719
Replace `$string .= $word.' and ';` with `$string .= $word.' '.Text::$separator_term.' ';`On line #719
Add `public static $separator_term = 'and';`
So for someone to update the separator term to their language, they'd have to subclass the Text class and overwrite that property?
Updated by Jeremy Bush 7 months ago
- Target version changed from 3.3.1 to v3.4.0
This definitely needs to be taken out of that pull request.
Updated by Robert-Jan Dreu -rjd22- 7 months ago
Mark Locker wrote:
So for someone to update the separator term to their language, they'd have to subclass the Text class and overwrite that property?
Yep, That is how you do it with the other functions too afaik.
Updated by Robert-Jan Dreu -rjd22- 7 months ago
Btw you should split off "Miscellaneous spelling corrections." in a different pull request.
Updated by Mark Locker 7 months ago
Robert-Jan Dreu rjd22 wrote:
Btw you should split off "Miscellaneous spelling corrections." in a different pull request.
Yeah, as noted in my pull request I thought this would be wrong. How can I create a separate pull request for the same branch though? Or can't I?
Updated by Mark Locker 7 months ago
Any advice on the above? What do I need to do now to separate these?
Updated by Jeremy Bush 7 months ago
You would need to create a separate branch. They should be different issue #'s anyway.
Updated by Mark Locker 5 months ago
I've just made a new pull request for this, which includes Robert's suggestion: https://github.com/kohana/core/pull/328
Updated by Mark Locker 5 months ago
I've just made a new pull request for this, which includes Robert's suggestion: https://github.com/kohana/core/pull/328
Updated by David Pommer 5 months ago
A class wide conjunction? Why not use a param?
/**
* Format an array to coordinating conjunctions
*
* // Display: apples, bananas and oranges
* echo Text::enumerate(array('apples', 'bananas', 'oranges'));
*
* @param array items to format
* @param string conjunction, e.g. 'and', 'or'
* @return string
* @since 1.0
*/
public static function enumerate(array $array = NULL, $conjunction = 'and')
{
// Nothing to enumerate
if (empty($array))
{
return '';
}
if (count($array) > 1)
{
$last = array_pop($array);
}
$text = implode(', ', $array);
if (isset($last))
{
$text .= ' '.$conjunction.' '.$last;
}
return $text;
}
An addition could be if(Text::$translate_conjunction === TRUE) { $conjunction = __($conjunction); }
Updated by Lorenzo Pisani 3 months ago
- Status changed from New to Assigned
- Assignee set to Lorenzo Pisani
Updated by Lorenzo Pisani 3 months ago
- Status changed from Assigned to Closed
- % Done changed from 0 to 100
- Resolution set to fixed
Updated by Moshe Katz about 1 month ago
- Status changed from Closed to Feedback
(I wasn't really sure whether this should re-open the same issue or open a new one...)
The documentation of this new method does not reflect the method's default use of the serial, or "Oxford", comma. The method defaults to using this comma but the example given in the documentation directly above it does not.
/**
* Turns an array of strings/ints into a readable, comma separated list.
*
* For example: array('eggs', 'milk', 'cheese') => "eggs, milk and cheese".
*
* @throws InvalidArgumentException
* @param array $words An array of words.
* @param string $conjunction The conjunction term used (e.g. 'and', 'or' etc.).
* @param bool $serial_comma Whether a serial comma should be used.
* @return string An inline, human readable list.
*/
public static function readable_list(array $words, $conjunction = 'and', $serial_comma = TRUE)
...