openems icon indicating copy to clipboard operation
openems copied to clipboard

feat: dynamic numberformat conversion by using current locale

Open miettal opened this issue 1 year ago • 2 comments

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 locale argument to utility functions and set LOCALE_ID as utility argument 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 useValue to useFactory. 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 de to testing function calling directly.

miettal avatar Aug 22 '24 14:08 miettal

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     

codecov[bot] avatar Aug 22 '24 15:08 codecov[bot]

rebased.

miettal avatar Sep 02 '24 07:09 miettal

it seems same function is in 2024.11 release. https://github.com/OpenEMS/openems/commit/a98d38c42969b782d93db720204829b258026e10

miettal avatar Nov 02 '24 00:11 miettal

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

miettal avatar Nov 02 '24 01:11 miettal

@lukasrgr or @venu-sagar: Could you please have a look?

sfeilmeier avatar Nov 04 '24 08:11 sfeilmeier

rebased

miettal avatar Jan 03 '25 12:01 miettal

@lukasrgr: FYI

sfeilmeier avatar Jan 03 '25 13:01 sfeilmeier

@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 avatar Jan 03 '25 14:01 sfeilmeier

@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.

Sn0w3y avatar Jan 04 '25 01:01 Sn0w3y

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

sfeilmeier avatar Jan 04 '25 21:01 sfeilmeier