Problem/Motivation
Function format_interval() in the file: includes/common.inc returns in some cases wrong value "1" instead of correct value of given interval.
For example:
1 min 1 sek instead of 31 min 21 sec.
The $units variable is array with keyed singular|plural.
$units = array(
'1 year|@count years' => 31536000,
'1 month|@count months' => 2592000,
'1 week|@count weeks' => 604800,
'1 day|@count days' => 86400,
'1 hour|@count hours' => 3600,
'1 min|@count min' => 60,
'1 sec|@count sec' => 1
);
These keys are used as parameters in function format_plural(). This function calls locale_get_plural() function, if exists. Function locale_get_plural() returns plural form index for a specific number. The index is computed from the formula of given language. In short, it returns 0 if number is singular.
Some languages (f.e. Czech - langcode "cs") use singulars for numbers ending with number 1 (f.e. 21, 31.. 91, 101 etc.). That's why locale_get_plural() returns 0 = singular.
Czech language plural formula snippet ($language->formula):
if ((((n % 10) == 1) && ((n % 100) != 11))) {
$plural = ((0));
}
The problem is that all singulars in format_interval() are hardcoded to 1, so function returns 1 instead of 21. This function is used in Views for "Time ago" formater, so all views with time ago formater for Czech language display wrong values.
Step to reproduce
- Locale module must be enabled.
- Add Czech language at admin/config/regional/language
Use this simple test:
$time_diffs = array(21, 31, 41, 51, 81);
foreach ($time_diffs as $time_diff) {
$timeago1 = format_interval($time_diff, 2, 'cs');
$timeago2 = format_interval($time_diff, 2, 'en');
print $time_diff . ' sec. = ' . $timeago1 . ', should be ' . $timeago2;
print '<br>';
}
Results:
21 sec. = 1 sek, should be 21 sec
31 sec. = 1 sek, should be 31 sec
41 sec. = 1 sek, should be 41 sec
51 sec. = 1 sek, should be 51 sec
81 sec. = 1 min 1 sek, should be 1 min 21 sec
Proposed resolution
The $units key for singular could contain @count instead of "1".
$units = array(
'@count year|@count years' => 31536000,
'@count month|@count months' => 2592000,
'@count week|@count weeks' => 604800,
'@count day|@count days' => 86400,
'@count hour|@count hours' => 3600,
'@count min|@count min' => 60,
'@count sec|@count sec' => 1
);
Tested for test code above and works well.
Comment | File | Size | Author |
---|---|---|---|
#31 | 2712603-31.patch | 15.03 KB | quietone |
#31 | interdiff-29-31.txt | 571 bytes | quietone |
#29 | locale_format_interval-2712603-29.patch | 15.02 KB | Chi |
| |||
#7 | counts_for_singulars_in-2712603-7.patch | 3.49 KB | radimklaska |
#2 | d7-fix_singulars-2712603-01.patch | 906 bytes | martin_klima |
Comments
Comment #2
martin_klimaComment #3
martin_klimaComment #4
martin_klimaComment #5
radimklaskaGood catch Martin!
Steps I took to reproduce this:
drush en views views_ui devel devel_generate
Content: Post date
formatted asTime ago (with "ago" appended)
Martin's patch fixes the issue and applies cleanly.
Comment #6
radimklaskaD8 has the same problem. If I'm not mistaken it needs to be fixed in D8 first and then backported. So, updating...
Comment #7
radimklaskaSame fix, updated for D8.
Comment #9
radimklaskaHmm, tests failing... I thought that @count will just be replaced by "1" and the result will be the same. We will have to take a look at the test and figure out why it's failing...
Comment #25
Chi CreditAttribution: Chi commentedPatch for Drupal 10.
Comment #26
Chi CreditAttribution: Chi commentedComment #27
andypostRelated has been fixed #3378675: Remove hardcoded "X minutes" translatable strings
Comment #28
smustgrave CreditAttribution: smustgrave at Mobomo commented#25 failed
Comment #29
Chi CreditAttribution: Chi commentedComment #30
smustgrave CreditAttribution: smustgrave at Mobomo commentedThanks for fixing that.
Seems like a valid cleanup. The existing test updates seem adequate.
Comment #31
quietone CreditAttribution: quietone at PreviousNext commentedI'm triaging RTBC issues. I re-read the IS and the comments. I didn't find any unanswered questions.
The array was originally added in #76802: Introduce placeholder magic into t(). The first patch in that issue has the array with the hardcoded '1 sec' and I didn't see any discussion in that issue about changing that.
I skimmed the patch and saw this. I am able to fix that now.
Comment should be wrapped at 80 columns.
This should also be on 11.x
Comment #32
catchComment #34
catchCommitted/pushed to 11.x, thanks! This introduces a new translated string so tagging for 10.2.0 strings changes.
Comment #35
catch