Files
photoprism/pkg/clean/locale.go
2025-06-25 00:03:41 +02:00

46 lines
1.4 KiB
Go

package clean
import "strings"
// Locale returns the normalized locale string in POSIX format with underscore, or the default locale otherwise.
// See https://en.wikipedia.org/wiki/Locale_(computer_software) for details.
func Locale(locale, defaultLocale string) string {
if locale == "" {
return defaultLocale
}
locale, _, _ = strings.Cut(strings.Replace(locale, "-", "_", 1), ".")
if l := len(locale); l == 2 {
return strings.ToLower(locale)
} else if l == 5 && locale[2] == '_' {
return strings.ToLower(locale[:2]) + "_" + strings.ToUpper(locale[3:])
}
return defaultLocale
}
// PosixLocale returns the normalized locale string in POSIX format with underscore, or the default locale otherwise.
// See https://en.wikipedia.org/wiki/Locale_(computer_software) for details.
func PosixLocale(locale, defaultLocale string) string {
return Locale(locale, defaultLocale)
}
// WebLocale returns a normalized locale string in BCP 47 format with a dash, or the default locale otherwise.
// See https://en.wikipedia.org/wiki/Locale_(computer_software) for details.
func WebLocale(locale, defaultLocale string) string {
if locale == "" {
return defaultLocale
}
locale, _, _ = strings.Cut(strings.Replace(locale, "_", "-", 1), ".")
if l := len(locale); l == 2 {
return strings.ToLower(locale)
} else if l == 5 && locale[2] == '-' {
return strings.ToLower(locale[:2]) + "-" + strings.ToUpper(locale[3:])
}
return defaultLocale
}