You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.3KB

  1. package gotext
  2. import (
  3. "os"
  4. "path"
  5. "testing"
  6. )
  7. func TestLocale(t *testing.T) {
  8. // Set PO content
  9. str := `# Some comment
  10. msgid "My text"
  11. msgstr "Translated text"
  12. # More comments
  13. msgid "Another string"
  14. msgstr ""
  15. msgid "One with var: %s"
  16. msgstr "This one sets the var: %s"
  17. `
  18. // Create Locales directory with simplified language code
  19. dirname := path.Clean("/tmp" + string(os.PathSeparator) + "en")
  20. err := os.MkdirAll(dirname, os.ModePerm)
  21. if err != nil {
  22. t.Fatalf("Can't create test directory: %s", err.Error())
  23. }
  24. // Write PO content to file
  25. filename := path.Clean(dirname + string(os.PathSeparator) + "my_domain.po")
  26. f, err := os.Create(filename)
  27. if err != nil {
  28. t.Fatalf("Can't create test file: %s", err.Error())
  29. }
  30. defer f.Close()
  31. _, err = f.WriteString(str)
  32. if err != nil {
  33. t.Fatalf("Can't write to test file: %s", err.Error())
  34. }
  35. // Create Locale with full language code
  36. l := NewLocale("/tmp", "en_US")
  37. // Add domain
  38. l.AddDomain("my_domain")
  39. // Test translations
  40. tr := l.GetD("my_domain", "My text")
  41. if tr != "Translated text" {
  42. t.Errorf("Expected 'Translated text' but got '%s'", tr)
  43. }
  44. v := "Variable"
  45. tr = l.GetD("my_domain", "One with var: %s", v)
  46. if tr != "This one sets the var: Variable" {
  47. t.Errorf("Expected 'This one sets the var: Variable' but got '%s'", tr)
  48. }
  49. }