Skip to content

Commit

Permalink
Merge pull request #39873 from jbytheway/localized_sorting
Browse files Browse the repository at this point in the history
Localized sorting
  • Loading branch information
kevingranade authored Apr 25, 2020
2 parents b797ed4 + bdd6e24 commit 4a304e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
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

0 comments on commit 4a304e3

Please sign in to comment.