local.go 774 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gettext
  5. import (
  6. "os"
  7. "strings"
  8. )
  9. func getDefaultLocale() string {
  10. if v := os.Getenv("LC_MESSAGES"); v != "" {
  11. return simplifiedLocale(v)
  12. }
  13. if v := os.Getenv("LANG"); v != "" {
  14. return simplifiedLocale(v)
  15. }
  16. return "default"
  17. }
  18. func simplifiedLocale(lang string) string {
  19. // en_US/en_US.UTF-8/zh_CN/zh_TW/el_GR@euro/...
  20. if idx := strings.Index(lang, ":"); idx != -1 {
  21. lang = lang[:idx]
  22. }
  23. if idx := strings.Index(lang, "@"); idx != -1 {
  24. lang = lang[:idx]
  25. }
  26. if idx := strings.Index(lang, "."); idx != -1 {
  27. lang = lang[:idx]
  28. }
  29. return strings.TrimSpace(lang)
  30. }