Skip to content

Commit

Permalink
fix: change translation logic to handle plurals better
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Dec 13, 2024
1 parent 10852d3 commit 21b96b4
Show file tree
Hide file tree
Showing 2 changed files with 730 additions and 80 deletions.
260 changes: 230 additions & 30 deletions apps/dav/lib/CalDAV/Schedule/IMipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,95 @@ public function generateWhenStringSingular(EventReader $er): string {
// TRANSLATORS
// Indicates when a calendar event will happen, shown on invitation emails
// Output produced in order:
// In a day/week/month/year on July 1, 2024 for the entire day
// In a day/week/month/year on July 1, 2024 between 8:00 AM - 9:00 AM (America/Toronto)
// In 2 days/weeks/months/years on July 1, 2024 for the entire day
// In 2 days/weeks/months/years on July 1, 2024 between 8:00 AM - 9:00 AM (America/Toronto)
return match ([($occurring[0] > 1), !empty($endTime)]) {
[false, false] => $this->l10n->t('In a %1$s on %2$s for the entire day', [$occurring[1], $startDate]),
[false, true] => $this->l10n->t('In a %1$s on %2$s between %3$s - %4$s', [$occurring[1], $startDate, $startTime, $endTime]),
[true, false] => $this->l10n->t('In %1$s %2$s on %3$s for the entire day', [$occurring[0], $occurring[1], $startDate]),
[true, true] => $this->l10n->t('In %1$s %2$s on %3$s between %4$s - %5$s', [$occurring[0], $occurring[1], $startDate, $startTime, $endTime]),
// In a minute/hour/day/week/month/year on July 1, 2024 for the entire day
// In a minute/hour/day/week/month/year on July 1, 2024 between 8:00 AM - 9:00 AM (America/Toronto)
// In 2 minutes/hours/days/weeks/months/years on July 1, 2024 for the entire day
// In 2 minutes/hours/days/weeks/months/years on July 1, 2024 between 8:00 AM - 9:00 AM (America/Toronto)
return match ([$occurring['scale'], !empty($endTime)]) {
['past', false] => $this->l10n->n(
'In the past on %1$s for the entire day',
'',
$occurring['interval'],
[$startDate]
),
['minute', false] => $this->l10n->n(
'In a minute on %1$s for the entire day',
'In %n minutes on %1$s for the entire day',
$occurring['interval'],
[$startDate]
),
['hour', false] => $this->l10n->n(
'In a hour on %1$s for the entire day',
'In %n hours on %1$s for the entire day',
$occurring['interval'],
[$startDate]
),
['day', false] => $this->l10n->n(
'In a day on %1$s for the entire day',
'In %n days on %1$s for the entire day',
$occurring['interval'],
[$startDate]
),
['week', false] => $this->l10n->n(
'In a week on %1$s for the entire day',
'In %n weeks on %1$s for the entire day',
$occurring['interval'],
[$startDate]
),
['month', false] => $this->l10n->n(
'In a month on %1$s for the entire day',
'In %n months on %1$s for the entire day',
$occurring['interval'],
[$startDate]
),
['year', false] => $this->l10n->n(
'In a year on %1$s for the entire day',
'In %n years on %1$s for the entire day',
$occurring['interval'],
[$startDate]
),
['past', true] => $this->l10n->n(
'In the past on %1$s between %2$s - %3$s',
'',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
['minute', true] => $this->l10n->n(
'In a minute on %1$s between %2$s - %3$s',
'In %n minutes on %1$s between %2$s - %3$s',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
['hour', true] => $this->l10n->n(
'In a hour on %1$s between %2$s - %3$s',
'In %n hours on %1$s between %2$s - %3$s',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
['day', true] => $this->l10n->n(
'In a day on %1$s between %2$s - %3$s',
'In %n days on %1$s between %2$s - %3$s',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
['week', true] => $this->l10n->n(
'In a week on %1$s between %2$s - %3$s',
'In %n weeks on %1$s between %2$s - %3$s',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
['month', true] => $this->l10n->n(
'In a month on %1$s between %2$s - %3$s',
'In %n months on %1$s between %2$s - %3$s',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
['year', true] => $this->l10n->n(
'In a year on %1$s between %2$s - %3$s',
'In %n years on %1$s between %2$s - %3$s',
$occurring['interval'],
[$startDate, $startTime, $endTime]
),
default => $this->l10n->t('Could not generate when statement')
};
}
Expand Down Expand Up @@ -545,19 +625,139 @@ public function generateOccurringString(EventReader $er): string {
// TRANSLATORS
// Indicates when a calendar event will happen, shown on invitation emails
// Output produced in order:
// In a day/week/month/year on July 1, 2024
// In a day/week/month/year on July 1, 2024 then on July 3, 2024
// In a day/week/month/year on July 1, 2024 then on July 3, 2024 and July 5, 2024
// In 2 days/weeks/months/years on July 1, 2024
// In 2 days/weeks/months/years on July 1, 2024 then on July 3, 2024
// In 2 days/weeks/months/years on July 1, 2024 then on July 3, 2024 and July 5, 2024
return match ([($occurrenceIn[0] > 1), !empty($occurrence2), !empty($occurrence3)]) {
[false, false, false] => $this->l10n->t('In a %1$s on %2$s', [$occurrenceIn[1], $occurrence]),
[false, true, false] => $this->l10n->t('In a %1$s on %2$s then on %3$s', [$occurrenceIn[1], $occurrence, $occurrence2]),
[false, true, true] => $this->l10n->t('In a %1$s on %2$s then on %3$s and %4$s', [$occurrenceIn[1], $occurrence, $occurrence2, $occurrence3]),
[true, false, false] => $this->l10n->t('In %1$s %2$s on %3$s', [$occurrenceIn[0], $occurrenceIn[1], $occurrence]),
[true, true, false] => $this->l10n->t('In %1$s %2$s on %3$s then on %4$s', [$occurrenceIn[0], $occurrenceIn[1], $occurrence, $occurrence2]),
[true, true, true] => $this->l10n->t('In %1$s %2$s on %3$s then on %4$s and %5$s', [$occurrenceIn[0], $occurrenceIn[1], $occurrence, $occurrence2, $occurrence3]),
// In a minute/hour/day/week/month/year on July 1, 2024
// In a minute/hour/day/week/month/year on July 1, 2024 then on July 3, 2024
// In a minute/hour/day/week/month/year on July 1, 2024 then on July 3, 2024 and July 5, 2024
// In 2 minutes/hours/days/weeks/months/years on July 1, 2024
// In 2 minutes/hours/days/weeks/months/years on July 1, 2024 then on July 3, 2024
// In 2 minutes/hours/days/weeks/months/years on July 1, 2024 then on July 3, 2024 and July 5, 2024
return match ([$occurrenceIn['scale'], !empty($occurrence2), !empty($occurrence3)]) {
['past', false, false] => $this->l10n->n(
'In the past on %1$s',
'',
$occurrenceIn['interval'],
[$occurrence]
),
['minute', false, false] => $this->l10n->n(
'In a minute on %1$s',
'In %n minutes on %1$s',
$occurrenceIn['interval'],
[$occurrence]
),
['hour', false, false] => $this->l10n->n(
'In a hour on %1$s',
'In %n hours on %1$s',
$occurrenceIn['interval'],
[$occurrence]
),
['day', false, false] => $this->l10n->n(
'In a day on %1$s',
'In %n days on %1$s',
$occurrenceIn['interval'],
[$occurrence]
),
['week', false, false] => $this->l10n->n(
'In a week on %1$s',
'In %n weeks on %1$s',
$occurrenceIn['interval'],
[$occurrence]
),
['month', false, false] => $this->l10n->n(
'In a month on %1$s',
'In %n months on %1$s',
$occurrenceIn['interval'],
[$occurrence]
),
['year', false, false] => $this->l10n->n(
'In a year on %1$s',
'In %n years on %1$s',
$occurrenceIn['interval'],
[$occurrence]
),
['past', true, false] => $this->l10n->n(
'In the past on %1$s then on %2$s',
'',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['minute', true, false] => $this->l10n->n(
'In a minute on %1$s then on %2$s',
'In %n minutes on %1$s then on %2$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['hour', true, false] => $this->l10n->n(
'In a hour on %1$s then on %2$s',
'In %n hours on %1$s then on %2$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['day', true, false] => $this->l10n->n(
'In a day on %1$s then on %2$s',
'In %n days on %1$s then on %2$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['week', true, false] => $this->l10n->n(
'In a week on %1$s then on %2$s',
'In %n weeks on %1$s then on %2$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['month', true, false] => $this->l10n->n(
'In a month on %1$s then on %2$s',
'In %n months on %1$s then on %2$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['year', true, false] => $this->l10n->n(
'In a year on %1$s then on %2$s',
'In %n years on %1$s then on %2$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2]
),
['past', true, true] => $this->l10n->n(
'In the past on %1$s then on %2$s and %3$s',
'',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
['minute', true, true] => $this->l10n->n(
'In a minute on %1$s then on %2$s and %3$s',
'In %n minutes on %1$s then on %2$s and %3$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
['hour', true, true] => $this->l10n->n(
'In a hour on %1$s then on %2$s and %3$s',
'In %n hours on %1$s then on %2$s and %3$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
['day', true, true] => $this->l10n->n(
'In a day on %1$s then on %2$s and %3$s',
'In %n days on %1$s then on %2$s and %3$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
['week', true, true] => $this->l10n->n(
'In a week on %1$s then on %2$s and %3$s',
'In %n weeks on %1$s then on %2$s and %3$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
['month', true, true] => $this->l10n->n(
'In a month on %1$s then on %2$s and %3$s',
'In %n months on %1$s then on %2$s and %3$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
['year', true, true] => $this->l10n->n(
'In a year on %1$s then on %2$s and %3$s',
'In %n years on %1$s then on %2$s and %3$s',
$occurrenceIn['interval'],
[$occurrence, $occurrence2, $occurrence3]
),
default => $this->l10n->t('Could not generate next recurrence statement')
};

Expand Down Expand Up @@ -965,30 +1165,30 @@ public function isRoomOrResource(Property $attendee): bool {
public function minimizeInterval(\DateInterval $dateInterval): array {
// evaluate if time interval is in the past
if ($dateInterval->invert == 1) {
return [1, 'the past'];
return ['interval' => 1, 'scale' => 'past'];
}
// evaluate interval parts and return smallest time period
if ($dateInterval->y > 0) {
$interval = $dateInterval->y;
$scale = ($dateInterval->y > 1) ? 'years' : 'year';
$scale = 'year';
} elseif ($dateInterval->m > 0) {
$interval = $dateInterval->m;
$scale = ($dateInterval->m > 1) ? 'months' : 'month';
$scale = 'month';
} elseif ($dateInterval->d >= 7) {
$interval = (int)($dateInterval->d / 7);
$scale = ((int)($dateInterval->d / 7) > 1) ? 'weeks' : 'week';
$scale = 'week';
} elseif ($dateInterval->d > 0) {
$interval = $dateInterval->d;
$scale = ($dateInterval->d > 1) ? 'days' : 'day';
$scale = 'day';
} elseif ($dateInterval->h > 0) {
$interval = $dateInterval->h;
$scale = ($dateInterval->h > 1) ? 'hours' : 'hour';
$scale = 'hour';
} else {
$interval = $dateInterval->i;
$scale = 'minutes';
$scale = 'minute';
}

return [$interval, $scale];
return ['interval' => $interval, 'scale' => $scale];
}

/**
Expand Down
Loading

0 comments on commit 21b96b4

Please sign in to comment.