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.

53 lines
997B

  1. package gotext
  2. import (
  3. "os"
  4. "path"
  5. "testing"
  6. )
  7. func TestPo(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. // Write PO content to file
  19. filename := path.Clean(os.TempDir() + string(os.PathSeparator) + "default.po")
  20. f, err := os.Create(filename)
  21. if err != nil {
  22. t.Fatalf("Can't create test file: %s", err.Error())
  23. }
  24. defer f.Close()
  25. _, err = f.WriteString(str)
  26. if err != nil {
  27. t.Fatalf("Can't write to test file: %s", err.Error())
  28. }
  29. // Parse po file
  30. po := new(Po)
  31. po.ParseFile(filename)
  32. // Test translations
  33. tr := po.Get("My text")
  34. if tr != "Translated text" {
  35. t.Errorf("Expected 'Translated text' but got '%s'", tr)
  36. }
  37. v := "Variable"
  38. tr = po.Get("One with var: %s", v)
  39. if tr != "This one sets the var: Variable" {
  40. t.Errorf("Expected 'This one sets the var: Variable' but got '%s'", tr)
  41. }
  42. }