NormalizeLocale returns the canonical locale code or an empty string.
(locale string)
| 209 | |
| 210 | // NormalizeLocale returns the canonical locale code or an empty string. |
| 211 | func (n Normalizer) NormalizeLocale(locale string) string { |
| 212 | candidate := strings.TrimSpace(strings.ReplaceAll(locale, "_", "-")) |
| 213 | if candidate == "" { |
| 214 | return "" |
| 215 | } |
| 216 | |
| 217 | aliases := make(map[string]string, len(n.locales)*4) |
| 218 | for _, code := range n.sortedLocaleCodes() { |
| 219 | name := n.locales[code] |
| 220 | normalizedCode := strings.ToLower(code) |
| 221 | aliases[normalizedCode] = code |
| 222 | aliases[strings.ReplaceAll(normalizedCode, "-", "")] = code |
| 223 | |
| 224 | short := strings.ToLower(strings.SplitN(code, "-", 2)[0]) |
| 225 | if _, ok := aliases[short]; !ok { |
| 226 | aliases[short] = code |
| 227 | } |
| 228 | |
| 229 | normalizedName := strings.ToLower(strings.TrimSpace(name)) |
| 230 | if normalizedName != "" { |
| 231 | aliases[normalizedName] = code |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return aliases[strings.ToLower(candidate)] |
| 236 | } |
| 237 | |
| 238 | func (n Normalizer) sortedLocaleCodes() []string { |
| 239 | codes := make([]string, 0, len(n.locales)) |
no test coverage detected