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

Formatter asShortWeight method throws unexpected errors with some locales #19744

Open
MoritzLost opened this issue Jan 20, 2023 · 7 comments
Open
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@MoritzLost
Copy link
Contributor

What steps will reproduce the problem?

I'm having trouble with the asShortWeight method of the formatter. This works as expected for some locales, but throws errors for others (in particular, de).

$formatter = new Formatter([
    'locale' => 'de',
]);

// this gives an error message
$result = $formatter->asShortWeight(5);

// those gives a slightly different error message
$result = $formatter->asShortWeight(5 / 1000);

What is the expected result?

The examples above should output 5 kg, 5 g and 5 tn, respectively. They do that if I use the en locale as well as some others I tested. However, with the de locale and some others as well, I get errors.

What do you get instead?

Different errors depending on the order of magnitude of the input:

$result = $formatter->asShortWeight(5);

yii\base\InvalidConfigException
Current ICU data version does not contain information about unit type "mass" and unit measure "kilogram". Check system requirements.

$result = $formatter->asShortWeight(5 / 1000);

yii\base\InvalidConfigException
msgfmt_create: message formatter creation failed: U_DEFAULT_KEYWORD_MISSING

Surprisingly, this does work (it returns 5 tn. sh.):

$result = $formatter->asShortWeight(5000);

Additional info

Q A
Yii version 2.0.47
PHP version 8.2
Operating system macOS
ICU version 71.1
ICU Data version 71.1
ICU Unicode version 14.0

Not sure if this is an error with Yii or with my environment. I'm using PHP 8.2 installed through homebrew, which comes with the intl extension and an up-to-date version of the ICU data.

@samdark samdark added type:bug Bug status:to be verified Needs to be reproduced and validated. labels Jan 20, 2023
@lubosdz
Copy link
Contributor

lubosdz commented Jan 20, 2023

What happens if you try for de e.g.:

$val = round(5 / 1000, 2);
$result = $formatter->asShortWeight($val);

Still same error?

@MoritzLost
Copy link
Contributor Author

@lubosdz

$formatter = new Formatter([
    'locale' => 'de',
]);
$result = $formatter->asShortWeight(round(5 / 1000, 2));

This gives the same error I get with 5 as the parameter:

msgfmt_create: message formatter creation failed: U_DEFAULT_KEYWORD_MISSING

@lubosdz
Copy link
Contributor

lubosdz commented Jan 24, 2023

The reason here is that keys for gram are missing in ICU for german locale.

Then formatter returns empty message which results in improperly formatted message "{n, plural, }" - and throws the Exception msgfmt_create: message formatter creation failed: U_DEFAULT_KEYWORD_MISSING.

This might be ICU issue, but it's strange since DE is commonly used locale. Other less often used langs have those definitions, e.g. SK, CS, HU, .. But for example PL is also missing those keys.

@MoritzLost
Copy link
Contributor Author

MoritzLost commented Jan 26, 2023

@lubosdz Thanks for the information! Yes, I was also surprised to see this issue, felt unlikely that I'd be the first to stumble upon this. But if it's an issue with the ICU data, maybe it needs to be changed there? Or maybe Yii2 can provide a wrapper / fallback in this case?

@lubosdz
Copy link
Contributor

lubosdz commented Jan 26, 2023

@MoritzLost
First, I would try out AT locale, or some other locale which close enough to de and is complete. Changing/updating ICU contained data is out of Yii2 power, since it's a huge international multipurpose database compiled as an INTL extension. It's really surprising that those keys are missing. I would suggest filling in a bug. It's also very hard to replace INTL data e.g. with partially extracted data (polyfills), which would be very ineffective - slow, incomplete and huge data size. This used to be approach of Yii1. I really have no idea how to fix or prevent this issue .. Perhaps Yii2 developers may have some recommendation.

@bizley
Copy link
Member

bizley commented Jan 27, 2023

I would just wait for the ICU fix to be honest. This looks really weird and I would be surprised if it was not fixed quickly when reported.

@MoritzLost
Copy link
Contributor Author

@lubosdz Thanks for the suggestion, we're now using en as a fallback, the output is identical to de (at least for kg / g), but using at is a good idea.

Could one of you report this bug in the ICU repository? I don't understand the ICU data structure well enough to be able to provide accurate information on what exactly is missing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
None yet
Development

No branches or pull requests

4 participants