Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change translation logic to handle plurals better #49852

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

SebastianKrupinski
Copy link
Contributor

@SebastianKrupinski SebastianKrupinski commented Dec 13, 2024

Summary

  • Altered phrase generation logic to work better with plurals and time intervals
  • Added more test to cover all possible scenarios

Checklist

Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
@SebastianKrupinski
Copy link
Contributor Author

/backport to stable30

@SebastianKrupinski
Copy link
Contributor Author

Code added in 30, do not backport further.

[$startDate]
),
['minute', false] => $this->l10n->n(
'In a minute on %1$s for the entire day',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'In a minute on %1$s for the entire day',
'In %n minute on %1$s for the entire day',

If you use a plural, you must also use the %n placeholder.

Same for the rest of the changes :)

https://docs.nextcloud.com/server/latest/developer_manual/basics/front-end/l10n.html#correct-plurals

Copy link
Contributor Author

@SebastianKrupinski SebastianKrupinski Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below

return match ([$occurring['scale'], !empty($endTime)]) {
['past', false] => $this->l10n->n(
'In the past on %1$s for the entire day',
'',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems problematic. If the plural function of the user language considers $occurring['interval'] a plural then the string will be empty. If there is no number in the translation string just use t instead of n.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work correctly as is...

Singular
image

Plural
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$occurring['interval'] = is a time interval (0+ integer) it will never be empty.

return match ([$occurrenceIn['scale'], !empty($occurrence2), !empty($occurrence3)]) {
['past', false, false] => $this->l10n->n(
'In the past on %1$s',
'',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above

// 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)]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would a null or empty string comparison work too to replace empty?

https://dev.to/klnjmm/never-use-empty-function-in-php-4pb0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a isset() will work also.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use an explicit null and empty string comparison. Isset has unexpecte side effects too https://dev.to/aleksikauppila/using-isset-and-empty-hurts-your-code-aaa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🏗️ In progress
Development

Successfully merging this pull request may close these issues.

i18n: Translation does not work with multiple placeholders
3 participants