|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +//go:build !windows && !linux && !darwin |
| 5 | + |
| 6 | +package sqlcmd |
| 7 | + |
| 8 | +import ( |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "golang.org/x/text/language" |
| 13 | +) |
| 14 | + |
| 15 | +// detectUserLocale returns the user's locale from environment variables. |
| 16 | +// This is a fallback implementation for platforms other than Windows, Linux, and Darwin. |
| 17 | +// It uses the same environment variable approach as Linux. |
| 18 | +func detectUserLocale() language.Tag { |
| 19 | + // Check standard locale environment variables in order of precedence |
| 20 | + for _, envVar := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} { |
| 21 | + if locale := os.Getenv(envVar); locale != "" { |
| 22 | + tag := parseUnixLocale(locale) |
| 23 | + if tag != language.Und { |
| 24 | + return tag |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + return language.English |
| 29 | +} |
| 30 | + |
| 31 | +// parseUnixLocale converts a Unix locale string to a language.Tag |
| 32 | +// Examples: "en_US.UTF-8", "de_DE", "fr_FR.utf8", "C", "POSIX" |
| 33 | +func parseUnixLocale(locale string) language.Tag { |
| 34 | + // Handle special cases |
| 35 | + if locale == "C" || locale == "POSIX" || locale == "" { |
| 36 | + return language.English |
| 37 | + } |
| 38 | + |
| 39 | + // Remove encoding suffix (e.g., ".UTF-8") |
| 40 | + if idx := strings.Index(locale, "."); idx != -1 { |
| 41 | + locale = locale[:idx] |
| 42 | + } |
| 43 | + |
| 44 | + // Remove modifier (e.g., "@euro") |
| 45 | + if idx := strings.Index(locale, "@"); idx != -1 { |
| 46 | + locale = locale[:idx] |
| 47 | + } |
| 48 | + |
| 49 | + // Convert underscore to hyphen for BCP 47 format |
| 50 | + locale = strings.Replace(locale, "_", "-", -1) |
| 51 | + |
| 52 | + if tag, err := language.Parse(locale); err == nil { |
| 53 | + return tag |
| 54 | + } |
| 55 | + |
| 56 | + // Try with just the language part |
| 57 | + if idx := strings.Index(locale, "-"); idx != -1 { |
| 58 | + if tag, err := language.Parse(locale[:idx]); err == nil { |
| 59 | + return tag |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return language.Und |
| 64 | +} |
0 commit comments