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

internal date/number formatters are not reexported from @angular/common #20536

Closed
iexela opened this Issue Nov 20, 2017 · 8 comments

Comments

Projects
None yet
6 participants
@iexela

iexela commented Nov 20, 2017

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Starting from the version 5 Angular uses CLDR data for number/date/currency internalization. It uses internal formatters for this purpose and does not reexports them to be available for developers. I mean formatters describes in these files: format_date.ts, format_number.ts.

Expected behavior

Reexported formatters will allow to localize data in code.

What is the motivation / use case for changing the behavior?

Sometimes it is necessary to translate dates or numbers in code rather than in templates.
For example, when we use 3rd-party components, like ag-grid, we can define custom formatters for date represented in grid and it will be a large overhead to use Angular component for this purpose. Much easier is to use formatting capabilities embedded into Angular itself to reach consistent formatting.
Another case is messages generated in code.

Environment


Angular version: 5.x.x
@trotyl

This comment has been minimized.

Contributor

trotyl commented Nov 20, 2017

Relates to #3809, #15107.

@vicb

This comment has been minimized.

Contributor

vicb commented Nov 21, 2017

Our intent with the refactoring in 5.x was to make this publicly available at some point.
We will do that at some point when we have some confidence in the new API.
(A lot of devs are using the pipes to format dates & numbers in their code today which is definitely not a good thing)

@Toxicable

This comment has been minimized.

Contributor

Toxicable commented Nov 21, 2017

I had the idea to put those date functions in a service rather than just exporting a bunch of functions but due to the services not being tree shakable I didn't proceed, maybe once that's a thing we could take a look back at it?

@iexela

This comment has been minimized.

iexela commented Nov 22, 2017

Injection of date pipe solved my problem, thank you.
Although from my perspective this solution looks like a workaround.
Glad to hear that you have plans to make format API public.

@alan-agius4

This comment has been minimized.

Contributor

alan-agius4 commented Dec 28, 2017

Yeah same thing I hate to inject the pipe, it does feel wrong.

i like the service approach, TBH, as like that when you use a method you dont’t need to provide the LOCAL_ID, as some pre functions that there are available require the localId as param at the moment.

Maybe as a starter you can export these method similar to what you are doing for other methods like getLocalCurrencySymbol?, since really I dont know when we’ll have treeshaking on services.

I’ll gladly do a PR if it is needed :)

@Toxicable

This comment has been minimized.

Contributor

Toxicable commented Dec 28, 2017

It would be better to do a proper job of exporting these functions with a Design Doc and as a service specifically designed for this.
Doing a half job by exporting random functions into the public API can force us into a bad position if we want to come back and refactor it into a service later

@alan-agius4

This comment has been minimized.

Contributor

alan-agius4 commented Dec 28, 2017

@ocombe

This comment has been minimized.

Contributor

ocombe commented Dec 29, 2017

It's on our todo list to export those functions, but we need to refactor them a bit to make them easier to use by anyone.

@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018

ocombe added a commit to ocombe/angular that referenced this issue Feb 22, 2018

feat(common): export pipe utility functions
The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

ocombe added a commit to ocombe/angular that referenced this issue Feb 22, 2018

feat(common): export pipe utility functions
The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

ocombe added a commit to ocombe/angular that referenced this issue Feb 23, 2018

feat(common): export pipe utility functions
The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

ocombe added a commit to ocombe/angular that referenced this issue Feb 23, 2018

feat(common): export functions to format numbers, percents, currencie…
…s & dates

The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

ocombe added a commit to ocombe/angular that referenced this issue Feb 23, 2018

feat(common): export functions to format numbers, percents, currenci…
…es & dates

The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

vicb added a commit to vicb/angular that referenced this issue Feb 23, 2018

feat(common): export functions to format numbers, percents, currencie…
…s & dates

The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

@vicb vicb closed this in 4180912 Feb 23, 2018

smdunn pushed a commit to smdunn/angular that referenced this issue Feb 28, 2018

feat(common): export functions to format numbers, percents, currencie…
…s & dates (angular#22423)

The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

PR Close angular#22423

leo6104 added a commit to leo6104/angular that referenced this issue Mar 25, 2018

feat(common): export functions to format numbers, percents, currencie…
…s & dates (angular#22423)

The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.

Fixes angular#20536

PR Close angular#22423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment