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

Localized sorting #39873

Merged
merged 2 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,16 +1235,16 @@ void debug()
if( elem == vproto_id( "custom" ) ) {
continue;
}
veh_strings.emplace_back( elem->name, elem );
veh_strings.emplace_back( _( elem->name ), elem );
}
std::sort( veh_strings.begin(), veh_strings.end() );
std::sort( veh_strings.begin(), veh_strings.end(), localized_compare );
uilist veh_menu;
veh_menu.text = _( "Choose vehicle to spawn" );
int menu_ind = 0;
for( auto &elem : veh_strings ) {
//~ Menu entry in vehicle wish menu: 1st string: displayed name, 2nd string: internal name of vehicle
veh_menu.addentry( menu_ind, true, MENU_AUTOASSIGN, _( "%1$s (%2$s)" ),
_( elem.first ), elem.second.c_str() );
elem.first, elem.second.c_str() );
++menu_ind;
}
veh_menu.query();
Expand Down
5 changes: 5 additions & 0 deletions src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,8 @@ std::string operator+( const translation &lhs, const translation &rhs )
{
return lhs.translated() + rhs.translated();
}

bool localized_comparator::operator()( const std::string &l, const std::string &r ) const
{
return std::locale()( l, r );
}
29 changes: 29 additions & 0 deletions src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,33 @@ std::string operator+( const translation &lhs, const std::string &rhs );
std::string operator+( const std::string &lhs, const translation &rhs );
std::string operator+( const translation &lhs, const translation &rhs );

// Localized comparison operator, intended for sorting strings when they should
// be sorted according to the user's locale.
//
// For convenience, it also sorts pairs recursively, because a common
// requirement is to sort some list of objects by their names, and this can be
// achieved by sorting a list of pairs where the first element of the pair is
// the translated name.
struct localized_comparator {
template<typename T, typename U>
bool operator()( const std::pair<T, U> &l, const std::pair<T, U> &r ) const {
if( ( *this )( l.first, r.first ) ) {
return true;
}
if( ( *this )( r.first, l.first ) ) {
return false;
}
return ( *this )( l.second, r.second );
}

template<typename T>
bool operator()( const T &l, const T &r ) const {
return l < r;
}

bool operator()( const std::string &, const std::string & ) const;
};

constexpr localized_comparator localized_compare;

#endif // CATA_SRC_TRANSLATIONS_H