-
Notifications
You must be signed in to change notification settings - Fork 14
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
Type initialization for Culture fails if installed cultures contains invalid neutral ancestry #82
Comments
Thanks for reporting! And what a beautiful issue! |
See issue #82. No idea if this code makes any sense and maybe it will lead to other problems later.
I wrote a PR with some cargo culting. If you have time you can review if it makes any sense. Chances are it just pushes the problem to crop up later when running the app. Ideal case is if we could somehow repro and create an UI-test for it. That would be the only way to guard against future regressions. |
See issue #82. No idea if this code makes any sense and maybe it will lead to other problems later.
Thank you @JohanLarsson. The technique I took though was pretty quick and dirty (and slow).
This works (I think) much like your take simply by not creating the NeutralCultureRegionMap entry for "broken" CultureInfos. Since The only thing using that collection is TryGetRegion, which returns false like I think it should here, this is probably good behavior. But what do you think about making the TryGet and TryGetRegion methods lazy-loading? |
Which version do you prefer? Also about speed it is probably plenty fast enough as it is only run once at app startup. |
I think you are right to add try/catch. Otherwise they are essentially the same. I'd say stick with your commit. It has the benefit of being done. Actually, what I think you really get from late loading is testability. Set the AllCultures in the test method and I think you can test this issue. While playing around with the idea of lazy loading, I found what may be a redundancy in TryGet. Since TwoLetterISOLanguageNameCultureMap is created on a filtered list of AllCultures where Name == TwoLetterISOLanguageName, isn't TwoLetterISOLanguageNameCultureMap a subset of NameCultureMap? Tell me if I read that wrong... it's getting late... it's possible. So I ended up with:
|
I would expect the added startup time to be in the microseconds. We can run some benechmarks and optimize later if needed. Things can probably be made lazy there. |
I think you are right about that. |
Ah, nice, very valid point. We should refactor to that so that we can write an UI-test that uses the cultures your client had. That way we can be sure to not have regressions in the future. I don't remember the reason for all those maps to be honest, looks a bit like I had a stroke when reading the code. |
I have a start on it. But getting late. I can submit a PR on it tomorrow evening, assuming I can replicate the invalid culture setup. |
https://www.nuget.org/packages/Gu.Localization/6.4.3 Takes 15 minutes or so for nuget to index it. |
If you decide to try writing UI-tests for it I think it is best to make a small separate app for this scenario that we use in the tests. |
Thank you very much for publishing that so quickly! Honestly, my initial thought was to only write a test method (probably in a new test fixture) that created an array of CultureInfos containing a neutral culture without a specific culture. Then try calling one of the methods on Translator that would cause the static Culture constructor to be executed. That would test this specific scenario, and you already have quite the nice testing infrastructure. I suspect you have something in mind that I'm missing... I'd like to be a thorough as I can. |
Unit test only is fine. |
We have a gitter room if you feel like chatting: https://gitter.im/JohanLarsson/Gu.Localization |
Ran a quick benchmark for fun. Confirmed microseconds.
|
The Gu.Localization.Culture static initializer throws a KeyNotFoundException while setting the NeutralCultrureRegionMap if the computer has a neutral culture installed (or one incorrectly marked as neutral) without a corresponding specific culture.
I have not been able to determine the full steps to reproduce simply because I don't know how the clients computer got into this state. However I can describe the state and perhaps someone with more knowledge about configuring CultureInfo's on the system can share steps to create the state.
Essentially, the computer had two cultures marked as neutral "bal" and "bal-Arab". Perhaps "bal-Arab" should not be marked as neutral. When the static class Gu.Localization.Culture is initialized, it creates a NeutralCultureRegionMap by iterating the neutral cultures (in this case both "bal" and "bal-Arab") and creates "specific" cultures. So for the neutral "en" culture, it creates "en-US" as the specific culture, then it attempts to find that culture in Gu.Localization.Culture.NameCultureMap using the indexer. For the client computer, neither "bal" and "bal-Arab" neutral cultures have an associated specific culture.
This script
Produced this output (with some irrelevant info striped):
Parent : bal
LCID : 8192
Name : bal-Arab
IetfLanguageTag : bal-Arab
DisplayName : Unknown Language (bal-Arab)
ThreeLetterWindowsLanguageName : ZZZ
TwoLetterISOLanguageName : bal
IsNeutralCulture : True
CultureTypes : NeutralCultures, InstalledWin32Cultures <-- Note incorrect CultureType
UseUserOverride : True
IsReadOnly : False
Parent : bal-Arab
LCID : 12288
Name : bal-Arab-001
DisplayName : Unknown Locale (bal-Arab-001)
TwoLetterISOLanguageName : bal
ThreeLetterWindowsLanguageName : ZZZ
IsNeutralCulture : False
CultureTypes : SpecificCultures, InstalledWin32Cultures
UseUserOverride : True
IsReadOnly : False
Parent :
LCID : 13312
Name : bal
DisplayName : Unknown Language (bal)
TwoLetterISOLanguageName : bal
ThreeLetterWindowsLanguageName : ZZZ
IsNeutralCulture : True
CultureTypes : NeutralCultures, InstalledWin32Cultures
UseUserOverride : True
IsReadOnly : False
When Gu.Localization is used in any way, this error will be thrown:
The text was updated successfully, but these errors were encountered: