feat: dynamic numberformat conversion by using current locale
Locale is depends on locale(tautology). but current openems-ui code is using de as hardcoding. This PR add mecanizm for locale determining by user environment.
- formatNumber is pure function, no state. so locale need to be specified by argument. In this change I added
localeargument to utility functions and set LOCALE_ID asutilityargument in caller. - In angular global locale context is in root module provider so it is difficult to change from function calling from outside. In this change I changed LOCAL_ID provider initialization from
useValuetouseFactory. Factory function use localstorage language setting currently because language and locale is not clearly separated. - I don't understand testing code well, so for now, I just added
deto testing function calling directly.
Codecov Report
Attention: Patch coverage is 21.87500% with 25 lines in your changes missing coverage. Please review.
Additional details and impacted files
@@ Coverage Diff @@
## develop #2757 +/- ##
=============================================
+ Coverage 56.70% 56.71% +0.01%
- Complexity 9567 9573 +6
=============================================
Files 2262 2262
Lines 96553 96565 +12
Branches 7122 7135 +13
=============================================
+ Hits 54740 54753 +13
- Misses 39781 39782 +1
+ Partials 2032 2030 -2
rebased.
it seems same function is in 2024.11 release. https://github.com/OpenEMS/openems/commit/a98d38c42969b782d93db720204829b258026e10
I will rewrite this pr to using localStorage.
Originally this pr avoided to use localStorage, because localStorage is global variable and using localStorage means depending global variable and it getting not pure function.
https://github.com/OpenEMS/openems/commit/a98d38c42969b782d93db720204829b258026e10#diff-f580914f33ae1bd24cf0aaee8a14d06808baad0414c65257dc1f138199a45ce1R8
@lukasrgr or @venu-sagar: Could you please have a look?
rebased
@lukasrgr: FYI
@venu-sagar: I receive an error
src/app/app.module.ts(60,45): error TS2531: Object is possibly 'null'.
in line
{ provide: LOCALE_ID, useFactory: () => (Language.getByKey(localStorage.LANGUAGE) ?? Language.getByBrowserLang(navigator.language)).key },
@sfeilmeier
{ provide: LOCALE_ID, useFactory: () => { const language = Language.getByKey(localStorage.LANGUAGE) ??Language.getByBrowserLang(navigator.language); return language?.key ?? 'en-US'; }, },
Is this approach for handling LOCALE_ID suitable for the issue? Please let me know if you have any suggestions or concerns.
Thanks @Sn0w3y. We already have a Language.DEFAULT, so I think it makes sense to use that. I'll fix it this way:
{ provide: LOCALE_ID, useFactory: () => (Language.getByKey(localStorage.LANGUAGE) ?? Language.getByBrowserLang(navigator.language) ?? Language.DEFAULT).key }
Fix will be merged with (or rather hidden in) https://github.com/OpenEMS/openems/pull/2658