File: domain_helper.go

package info (click to toggle)
golang-github-chai2010-gettext-go 0.0~git20191225.6b9f4b1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,256 kB
  • sloc: makefile: 23
file content (50 lines) | stat: -rw-r--r-- 1,325 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gettext

import (
	"fmt"
	"strings"
)

func (p *domainManager) bindDomainTranslators(domain, path string, data []byte) {
	if _, ok := p.domainMap[domain]; ok {
		p.deleteDomain(domain) // delete old domain
	}
	fs := newFileSystem(path, data)
	for locale, _ := range fs.LocaleMap {
		trMapKey := p.makeTrMapKey(domain, locale)
		if data, err := fs.LoadMessagesFile(domain, locale, ".mo"); err == nil {
			p.trTextMap[trMapKey], _ = newMoTranslator(
				fmt.Sprintf("%s_%s.mo", domain, locale),
				data,
			)
			continue
		}
		if data, err := fs.LoadMessagesFile(domain, locale, ".po"); err == nil {
			p.trTextMap[trMapKey], _ = newPoTranslator(
				fmt.Sprintf("%s_%s.po", domain, locale),
				data,
			)
			continue
		}
		p.trTextMap[p.makeTrMapKey(domain, locale)] = nilTranslator
	}
	p.domainMap[domain] = fs
}

func (p *domainManager) deleteDomain(domain string) {
	if _, ok := p.domainMap[domain]; !ok {
		return
	}
	// delete all mo files
	trMapKeyPrefix := p.makeTrMapKey(domain, "")
	for k, _ := range p.trTextMap {
		if strings.HasPrefix(k, trMapKeyPrefix) {
			delete(p.trTextMap, k)
		}
	}
	delete(p.domainMap, domain)
}