Fix gotext.GetD with unloaded domain

This commit is contained in:
draveness
2018-12-07 12:32:39 +08:00
parent 7b73c0d36b
commit 37bac2fe57
2 changed files with 39 additions and 0 deletions

View File

@@ -182,6 +182,11 @@ func GetND(dom, str, plural string, n int, vars ...interface{}) string {
// Return Translation // Return Translation
globalConfig.RLock() globalConfig.RLock()
if _, ok := globalConfig.storage.Domains[dom]; !ok {
globalConfig.storage.AddDomain(dom)
}
tr := globalConfig.storage.GetND(dom, str, plural, n, vars...) tr := globalConfig.storage.GetND(dom, str, plural, n, vars...)
globalConfig.RUnlock() globalConfig.RUnlock()

View File

@@ -82,6 +82,22 @@ msgstr[1] ""
` `
anotherStr := `
msgid ""
msgstr "Project-Id-Version: %s\n"
"Report-Msgid-Bugs-To: %s\n"
# Initial comment
# More Headers below
"Language: en\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "Another text on a different domain"
msgstr "Another text on another domain"
`
// Create Locales directory on default location // Create Locales directory on default location
dirname := path.Join("/tmp", "en_US") dirname := path.Join("/tmp", "en_US")
err := os.MkdirAll(dirname, os.ModePerm) err := os.MkdirAll(dirname, os.ModePerm)
@@ -102,8 +118,21 @@ msgstr[1] ""
t.Fatalf("Can't write to test file: %s", err.Error()) t.Fatalf("Can't write to test file: %s", err.Error())
} }
anotherFilename := path.Join(dirname, "another.po")
af, err := os.Create(anotherFilename)
if err != nil {
t.Fatalf("Can't create test file: %s", err.Error())
}
_, err = af.WriteString(anotherStr)
if err != nil {
t.Fatalf("Can't write to test file: %s", err.Error())
}
// Move file close to write the file, so we can use it in the next step // Move file close to write the file, so we can use it in the next step
f.Close() f.Close()
af.Close()
// Set package configuration // Set package configuration
Configure("/tmp", "en_US", "default") Configure("/tmp", "en_US", "default")
@@ -142,6 +171,11 @@ msgstr[1] ""
if tr != "This one is the plural in a Ctx context: Variable" { if tr != "This one is the plural in a Ctx context: Variable" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Variable' but got '%s'", tr) t.Errorf("Expected 'This one is the plural in a Ctx context: Variable' but got '%s'", tr)
} }
tr = GetD("another", "Another text on a different domain")
if tr != "Another text on another domain" {
t.Errorf("Expected 'Another text on another domain' but got '%s'", tr)
}
} }
func TestUntranslated(t *testing.T) { func TestUntranslated(t *testing.T) {