-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Reader: Fix Post Count Not Appearing #87864
base: trunk
Are you sure you want to change the base?
Conversation
@@ -15,7 +15,9 @@ export const Count = ( { count, compact, numberFormat, forwardRef, primary, ...r | |||
className={ classnames( 'count', { 'is-primary': primary } ) } | |||
{ ...inheritProps } | |||
> | |||
{ compact ? formatNumberCompact( count ) || numberFormat( count ) : numberFormat( count ) } | |||
{ compact | |||
? formatNumber( count, getLocaleSlug() ) || numberFormat( count ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mystery continues 🤔 The Count component also uses its own formatNumber which, as far as I can see, was copied over from the original function. But the only thing that stops it breaking currently is the fallback; it doesn't format properly.
![Screenshot 2024-02-25 at 21 29 25](https://private-user-images.githubusercontent.com/43215253/307623614-852a59ea-0a5b-41bd-b0a0-4a0c58757d9d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0NTM2NjYsIm5iZiI6MTczOTQ1MzM2NiwicGF0aCI6Ii80MzIxNTI1My8zMDc2MjM2MTQtODUyYTU5ZWEtMGE1Yi00MWJkLWIwYTAtNGEwYzU4NzU3ZDlkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDEzMjkyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWFmZWI4ODQyN2FmNmEzN2MzZjA5YjdkODRjZjE0NWY2MGY0ODBlOWE3ZmYxY2M2NWQxNjQzNmY0MzE0YzU0YzMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.eklA-3oksmdFcG14a4E0Hgz0xi8Q9rTXDjG7ux5IT3Q)
This PR fixes it (I think?) but I wonder if I'm missing something? @jeyip @chriskmnds might you be able to advise from working on #79257? Thanks!
Ahhhhhh I see -
|
cc @tyxla - you might be pleased about the chance to clean up some code? 😁 |
@@ -1,14 +1,14 @@ | |||
import { Button } from '@automattic/components'; | |||
import formatNumber from '@automattic/components/src/number-formatters/lib/format-number'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd generally recommend importing only public API. That means going with one of the following alternatives:
- Exporting
formatNumber
publicly from the package and consuming it from there - Moving
formatNumber
to another package where it makes the most sense.
Since number formatting has little to do with components, I'd recommend the latter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I've moved it to i18n-utils, where I'd argue it makes the most sense, since this is mostly about ensuring correct number formatting in all languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
I'd like feedback @Automattic/i18n on this as well. Particularly because we already have a numberFormat
utility in the i18n-calypso
package and I'm not sure how much we want to duplicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The part that could be concerning is the locale. If I understand correctly, we won't consider the user locale from the user profile, but only the browser one, which is often not the right one. I have serious doubts that this is what we want to do; I believe instead we want to use the locale from the user's chosen interface language.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that we pass the locale slug specifically to avoid this? For instance, this is me with my WP.com profile in Turkish, and my browser in English.
![Screenshot 2024-03-18 at 08 49 20](https://private-user-images.githubusercontent.com/43215253/313608307-4e5354aa-afb0-4752-8789-50b6f529b1cb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0NTM2NjYsIm5iZiI6MTczOTQ1MzM2NiwicGF0aCI6Ii80MzIxNTI1My8zMTM2MDgzMDctNGU1MzU0YWEtYWZiMC00NzUyLTg3ODktNTBiNmY1MjliMWNiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDEzMjkyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTRkYjQ4MTg2ZjBkZTg1NDlhOTIzYzZjNTkwNDA5Y2RjNWJiNzEyMWIzYTlkYTcyZmZmYmE2MjQ1ZTg4NDdkMWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.6PmK0Z9I6dA-s7irQeOV7fjQ08evY0DKeOUrRl0QCu4)
Do note that in some cases, such as the above one, we're already using the browser for the number on the right but not the one on the left!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhhhh - I see. I think that's an oversight on my end; we should pass the slug, but I didn't update the ones where formattedNumber
is used. I'll change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I do wonder if it might be easier not to require the slug to be passed? translate
does it, so it must somehow be possible (though haven't looked into it yet!).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
translate()
is a different deal. It will use the internal i18n instance that gets updated once the slug changes, while the current formatting function you're working with won't do that - it will just statically use the default slug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgive me as I'm not quite sure I understand how this i18n works, but would it not be possible to just use the default slug like this?
wp-calypso/packages/i18n-utils/src/localize-url.tsx
Lines 21 to 23 in b9ea6f0
function getDefaultLocale(): Locale { | |
return getLocaleSlug?.() ?? 'en'; | |
} |
Would that have unexpected problems?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This leads to the same potential issue with using getLocaleSlug()
directly that I pointed out in #87864 (comment).
@@ -114,13 +115,13 @@ const ReaderTagSidebar = ( { | |||
<div className="reader-tag-sidebar-stats"> | |||
<div className="reader-tag-sidebar-stats__item"> | |||
<span className="reader-tag-sidebar-stats__count"> | |||
{ formatNumberCompact( tagStats?.data?.total_posts ) } | |||
{ formatNumber( tagStats?.data?.total_posts, localeSlug ) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see formatNumber()
from @automattic/components
and formatNumberCompact()
from /lib
are quite different and the latter is custom while the former uses Intl.NumberFormat
. While I prefer using Intl.NumberFormat()
generally, I'm concerned about any unexpected differences or regressions that this change might cause. Have you done a comparison between the two? I think some tests may be necessary to ensure we don't break something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some tests - see below for my comments on the differences! Only other difference I could find is that with the exact number, there is one less significant number in the digit (ie. 1000 becomes 1K instead of 1.0K).
I've checked the contexts in which this is used, and I personally don't think that's a big deal - especially since formatNumberCompact()
doesn't account for all locales, whereas Intl.NumberFormat
does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to list all the places where this is affected, since we might want to check them with design folks and ensure that the change is alright.
client/reader/tags/trending-tags.tsx
Outdated
import { addLocaleToPathLocaleInFront } from '@automattic/i18n-utils'; | ||
import { getLocaleSlug } from 'i18n-calypso'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few issues with using getLocaleSlug()
directly for getting the locale slug:
- It's shared between the client and the server so it might cause unexpected issues with translations on the server (and trending tags is SSR-ed as part of the
/tags
page) getLocaleSlug()
is just a method so if the locale changes, it won't trigger a re-render, which might cause obsolete strings to be displayed.
with that in mind, I'd recommend either:
- Using the
useTranslate()
hook for functional components, it also returns alocaleSlug
property. - Using the
localize()
high-order component for class components, it also returns alocaleSlug
property.
Hat tip to @yuliyan for the extra info on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, never knew that! I've made those changes now - let me know if I've misunderstood what you're getting at. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - that change seems alright. The only concern I have is around the inconsistent use of the locale slug - sometimes we'll pass it to formatNumber()
, sometimes we won't, and that may lead to inconsistent results when a non-EN locale is currently used for the user's WP.com interface.
@@ -1,195 +1,4 @@ | |||
import formatNumberCompact, { formatNumberMetric } from 'calypso/lib/format-number-compact'; | |||
|
|||
describe( 'formatNumberCompact', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what would happen if we run these tests against the alternative solution that you're recommending to use 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there are some differences - the Arabic version, for instance, used to be 1٫2 ألف
. It's now ١٫٢ ألف
.
I'm inclined to defer to Intl.NumberFormat
on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd want to double-check this though, since it might introduce unexpected regressions in some locales.
On top of the @Automattic/i18n team you already pinged, perhaps we could ask a native speaker? IIRC @alshakero could help, but he's on leave. The i18n team is also on a meetup this week AFAIK, so this might take a bit longer than usual.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely don't intend to block this work. I just don't feel particularly knowledgeable about this aspect of i18n. The i18n team has historically worked with RTL languages way more, so I hope they will have a better idea.
Proposed Changes
For some reason,
formatNumberCompact
doesn't seem to work. I'd like to propose getting rid of it for two reasons:formatNumber
, which as far as I can see achieves the same thing. In fact, it's already used in more recent code in the Reader - that's why the subscription count works, but not the post count!Testing Instructions
Edit: turns out you need to use a locale that isn't hardcoded in the
formatNumberCompact
list! For instance, British English.You can see the difference here: https://wordpress.com/read/feeds/25823
Before:
![Screenshot 2024-02-25 at 21 09 27](https://private-user-images.githubusercontent.com/43215253/307622584-bcf09cd9-707d-48c9-a8da-2f7b6c7de036.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0NTM2NjUsIm5iZiI6MTczOTQ1MzM2NSwicGF0aCI6Ii80MzIxNTI1My8zMDc2MjI1ODQtYmNmMDljZDktNzA3ZC00OGM5LWE4ZGEtMmY3YjZjN2RlMDM2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDEzMjkyNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk4MjlmZTY5ZmMyY2UzZTMyZGQ1NTA1ZDViMjc1YmNiNzkyMDlmMDdjNjkzODUxY2U0YWU0N2RmZmIwZWE0YmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.ztscmSwEWyUvb9zNnArh-vjCvWct8jWS9fP97gGRI_0)
After:
![Screenshot 2024-02-25 at 21 09 29](https://private-user-images.githubusercontent.com/43215253/307622590-f49d5ff2-caa1-4268-9db2-efaeae2a2b02.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0NTM2NjUsIm5iZiI6MTczOTQ1MzM2NSwicGF0aCI6Ii80MzIxNTI1My8zMDc2MjI1OTAtZjQ5ZDVmZjItY2FhMS00MjY4LTlkYjItZWZhZWFlMmEyYjAyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDEzMjkyNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAxNTgxNmNhNTkyM2VjYTgzN2M5NTVlMWRlMmFjYzRlYWM5ZWZkNjJjZDZjYzkyNTJlZTYwOGI0YmU1OTViZTUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.V6MakH4GmtQ7ACBi-dLhrrROzwmLLJRUy2nnR4OiYhE)
cc @eoigal, @davemart-in