Detect system locale and estimate best default locale setting
This commit is contained in:
@@ -3,7 +3,11 @@ package core
|
||||
import (
|
||||
"flag"
|
||||
|
||||
locale "github.com/Xuanwo/go-locale"
|
||||
"golang.org/x/exp/slices"
|
||||
|
||||
"github.com/safing/portbase/config"
|
||||
"github.com/safing/portbase/log"
|
||||
)
|
||||
|
||||
// Configuration Keys.
|
||||
@@ -14,7 +18,7 @@ var (
|
||||
CfgNetworkServiceKey = "core/networkService"
|
||||
defaultNetworkServiceMode bool
|
||||
|
||||
CfgLocaleKey = "core/localeID"
|
||||
CfgLocaleKey = "core/locale"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -44,21 +48,21 @@ func registerConfig() error {
|
||||
}
|
||||
|
||||
if err := config.Register(&config.Option{
|
||||
Name: "Locale",
|
||||
Name: "Time and Date Format",
|
||||
Key: CfgLocaleKey,
|
||||
Description: "Configures the locale for the user interface. This mainly affects rendering of dates, currency and numbers. Note that the Portmaster does not yet support different languages.",
|
||||
Description: "Configures the time and date format for the user interface. Selection is an example and correct formatting in the UI is a continual work in progress.",
|
||||
OptType: config.OptTypeString,
|
||||
ExpertiseLevel: config.ExpertiseLevelUser,
|
||||
ReleaseLevel: config.ReleaseLevelStable,
|
||||
DefaultValue: "en-GB",
|
||||
DefaultValue: getDefaultLocale(),
|
||||
PossibleValues: []config.PossibleValue{
|
||||
{
|
||||
Name: "en-GB",
|
||||
Value: "en-GB",
|
||||
Name: "24h DD-MM-YYYY",
|
||||
Value: enGBLocale,
|
||||
},
|
||||
{
|
||||
Name: "en-US",
|
||||
Value: "en-US",
|
||||
Name: "12h MM/DD/YYYY",
|
||||
Value: enUSLocale,
|
||||
},
|
||||
},
|
||||
Annotations: config.Annotations{
|
||||
@@ -72,3 +76,37 @@ func registerConfig() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getDefaultLocale() string {
|
||||
// Get locales from system.
|
||||
detectedLocales, err := locale.DetectAll()
|
||||
if err != nil {
|
||||
log.Warningf("core: failed to detect locale: %s", err)
|
||||
return enGBLocale
|
||||
}
|
||||
|
||||
// log.Debugf("core: detected locales: %s", detectedLocales)
|
||||
|
||||
// Check if there is a locale that corresponds to the en-US locale.
|
||||
for _, detectedLocale := range detectedLocales {
|
||||
if slices.Contains[[]string, string](defaultEnUSLocales, detectedLocale.String()) {
|
||||
return enUSLocale
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, return the en-GB locale as default.
|
||||
return enGBLocale
|
||||
}
|
||||
|
||||
var (
|
||||
enGBLocale = "en-GB"
|
||||
enUSLocale = "en-US"
|
||||
|
||||
defaultEnUSLocales = []string{
|
||||
"en-AS", // English (American Samoa)
|
||||
"en-GU", // English (Guam)
|
||||
"en-UM", // English (U.S. Minor Outlying Islands)
|
||||
"en-US", // English (United States)
|
||||
"en-VI", // English (U.S. Virgin Islands)
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user