Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

languages.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "strings"
  6. "sync"
  7. )
  8. // LanguageManager manages our languages and provides translation abilities.
  9. type LanguageManager struct {
  10. sync.RWMutex
  11. Info map[string]LangData
  12. translations map[string]map[string]string
  13. defaultLang string
  14. }
  15. // NewLanguageManager returns a new LanguageManager.
  16. func NewLanguageManager(defaultLang string, languageData map[string]LangData) *LanguageManager {
  17. lm := LanguageManager{
  18. Info: make(map[string]LangData),
  19. translations: make(map[string]map[string]string),
  20. defaultLang: defaultLang,
  21. }
  22. // make fake "en" info
  23. lm.Info["en"] = LangData{
  24. Code: "en",
  25. Name: "English",
  26. Maintainers: "Oragono contributors and the IRC community",
  27. }
  28. // load language data
  29. for name, data := range languageData {
  30. lm.Info[name] = data
  31. // make sure we don't include empty translations
  32. lm.translations[name] = make(map[string]string)
  33. for key, value := range data.Translations {
  34. if strings.TrimSpace(value) == "" {
  35. continue
  36. }
  37. lm.translations[name][key] = value
  38. }
  39. }
  40. return &lm
  41. }
  42. // Default returns the default languages.
  43. func (lm *LanguageManager) Default() []string {
  44. lm.RLock()
  45. defer lm.RUnlock()
  46. if lm.defaultLang == "" {
  47. return []string{}
  48. }
  49. return []string{lm.defaultLang}
  50. }
  51. // Count returns how many languages we have.
  52. func (lm *LanguageManager) Count() int {
  53. lm.RLock()
  54. defer lm.RUnlock()
  55. return len(lm.Info)
  56. }
  57. // Codes returns the proper language codes for the given casefolded language codes.
  58. func (lm *LanguageManager) Codes(codes []string) []string {
  59. lm.RLock()
  60. defer lm.RUnlock()
  61. var newCodes []string
  62. for _, code := range codes {
  63. info, exists := lm.Info[code]
  64. if exists {
  65. newCodes = append(newCodes, info.Code)
  66. }
  67. }
  68. if len(newCodes) == 0 {
  69. newCodes = []string{"en"}
  70. }
  71. return newCodes
  72. }
  73. // Translate returns the given string, translated into the given language.
  74. func (lm *LanguageManager) Translate(languages []string, originalString string) string {
  75. // not using any special languages
  76. if len(languages) == 0 || languages[0] == "en" || len(lm.translations) == 0 {
  77. return originalString
  78. }
  79. lm.RLock()
  80. defer lm.RUnlock()
  81. for _, lang := range languages {
  82. lang = strings.ToLower(lang)
  83. if lang == "en" {
  84. return originalString
  85. }
  86. translations, exists := lm.translations[lang]
  87. if !exists {
  88. continue
  89. }
  90. newString, exists := translations[originalString]
  91. if !exists {
  92. continue
  93. }
  94. // found a valid translation!
  95. return newString
  96. }
  97. // didn't find any translation
  98. return originalString
  99. }