robisim74 / angular-l10n

Angular library to translate texts, dates and numbers
MIT License
380 stars 59 forks source link

DateTimeFormat independent from language[-region] #302

Closed dzena closed 4 years ago

dzena commented 4 years ago

Hi Roberto.

Is it possible to set a DateTime format independent from the current language[-region]? My app has support for multiple languages. Is it possible to use en-US for translations and at the same time to use Italian DateTime format?

For the time format is easy, i can just use unicode extensions: this._translationService.setLocale({ language: 'en-GB-u-hc-h24' }); for instance. But the problem is the date format. US dates are 05/25/2020 and Italian are 25/05/2020 or 25.05.2020.

I uploaded the part of the UI, maybe it will help understand the problem better.

Thank you!

Screenshot 2020-05-25 at 10 58 39

robisim74 commented 4 years ago

Hi Milan,

Short answer: no it is not possible to manage multiple locales at the same time.

Long answer: you could create an alternative locale (managed by yourself) for the dates. You could then pass your locale only to dates (through pipes or APIs, not directives):

{{ today | l10nDate:myLocale.language:{ dateStyle: 'short' } }

dzena commented 4 years ago

@robisim74 Thanks for the quick answer. I was thinking in the similar direction.

Do you think that this feature could belong to this lib? Maybe I could find some time for a PR.

robisim74 commented 4 years ago

Maybe you've already thought about it, but there is a faster way to have alternative locales: you can add a property to L10nLocale, for example a timeLanguage.

You should work with the any type when you set the locale, but then you wouldn't need to handle anything else, and you could just use it like this:

{{ today | l10nDate:locale.timeLanguage:{ dateStyle: 'short' } }

dzena commented 4 years ago

Thanks a lot.

I've considered this option as well. But it means that i would need to cast the locale to any all the time.

If you think it makes sense to have this possibility as a first class citizen of the lib, i think i could find the time end of June and make a PR. Just let me know, and we can sync later on the details. Maybe it makes sense to have support for a dateLanguage, numberLanguage etc.

robisim74 commented 4 years ago

Contributions are always welcome.

In the meantime I leave this issue open to see if there are other opinions.

dzena commented 4 years ago

@robisim74 Hi Roberto, I started with the implementation of the dateLanguage. Do you want to discuss the possible implementation before I finish it, or should I just implement it and create a pull req? Thanks!

robisim74 commented 4 years ago

Hey Milan! As you said above, Maybe it makes sense to have support for a dateLanguage, numberLanguage

dzena commented 4 years ago

@robisim74 Hey Roberto! Here is the PR: https://github.com/robisim74/angular-l10n/pull/305

I tried to make minimal changes and no breaking changes were introduced. If more sophisticated solution is needed, i am happy to redo whats needed.

It includes only the dateLanguage. I'll add the numberLanguage until end of the week.

Cheers!

robisim74 commented 4 years ago

Thanks Milan! This evening I'll do a review.

robisim74 commented 4 years ago

@dzena Thanks again for your contribution. The feature is available in latest release:

Greetings