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

Autoremove a locale only if its not the fallback for another enabled one #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bzeller
Copy link
Contributor

@bzeller bzeller commented Jun 25, 2018

This PR adds a extra check before automatically removing a locales fallback. It will only be removed if its not a fallback for another requested locale.

@bzeller bzeller requested a review from mlandres June 25, 2018 14:38
const auto &currLangs = localeIds.current();
for ( lang = lang.fallback(); lang && ! localeIds.current().count( IdString(lang) ); lang = lang.fallback() ) {
//remove the language only if its not a fallback for any other
if ( std::none_of(currLangs.begin(), currLangs.end(), [&lang](const IdString &loc){ return Locale(loc).fallback() == lang; }) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallbacks form a list de_DE->de->en. We don't want to remove lang if it's in the fallbackllist of a selected locale, not only if it's the direct fallback. IMO the code would remove en if currLangs is just de_DE.

I'd enhance the Locale class and add a member bool hasFallback( const Locale & fallback_r ) const; returning whether fallback_r is in the Locales fallback list (separate commit) and use this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@bzeller bzeller force-pushed the remove_locale_if_no_fallback branch from cffd83b to fb287ef Compare June 26, 2018 13:57
@bzeller bzeller force-pushed the remove_locale_if_no_fallback branch from fb287ef to 45a98e9 Compare June 26, 2018 13:59
const auto &currLangs = localeIds.current();
auto checkIsFallback = [&lang]( const IdString &currLocStr_r ){
Locale currLoc(currLocStr_r);
return ( currLoc && Locale(currLoc).hasFallback(lang) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doppeltgemoppelt. Locale currLoc(currLocStr_r); and Locale(currLoc).hasF...

Actually you don't need the extra currLoc variable. Even if currLangs had an empty string included, Locale(currLocStr_r) would be the empty Locale (Locale::noCode) and hasFallback would always be false.

return Locale(currLocStr_r).hasFallback(lang);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@bzeller bzeller force-pushed the remove_locale_if_no_fallback branch from 45a98e9 to 9e36ad9 Compare June 26, 2018 15:28
};
for ( lang = lang.fallback(); lang && ! localeIds.current().count( IdString(lang) ); lang = lang.fallback() ) {
//remove the language only if its not a fallback for any other
if ( std::none_of( currLangs.begin(), currLangs.end(), checkIsFallback ) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer sure if this is the right approach / whether checkIsFallback can actually happen.

The 1st of the 3 for-loops (L490) adds all already supported locales (in localesTracker.current but nor newly added) and their fallbacks to localeIds.current().

The 2nd loop (L499) adds all newly added locales including their fallbacks to localeIds.added(), unless a locale is already in localeIds.current() (in this case it's fallbacks are also already in current()).

IMO the original 3rd-loop is almost right. All removed locales and their fallbacks are remembered in localeIds.removed() unless a fallback is already in localeIds.current() . It just misses an additional check for && ! localeIds.added().count( IdString(lang) ). The fallback must not be removed it was newly added (removed de_DE but added de).

In contrast to localesTracker, localeIds.current() contains all fallbacks. So if a locale is not in current, it can't be fallback of a locale in current.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants