Merge pull request #30 from draven-archive/master

Fix gotext.GetD with unloaded domain
This commit is contained in:
Leonel Quinteros
2018-12-17 10:02:03 -03:00
committed by GitHub
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
globalConfig.RLock()
if _, ok := globalConfig.storage.Domains[dom]; !ok {
globalConfig.storage.AddDomain(dom)
}
tr := globalConfig.storage.GetND(dom, str, plural, n, vars...)
globalConfig.RUnlock()

View File

@@ -80,6 +80,22 @@ msgid_plural "Several untranslated"
msgstr[0] ""
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
@@ -102,8 +118,21 @@ msgstr[1] ""
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
f.Close()
af.Close()
// Set package configuration
Configure("/tmp", "en_US", "default")
@@ -142,6 +171,11 @@ msgstr[1] ""
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)
}
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) {