domain_helper.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "fmt"
  7. "strings"
  8. )
  9. func (p *domainManager) bindDomainTranslators(domain, path string, data []byte) {
  10. if _, ok := p.domainMap[domain]; ok {
  11. p.deleteDomain(domain) // delete old domain
  12. }
  13. fs := newFileSystem(path, data)
  14. for locale, _ := range fs.LocaleMap {
  15. trMapKey := p.makeTrMapKey(domain, locale)
  16. if data, err := fs.LoadMessagesFile(domain, locale, ".mo"); err == nil {
  17. p.trTextMap[trMapKey], _ = newMoTranslator(
  18. fmt.Sprintf("%s_%s.mo", domain, locale),
  19. data,
  20. )
  21. continue
  22. }
  23. if data, err := fs.LoadMessagesFile(domain, locale, ".po"); err == nil {
  24. p.trTextMap[trMapKey], _ = newPoTranslator(
  25. fmt.Sprintf("%s_%s.po", domain, locale),
  26. data,
  27. )
  28. continue
  29. }
  30. p.trTextMap[p.makeTrMapKey(domain, locale)] = nilTranslator
  31. }
  32. p.domainMap[domain] = fs
  33. }
  34. func (p *domainManager) deleteDomain(domain string) {
  35. if _, ok := p.domainMap[domain]; !ok {
  36. return
  37. }
  38. // delete all mo files
  39. trMapKeyPrefix := p.makeTrMapKey(domain, "")
  40. for k, _ := range p.trTextMap {
  41. if strings.HasPrefix(k, trMapKeyPrefix) {
  42. delete(p.trTextMap, k)
  43. }
  44. }
  45. delete(p.domainMap, domain)
  46. }