Skip to content
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

Locale handling should be improved #1

Closed
averbraeck opened this issue Jan 20, 2023 · 6 comments
Closed

Locale handling should be improved #1

averbraeck opened this issue Jan 20, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@averbraeck
Copy link
Owner

The use of Locale and DefaultLocale in djunits is not optimal. The following issues should (at least) be repaired:

  • use the standard Java names for the Locale files, such as en-US for the US locale and nl-NL for the Dutch locale.
  • always format the numbers according to the rules of the Locale.
  • remove the DefaultLocale class. Use the en_US locale files for prefixes when the used Locale does not have prefix files. Still format the numbers according to the used Locale.
  • add unit tests using different Locales, for reading (parsing) quantities as well as outputting (formatted) quantities.
@averbraeck averbraeck added the bug Something isn't working label Jan 20, 2023
@averbraeck
Copy link
Owner Author

Note that the DefaultLocale in Java is set for the entire JVM. This means that our Locale for displaying units can use a static variable. See https://www.oracle.com/technical-resources/articles/javase/locale.html. Note that every time when a formatted scalar, vector or matrix is printed the code needs to check whether the locale has changed, and load the corresponding resource bundle if it has. The Localization class can handle the reading of the resource bundles.

averbraeck added a commit that referenced this issue Jan 21, 2023
When the current default JVM Locale cannot be found, djunits will
fallback to en_US (or en for short).
averbraeck added a commit that referenced this issue Jan 21, 2023
The Localization (now UnitLocale) class is just providing localization
for the units through the corresponding resource bundles; the formatting
of the numbers will be done with a NumberFormatter that corresponds to
the current default (or given) Locale.
@averbraeck
Copy link
Owner Author

Resource bundles have moved to a subfolder 'locale' in /src/main/resources (/resources in generated jar files), and names of the resource bundles have been shortened (no need to start with 'locale' anymore).

@averbraeck
Copy link
Owner Author

averbraeck commented Jan 21, 2023

UnitSystem points to a static resource bundle loaded as a UnitLocale. Unit did not do this yet, which is quite strange. Let's define a static localization also in the Unit, so it can print names, abbreviations, etc. in a localized form as well. A few extra methods will be created for returning localized unit strings.

@averbraeck
Copy link
Owner Author

There are two categories of Locales: those for DISPLAY and those for FORMAT. The FORMAT one takes care of digits, decimal separator and thousands separator. The DISPLAY one takes care of formatting for a user interface in a local language. Therefore, we clearly have to use just the DISPLAY category of the current default Locale. This means that we can, e.g., use Dutch symbols with English formatted numbers,such as 14.2 km/u. Here, the Locale.Category.DISPLAY is nl_NL or nl, whereas the Locale.Category.FORMAT is en_US or en.

averbraeck added a commit that referenced this issue Jan 21, 2023
Added: getLocalizedDisplayAbbreviation(), getLocalizedTextualAbbreviation(), getLocalizedName(). These will always use the current default locale for the JVM.
averbraeck added a commit that referenced this issue Jan 21, 2023
This unit tests was commented out because it did not work. Now, it works
as intended, and retrieves the names and abbreviations for Durations.
Some more tests will be implemented, e.g., the ones using special
symbols.
@averbraeck
Copy link
Owner Author

This test code now finally works (it was commented out in the unit tests because it never worked as intended):

        Locale.setDefault(Locale.US);
        assertEquals("Duration", DurationUnit.MINUTE.getLocalizedName());
        assertEquals("h", DurationUnit.HOUR.getLocalizedDisplayAbbreviation());
        assertEquals("hour", DurationUnit.HOUR.getLocalizedTextualAbbreviation());
        Locale.setDefault(new Locale("nl", "NL"));
        assertEquals("Tijdsduur", DurationUnit.MINUTE.getLocalizedName());
        assertEquals("u", DurationUnit.HOUR.getLocalizedDisplayAbbreviation());
        assertEquals("uur", DurationUnit.HOUR.getLocalizedTextualAbbreviation());

averbraeck added a commit that referenced this issue Jan 21, 2023
And add method getLocalizedAbbreviations()
@averbraeck
Copy link
Owner Author

Localized methods are available and working.
Unit tests have been added and work properly.
Implications for printing and parsing scalars will be picked up in Issue #3 and Issue #4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant