Add option to sort base on certain locale. (#1818)
* Add support for localized sorting * Switch go-locale module; Use collate option for natural sorting * Doc and error message improvement * Simplify predicated checking of localized sorting. * Improve error handling of `set locale` command
This commit is contained in:
8 files changed
+189
-19
No files matched your search
@@ -11,7 +11,15 @@ import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/Xuanwo/go-locale"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"golang.org/x/text/collate"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
const (
|
||||
localeStrDisable = "" // disable locale ordering for this locale value
|
||||
localeStrSys = "*" // replace this locale value with locale value read from environment
|
||||
)
|
||||
|
||||
func isRoot(name string) bool { return filepath.Dir(name) == name }
|
||||
@@ -338,6 +346,40 @@ func max(a, b int) int {
|
||||
return b
|
||||
}
|
||||
|
||||
// This function parses given locale string into language tag value. Passing empty
|
||||
// string as locale means reading locale value from environment.
|
||||
func getLocaleTag(localeStr string) (language.Tag, error) {
|
||||
if localeStr == localeStrSys {
|
||||
// read environment locale
|
||||
return locale.Detect()
|
||||
}
|
||||
|
||||
localeTag, err := language.Parse(localeStr)
|
||||
if err != nil {
|
||||
return localeTag, fmt.Errorf("invalid locale %q: %s", localeStr, err)
|
||||
}
|
||||
|
||||
return localeTag, nil
|
||||
}
|
||||
|
||||
// This function creates new collator for given locale. Passing empty string as
|
||||
// as locale means reading locale value from environment.
|
||||
//
|
||||
// *Note*: this function returns error when given `localeStr` has value `localeStrDisable`
|
||||
// or is an invalid locale tag.
|
||||
func makeCollator(localeStr string, opts ...collate.Option) (*collate.Collator, error) {
|
||||
if localeStr == localeStrDisable {
|
||||
return nil, fmt.Errorf("locale is disabled")
|
||||
}
|
||||
|
||||
localeTag, err := getLocaleTag(localeStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return collate.New(localeTag, opts...), nil
|
||||
}
|
||||
|
||||
// We don't need no generic code
|
||||
// We don't need no type control
|
||||
// No dark templates in compiler
|
||||
|
||||
Reference in new issue
Block a user