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 post time localization on Android #6742

Merged
merged 15 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@expo/webpack-config": "^19.0.0",
"@floating-ui/dom": "^1.6.3",
"@floating-ui/react-dom": "^2.0.8",
"@formatjs/intl-datetimeformat": "^6.16.5",
"@formatjs/intl-locale": "^4.0.0",
"@formatjs/intl-numberformat": "^8.10.3",
"@formatjs/intl-pluralrules": "^5.2.14",
Expand Down
32 changes: 28 additions & 4 deletions src/lib/strings/time.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
import {DateTimeFormat} from '@formatjs/intl-datetimeformat'
Copy link
Collaborator

Choose a reason for hiding this comment

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

We'd like to avoid importing something that isn't used on the web. Let's instead fork this file like time.ts vs time.web.ts and keep this import in the native version. This also lets you remove the inline checks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In fact, maybe it's worth moving the code into time.android.ts if we're sure the fix is only needed there

import {I18n} from '@lingui/core'

import {isNative, isWeb} from '#/platform/detection'

/**
* Formats the given date into a localized string.
*
* This function chooses different formatting approaches based on the platform:
* - On the Web, it uses the `i18n.date` method to format the date.
* - On Native, it adjusts the time zone offset and uses `DateTimeFormat` for formatting.
*
*/
export function niceDate(i18n: I18n, date: number | string | Date) {
const d = new Date(date)

return i18n.date(d, {
dateStyle: 'long',
timeStyle: 'short',
})
if (isWeb) {
return i18n.date(d, {
dateStyle: 'long',
timeStyle: 'short',
})
}

if (isNative) {
const timeZoneOffsetInMinutes = new Date().getTimezoneOffset()
d.setMinutes(d.getMinutes() - timeZoneOffsetInMinutes)

const dateFormatter = new DateTimeFormat(i18n.locale, {
dateStyle: 'long',
timeStyle: 'short',
})
return dateFormatter.format(d)
}
}

export function getAge(birthDate: Date): number {
Expand Down
27 changes: 27 additions & 0 deletions src/locale/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Don't remove -force from these because detection is VERY slow on low-end Android.
// https://github.com/formatjs/formatjs/issues/4463#issuecomment-2176070577
import '@formatjs/intl-locale/polyfill-force'
import '@formatjs/intl-datetimeformat/polyfill-force'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just flagging we need to measure the startup time impact of this on a real android device. We've had pretty bad regressions in the past due to related changes.

Another thing we might want to consider is forking this block between iOS and Android. If this is really android-only, it would be good to avoid activating this on iOS.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually maybe you can move this import to time.android.ts? Since that's the only place that needs that API. I realize this makes it a bit inconsistent but it does avoid potentially regressing something on iOS.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess I'm not sure whether that would break the dynamic imports below. Do they rely on ordering?

import '@formatjs/intl-pluralrules/polyfill-force'
import '@formatjs/intl-numberformat/polyfill-force'
import '@formatjs/intl-datetimeformat/locale-data/en'
import '@formatjs/intl-pluralrules/locale-data/en'
import '@formatjs/intl-numberformat/locale-data/en'

Expand Down Expand Up @@ -65,6 +67,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.ca: {
i18n.loadAndActivate({locale, messages: messagesCa})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/ca'),
import('@formatjs/intl-pluralrules/locale-data/ca'),
import('@formatjs/intl-numberformat/locale-data/ca'),
])
Expand All @@ -73,6 +76,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.de: {
i18n.loadAndActivate({locale, messages: messagesDe})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/de'),
import('@formatjs/intl-pluralrules/locale-data/de'),
import('@formatjs/intl-numberformat/locale-data/de'),
])
Expand All @@ -81,6 +85,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.en_GB: {
i18n.loadAndActivate({locale, messages: messagesEn_GB})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/en-GB'),
import('@formatjs/intl-pluralrules/locale-data/en'),
import('@formatjs/intl-numberformat/locale-data/en-GB'),
])
Expand All @@ -89,6 +94,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.es: {
i18n.loadAndActivate({locale, messages: messagesEs})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/es'),
import('@formatjs/intl-pluralrules/locale-data/es'),
import('@formatjs/intl-numberformat/locale-data/es'),
])
Expand All @@ -97,6 +103,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.fi: {
i18n.loadAndActivate({locale, messages: messagesFi})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/fi'),
import('@formatjs/intl-pluralrules/locale-data/fi'),
import('@formatjs/intl-numberformat/locale-data/fi'),
])
Expand All @@ -105,6 +112,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.fr: {
i18n.loadAndActivate({locale, messages: messagesFr})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/fr'),
import('@formatjs/intl-pluralrules/locale-data/fr'),
import('@formatjs/intl-numberformat/locale-data/fr'),
])
Expand All @@ -113,6 +121,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.ga: {
i18n.loadAndActivate({locale, messages: messagesGa})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/ga'),
import('@formatjs/intl-pluralrules/locale-data/ga'),
import('@formatjs/intl-numberformat/locale-data/ga'),
])
Expand All @@ -121,6 +130,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.gl: {
i18n.loadAndActivate({locale, messages: messagesGl})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/gl'),
import('@formatjs/intl-pluralrules/locale-data/gl'),
import('@formatjs/intl-numberformat/locale-data/gl'),
])
Expand All @@ -129,6 +139,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.hi: {
i18n.loadAndActivate({locale, messages: messagesHi})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/hi'),
import('@formatjs/intl-pluralrules/locale-data/hi'),
import('@formatjs/intl-numberformat/locale-data/hi'),
])
Expand All @@ -137,6 +148,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.hu: {
i18n.loadAndActivate({locale, messages: messagesHu})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/hu'),
import('@formatjs/intl-pluralrules/locale-data/hu'),
import('@formatjs/intl-numberformat/locale-data/hu'),
])
Expand All @@ -145,6 +157,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.id: {
i18n.loadAndActivate({locale, messages: messagesId})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/id'),
import('@formatjs/intl-pluralrules/locale-data/id'),
import('@formatjs/intl-numberformat/locale-data/id'),
])
Expand All @@ -153,6 +166,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.it: {
i18n.loadAndActivate({locale, messages: messagesIt})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/it'),
import('@formatjs/intl-pluralrules/locale-data/it'),
import('@formatjs/intl-numberformat/locale-data/it'),
])
Expand All @@ -161,6 +175,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.ja: {
i18n.loadAndActivate({locale, messages: messagesJa})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/ja'),
import('@formatjs/intl-pluralrules/locale-data/ja'),
import('@formatjs/intl-numberformat/locale-data/ja'),
])
Expand All @@ -169,6 +184,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.ko: {
i18n.loadAndActivate({locale, messages: messagesKo})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/ko'),
import('@formatjs/intl-pluralrules/locale-data/ko'),
import('@formatjs/intl-numberformat/locale-data/ko'),
])
Expand All @@ -177,6 +193,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.nl: {
i18n.loadAndActivate({locale, messages: messagesNl})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/nl'),
import('@formatjs/intl-pluralrules/locale-data/nl'),
import('@formatjs/intl-numberformat/locale-data/nl'),
])
Expand All @@ -185,6 +202,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.pl: {
i18n.loadAndActivate({locale, messages: messagesPl})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/pl'),
import('@formatjs/intl-pluralrules/locale-data/pl'),
import('@formatjs/intl-numberformat/locale-data/pl'),
])
Expand All @@ -193,6 +211,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.pt_BR: {
i18n.loadAndActivate({locale, messages: messagesPt_BR})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/pt'),
import('@formatjs/intl-pluralrules/locale-data/pt'),
import('@formatjs/intl-numberformat/locale-data/pt'),
])
Expand All @@ -201,6 +220,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.ru: {
i18n.loadAndActivate({locale, messages: messagesRu})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/ru'),
import('@formatjs/intl-pluralrules/locale-data/ru'),
import('@formatjs/intl-numberformat/locale-data/ru'),
])
Expand All @@ -209,6 +229,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.th: {
i18n.loadAndActivate({locale, messages: messagesTh})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/th'),
import('@formatjs/intl-pluralrules/locale-data/th'),
import('@formatjs/intl-numberformat/locale-data/th'),
])
Expand All @@ -217,6 +238,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.tr: {
i18n.loadAndActivate({locale, messages: messagesTr})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/tr'),
import('@formatjs/intl-pluralrules/locale-data/tr'),
import('@formatjs/intl-numberformat/locale-data/tr'),
])
Expand All @@ -225,6 +247,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.uk: {
i18n.loadAndActivate({locale, messages: messagesUk})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/uk'),
import('@formatjs/intl-pluralrules/locale-data/uk'),
import('@formatjs/intl-numberformat/locale-data/uk'),
])
Expand All @@ -233,6 +256,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.vi: {
i18n.loadAndActivate({locale, messages: messagesVi})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/vi'),
import('@formatjs/intl-pluralrules/locale-data/vi'),
import('@formatjs/intl-numberformat/locale-data/vi'),
])
Expand All @@ -241,6 +265,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.zh_CN: {
i18n.loadAndActivate({locale, messages: messagesZh_CN})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/zh-Hans'),
import('@formatjs/intl-pluralrules/locale-data/zh'),
import('@formatjs/intl-numberformat/locale-data/zh'),
])
Expand All @@ -249,6 +274,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.zh_HK: {
i18n.loadAndActivate({locale, messages: messagesZh_HK})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/yue'),
import('@formatjs/intl-pluralrules/locale-data/zh'),
import('@formatjs/intl-numberformat/locale-data/zh'),
])
Expand All @@ -257,6 +283,7 @@ export async function dynamicActivate(locale: AppLanguage) {
case AppLanguage.zh_TW: {
i18n.loadAndActivate({locale, messages: messagesZh_TW})
await Promise.all([
import('@formatjs/intl-datetimeformat/locale-data/zh-Hant'),
import('@formatjs/intl-pluralrules/locale-data/zh'),
import('@formatjs/intl-numberformat/locale-data/zh'),
])
Expand Down
37 changes: 37 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4113,6 +4113,31 @@
"@formatjs/intl-localematcher" "0.5.4"
tslib "^2.4.0"

"@formatjs/[email protected]":
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have two copies of this in the lockfile now. I think we need to bump other related libraries in package.json so that we have everything in a lockstep and transitive dependencies are shared as much as possible.

version "2.2.4"
resolved "https://registry.yarnpkg.com/@formatjs/ecma402-abstract/-/ecma402-abstract-2.2.4.tgz#355e42d375678229d46dc8ad7a7139520dd03e7b"
integrity sha512-lFyiQDVvSbQOpU+WFd//ILolGj4UgA/qXrKeZxdV14uKiAUiPAtX6XAn7WBCRi7Mx6I7EybM9E5yYn4BIpZWYg==
dependencies:
"@formatjs/fast-memoize" "2.2.3"
"@formatjs/intl-localematcher" "0.5.8"
tslib "2"

"@formatjs/[email protected]":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@formatjs/fast-memoize/-/fast-memoize-2.2.3.tgz#74e64109279d5244f9fc281f3ae90c407cece823"
integrity sha512-3jeJ+HyOfu8osl3GNSL4vVHUuWFXR03Iz9jjgI7RwjG6ysu/Ymdr0JRCPHfF5yGbTE6JCrd63EpvX1/WybYRbA==
dependencies:
tslib "2"

"@formatjs/intl-datetimeformat@^6.16.5":
version "6.16.5"
resolved "https://registry.yarnpkg.com/@formatjs/intl-datetimeformat/-/intl-datetimeformat-6.16.5.tgz#65302d7cfe532cbeebf818728fe370a2a4dfbec3"
integrity sha512-IukSD4pmyZZzpT5ysm6ONRKSu/OQq3vFKHA3exJDWSMjboPO+wO/bT9vOOw1y957N3vtrqvxHSfja6TYwbPpkQ==
dependencies:
"@formatjs/ecma402-abstract" "2.2.4"
"@formatjs/intl-localematcher" "0.5.8"
tslib "2"

"@formatjs/[email protected]":
version "1.4.7"
resolved "https://registry.yarnpkg.com/@formatjs/intl-enumerator/-/intl-enumerator-1.4.7.tgz#6ab697f3f8f18cf0cc6a6b028cb9c40db6001f3d"
Expand Down Expand Up @@ -4144,6 +4169,13 @@
dependencies:
tslib "^2.4.0"

"@formatjs/[email protected]":
version "0.5.8"
resolved "https://registry.yarnpkg.com/@formatjs/intl-localematcher/-/intl-localematcher-0.5.8.tgz#b11bbd04bd3551f7cadcb1ef1e231822d0e3c97e"
integrity sha512-I+WDNWWJFZie+jkfkiK5Mp4hEDyRSEvmyfYadflOno/mmKJKcB17fEpEH0oJu/OWhhCJ8kJBDz2YMd/6cDl7Mg==
dependencies:
tslib "2"

"@formatjs/intl-numberformat@^8.10.3":
version "8.10.3"
resolved "https://registry.yarnpkg.com/@formatjs/intl-numberformat/-/intl-numberformat-8.10.3.tgz#abc97cc6a7b7f1b20da9f07a976b5589c1192ab8"
Expand Down Expand Up @@ -18246,6 +18278,11 @@ ts-node@^10.9.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tslib@2:
version "2.8.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.8.1.tgz#612efe4ed235d567e8aba5f2a5fab70280ade83f"
integrity sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==

tslib@^1.11.1, tslib@^1.8.1, tslib@^1.9.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
Expand Down