Skip to content

feat: add list format convenience wrapper#2223

Closed
MikaelSiidorow wants to merge 2 commits intolingui:mainfrom
MikaelSiidorow:add-i18n-list-convenience-wrapper
Closed

feat: add list format convenience wrapper#2223
MikaelSiidorow wants to merge 2 commits intolingui:mainfrom
MikaelSiidorow:add-i18n-list-convenience-wrapper

Conversation

@MikaelSiidorow
Copy link
Copy Markdown

Description

Add a new convenience wrapper for Intl.ListFormat with i18n.list. Similarly to i18n.number is for Intl.NumberFormat and i18n.date is for Intl.DateTimeFormat.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
    • The ES target change might need to be considered breaking change, though this new functionality is opt-in and existing features should work as before.
  • Documentation update
  • Examples update

Fixes #2222

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview Apr 8, 2025 8:14am

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2025

size-limit report 📦

Path Size
packages/core/dist/index.mjs 2.91 KB (0%)
packages/detect-locale/dist/index.mjs 618 B (0%)
packages/react/dist/index.mjs 1.35 KB (0%)

Copy link
Copy Markdown
Collaborator

@vonovak vonovak left a comment

Choose a reason for hiding this comment

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

Hello and thanks for the PR,
my personal opinion is that these helpers should not be part of Lingui (not even the helpers for date and number).

But I'm not the one to decide :)

@timofei-iatsenko
Copy link
Copy Markdown
Collaborator

my personal opinion is that these helpers should not be part of Lingui (not even the helpers for date and number).

Could you elaborate why?

But I'm not the one to decide :)

Who should? This is the best and worse part of the opensource, someone need to take decision and show the direction of the development, but every one could put it's own 5 cents into discussion. It's very hard to take all decisions alone, considering the fact i'm even not an official maintainer

@timofei-iatsenko
Copy link
Copy Markdown
Collaborator

BTW, the PR looks good to me

@vonovak
Copy link
Copy Markdown
Collaborator

vonovak commented Apr 9, 2025

hi! Let me expand on it (I thought you were somehow more involved on the project @timofei-iatsenko, sorry).

People should understand that these apis are no magic, just Intl apis. And so I'd suggest they use:

const getIntl = (locales) => new Intl.ListFormat(locales, myFavoriteFormat)
const memoized = memoizeOne(getIntl)

export function listWithMyFavoriteFormat(
  i18n: I18n,
  values: string[],
): string {
  const _locales = i18n.locales

  const formatter = memoized(_locales)

  return formatter.format(values)
}

@vonovak
Copy link
Copy Markdown
Collaborator

vonovak commented Apr 9, 2025

similar PR was previously rejected for these reasons: #1708

@dsonet
Copy link
Copy Markdown
Contributor

dsonet commented Apr 14, 2025

Then maybe move date, number and this list into helper/util package, user can import them as needed.

@timofei-iatsenko
Copy link
Copy Markdown
Collaborator

@vonovak that's a fair enough. From other side, i found it really useful in our project. It's shorter, doing all needed essentials (memoization, current locace, etc)

The @dsonet proposition is also good, but could not imagine right now a good API for this one. Currently, it's implemented as i18n instance methods, so it always has access to the current locale and works for react / vaniala and any other solution.

If you create this as a separate helpers, you will have to pass i18n instance manually or pass locale and it wouldn't be much difference from just using intl apis directly.

For react that could be achived by the additional hook, which will tie everethying together, but that would only for react, for other platforms has to be re-done

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 15, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 76.80%. Comparing base (6bb8983) to head (b3f2283).
Report is 186 commits behind head on main.

Files with missing lines Patch % Lines
packages/core/src/i18n.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2223      +/-   ##
==========================================
- Coverage   77.05%   76.80%   -0.25%     
==========================================
  Files          84       88       +4     
  Lines        2157     2492     +335     
  Branches      555      648      +93     
==========================================
+ Hits         1662     1914     +252     
- Misses        382      463      +81     
- Partials      113      115       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vonovak
Copy link
Copy Markdown
Collaborator

vonovak commented Apr 17, 2025

From other side, i found it really useful in our project. It's shorter, doing all needed essentials (memoization, current locale, etc)

I have no doubt it's useful but from a library maintenance point of view I find it problematic.

If you create this as a separate helpers, you will have to pass i18n instance manually or pass locale and it wouldn't be much difference from just using intl apis directly.

is i18n.list(["a", "b", "c"], { type: "conjunction" }) really that much better than list(i18n, ["a", "b", "c"], { type: "conjunction" })? The first one requires code in the library that comes with the issues I listed above. The second lives in userland. And yes, there's not much difference from Intl apis directly, and that's a good thing! That makes it more transparent.

These helper functions are 5-10 lines and we could just list them somewhere for people to copy-paste them into their projects. Zero responsibility for Lingui - it's the responsibility of Intl.

@MikaelSiidorow
Copy link
Copy Markdown
Author

Makes sense! We've already gone with a userland wrapper, since we wanted to take these into use as soon as possible.

The wrappers are convenient, but probably not worth library maintenance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend @lingui/core convenience wrapper for Intl.ListFormat

4 participants