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.

511 lines
13KB

  1. package gotext
  2. import (
  3. "os"
  4. "path/filepath"
  5. "sync"
  6. "testing"
  7. )
  8. func TestGettersSetters(t *testing.T) {
  9. SetDomain("test")
  10. dom := GetDomain()
  11. if dom != "test" {
  12. t.Errorf("Expected GetDomain to return 'test', but got '%s'", dom)
  13. }
  14. SetLibrary("/tmp/test")
  15. lib := GetLibrary()
  16. if lib != "/tmp/test" {
  17. t.Errorf("Expected GetLibrary to return '/tmp/test', but got '%s'", lib)
  18. }
  19. SetLanguage("es")
  20. lang := GetLanguage()
  21. if lang != "es" {
  22. t.Errorf("Expected GetLanguage to return 'es', but got '%s'", lang)
  23. }
  24. }
  25. func TestPackageFunctions(t *testing.T) {
  26. // Set PO content
  27. str := `
  28. msgid ""
  29. msgstr "Project-Id-Version: %s\n"
  30. "Report-Msgid-Bugs-To: %s\n"
  31. # Initial comment
  32. # More Headers below
  33. "Language: en\n"
  34. "Content-Type: text/plain; charset=UTF-8\n"
  35. "Content-Transfer-Encoding: 8bit\n"
  36. "Plural-Forms: nplurals=2; plural=(n != 1);\n"
  37. # Some comment
  38. msgid "My text"
  39. msgstr "Translated text"
  40. # More comments
  41. msgid "Another string"
  42. msgstr ""
  43. msgid "One with var: %s"
  44. msgid_plural "Several with vars: %s"
  45. msgstr[0] "This one is the singular: %s"
  46. msgstr[1] "This one is the plural: %s"
  47. msgstr[2] "And this is the second plural form: %s"
  48. msgctxt "Ctx"
  49. msgid "One with var: %s"
  50. msgid_plural "Several with vars: %s"
  51. msgstr[0] "This one is the singular in a Ctx context: %s"
  52. msgstr[1] "This one is the plural in a Ctx context: %s"
  53. msgid "Some random"
  54. msgstr "Some random Translation"
  55. msgctxt "Ctx"
  56. msgid "Some random in a context"
  57. msgstr "Some random Translation in a context"
  58. msgid "More"
  59. msgstr "More Translation"
  60. msgid "Untranslated"
  61. msgid_plural "Several untranslated"
  62. msgstr[0] ""
  63. msgstr[1] ""
  64. `
  65. anotherStr := `
  66. msgid ""
  67. msgstr "Project-Id-Version: %s\n"
  68. "Report-Msgid-Bugs-To: %s\n"
  69. # Initial comment
  70. # More Headers below
  71. "Language: en\n"
  72. "Content-Type: text/plain; charset=UTF-8\n"
  73. "Content-Transfer-Encoding: 8bit\n"
  74. "Plural-Forms: nplurals=2; plural=(n != 1);\n"
  75. msgid "Another text on a different domain"
  76. msgstr "Another text on another domain"
  77. `
  78. // Create Locales directory on default location
  79. dirname := filepath.Join("/tmp", "en_US")
  80. err := os.MkdirAll(dirname, os.ModePerm)
  81. if err != nil {
  82. t.Fatalf("Can't create test directory: %s", err.Error())
  83. }
  84. // Write PO content to default domain file
  85. filename := filepath.Join(dirname, "default.po")
  86. f, err := os.Create(filename)
  87. if err != nil {
  88. t.Fatalf("Can't create test file: %s", err.Error())
  89. }
  90. _, err = f.WriteString(str)
  91. if err != nil {
  92. t.Fatalf("Can't write to test file: %s", err.Error())
  93. }
  94. anotherFilename := filepath.Join(dirname, "another.po")
  95. af, err := os.Create(anotherFilename)
  96. if err != nil {
  97. t.Fatalf("Can't create test file: %s", err.Error())
  98. }
  99. _, err = af.WriteString(anotherStr)
  100. if err != nil {
  101. t.Fatalf("Can't write to test file: %s", err.Error())
  102. }
  103. // Move file close to write the file, so we can use it in the next step
  104. f.Close()
  105. af.Close()
  106. // Set package configuration
  107. Configure("/tmp", "en_US", "default")
  108. // Test translations
  109. tr := Get("My text")
  110. if tr != "Translated text" {
  111. t.Errorf("Expected 'Translated text' but got '%s'", tr)
  112. }
  113. v := "Variable"
  114. tr = Get("One with var: %s", v)
  115. if tr != "This one is the singular: Variable" {
  116. t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
  117. }
  118. // Test plural
  119. tr = GetN("One with var: %s", "Several with vars: %s", 2, v)
  120. if tr != "This one is the plural: Variable" {
  121. t.Errorf("Expected 'This one is the plural: Variable' but got '%s'", tr)
  122. }
  123. // Test context translations
  124. tr = GetC("Some random in a context", "Ctx")
  125. if tr != "Some random Translation in a context" {
  126. t.Errorf("Expected 'Some random Translation in a context' but got '%s'", tr)
  127. }
  128. v = "Variable"
  129. tr = GetC("One with var: %s", "Ctx", v)
  130. if tr != "This one is the singular in a Ctx context: Variable" {
  131. t.Errorf("Expected 'This one is the singular in a Ctx context: Variable' but got '%s'", tr)
  132. }
  133. tr = GetNC("One with var: %s", "Several with vars: %s", 19, "Ctx", v)
  134. if tr != "This one is the plural in a Ctx context: Variable" {
  135. t.Errorf("Expected 'This one is the plural in a Ctx context: Variable' but got '%s'", tr)
  136. }
  137. tr = GetD("another", "Another text on a different domain")
  138. if tr != "Another text on another domain" {
  139. t.Errorf("Expected 'Another text on another domain' but got '%s'", tr)
  140. }
  141. }
  142. func TestUntranslated(t *testing.T) {
  143. // Set PO content
  144. str := `
  145. msgid ""
  146. msgstr ""
  147. # Initial comment
  148. # Headers below
  149. "Language: en\n"
  150. "Content-Type: text/plain; charset=UTF-8\n"
  151. "Content-Transfer-Encoding: 8bit\n"
  152. "Plural-Forms: nplurals=2; plural=(n != 1);\n"
  153. msgid "Untranslated"
  154. msgid_plural "Several untranslated"
  155. msgstr[0] ""
  156. msgstr[1] ""
  157. `
  158. // Create Locales directory on default location
  159. dirname := filepath.Join("/tmp", "en_US")
  160. err := os.MkdirAll(dirname, os.ModePerm)
  161. if err != nil {
  162. t.Fatalf("Can't create test directory: %s", err.Error())
  163. }
  164. // Write PO content to default domain file
  165. filename := filepath.Join(dirname, "default.po")
  166. f, err := os.Create(filename)
  167. if err != nil {
  168. t.Fatalf("Can't create test file: %s", err.Error())
  169. }
  170. defer f.Close()
  171. _, err = f.WriteString(str)
  172. if err != nil {
  173. t.Fatalf("Can't write to test file: %s", err.Error())
  174. }
  175. // Set package configuration
  176. Configure("/tmp", "en_US", "default")
  177. // Test untranslated
  178. tr := Get("Untranslated")
  179. if tr != "Untranslated" {
  180. t.Errorf("Expected 'Untranslated' but got '%s'", tr)
  181. }
  182. tr = GetN("Untranslated", "Several untranslated", 1)
  183. if tr != "Untranslated" {
  184. t.Errorf("Expected 'Untranslated' but got '%s'", tr)
  185. }
  186. tr = GetN("Untranslated", "Several untranslated", 2)
  187. if tr != "Several untranslated" {
  188. t.Errorf("Expected 'Several untranslated' but got '%s'", tr)
  189. }
  190. tr = GetD("default", "Untranslated")
  191. if tr != "Untranslated" {
  192. t.Errorf("Expected 'Untranslated' but got '%s'", tr)
  193. }
  194. tr = GetND("default", "Untranslated", "Several untranslated", 1)
  195. if tr != "Untranslated" {
  196. t.Errorf("Expected 'Untranslated' but got '%s'", tr)
  197. }
  198. tr = GetND("default", "Untranslated", "Several untranslated", 2)
  199. if tr != "Several untranslated" {
  200. t.Errorf("Expected 'Several untranslated' but got '%s'", tr)
  201. }
  202. }
  203. func TestMoAndPoTranslator(t *testing.T) {
  204. fixPath, _ := filepath.Abs("./fixtures/")
  205. Configure(fixPath, "en_GB", "default")
  206. // Check default domain Translation
  207. SetDomain("default")
  208. tr := Get("My text")
  209. if tr != "Translated text" {
  210. t.Errorf("Expected 'Translated text'. Got '%s'", tr)
  211. }
  212. tr = Get("language")
  213. if tr != "en_GB" {
  214. t.Errorf("Expected 'en_GB'. Got '%s'", tr)
  215. }
  216. // Change Language (locale)
  217. SetLanguage("en_AU")
  218. // Check default domain Translation
  219. SetDomain("default")
  220. tr = Get("My text")
  221. if tr != "Translated text" {
  222. t.Errorf("Expected 'Translated text'. Got '%s'", tr)
  223. }
  224. tr = Get("language")
  225. if tr != "en_AU" {
  226. t.Errorf("Expected 'en_AU'. Got '%s'", tr)
  227. }
  228. }
  229. func TestDomains(t *testing.T) {
  230. // Set PO content
  231. strDefault := `
  232. msgid ""
  233. msgstr "Plural-Forms: nplurals=2; plural=(n != 1);\n"
  234. msgid "Default text"
  235. msgid_plural "Default texts"
  236. msgstr[0] "Default Translation"
  237. msgstr[1] "Default translations"
  238. msgctxt "Ctx"
  239. msgid "Default context"
  240. msgid_plural "Default contexts"
  241. msgstr[0] "Default ctx Translation"
  242. msgstr[1] "Default ctx translations"
  243. `
  244. strCustom := `
  245. msgid ""
  246. msgstr "Plural-Forms: nplurals=2; plural=(n != 1);\n"
  247. msgid "Custom text"
  248. msgid_plural "Custom texts"
  249. msgstr[0] "Custom Translation"
  250. msgstr[1] "Custom translations"
  251. msgctxt "Ctx"
  252. msgid "Custom context"
  253. msgid_plural "Custom contexts"
  254. msgstr[0] "Custom ctx Translation"
  255. msgstr[1] "Custom ctx translations"
  256. `
  257. // Create Locales directory and files on temp location
  258. dirname := filepath.Join("/tmp", "en_US")
  259. err := os.MkdirAll(dirname, os.ModePerm)
  260. if err != nil {
  261. t.Fatalf("Can't create test directory: %s", err.Error())
  262. }
  263. fDefault, err := os.Create(filepath.Join(dirname, "default.po"))
  264. if err != nil {
  265. t.Fatalf("Can't create test file: %s", err.Error())
  266. }
  267. defer fDefault.Close()
  268. fCustom, err := os.Create(filepath.Join(dirname, "custom.po"))
  269. if err != nil {
  270. t.Fatalf("Can't create test file: %s", err.Error())
  271. }
  272. defer fCustom.Close()
  273. _, err = fDefault.WriteString(strDefault)
  274. if err != nil {
  275. t.Fatalf("Can't write to test file: %s", err.Error())
  276. }
  277. _, err = fCustom.WriteString(strCustom)
  278. if err != nil {
  279. t.Fatalf("Can't write to test file: %s", err.Error())
  280. }
  281. Configure("/tmp", "en_US", "default")
  282. // Check default domain Translation
  283. SetDomain("default")
  284. tr := Get("Default text")
  285. if tr != "Default Translation" {
  286. t.Errorf("Expected 'Default Translation'. Got '%s'", tr)
  287. }
  288. tr = GetN("Default text", "Default texts", 23)
  289. if tr != "Default translations" {
  290. t.Errorf("Expected 'Default translations'. Got '%s'", tr)
  291. }
  292. tr = GetC("Default context", "Ctx")
  293. if tr != "Default ctx Translation" {
  294. t.Errorf("Expected 'Default ctx Translation'. Got '%s'", tr)
  295. }
  296. tr = GetNC("Default context", "Default contexts", 23, "Ctx")
  297. if tr != "Default ctx translations" {
  298. t.Errorf("Expected 'Default ctx translations'. Got '%s'", tr)
  299. }
  300. SetDomain("custom")
  301. tr = Get("Custom text")
  302. if tr != "Custom Translation" {
  303. t.Errorf("Expected 'Custom Translation'. Got '%s'", tr)
  304. }
  305. tr = GetN("Custom text", "Custom texts", 23)
  306. if tr != "Custom translations" {
  307. t.Errorf("Expected 'Custom translations'. Got '%s'", tr)
  308. }
  309. tr = GetC("Custom context", "Ctx")
  310. if tr != "Custom ctx Translation" {
  311. t.Errorf("Expected 'Custom ctx Translation'. Got '%s'", tr)
  312. }
  313. tr = GetNC("Custom context", "Custom contexts", 23, "Ctx")
  314. if tr != "Custom ctx translations" {
  315. t.Errorf("Expected 'Custom ctx translations'. Got '%s'", tr)
  316. }
  317. }
  318. func TestPackageRace(t *testing.T) {
  319. // Set PO content
  320. str := `# Some comment
  321. msgid "My text"
  322. msgstr "Translated text"
  323. # More comments
  324. msgid "Another string"
  325. msgstr ""
  326. msgid "One with var: %s"
  327. msgid_plural "Several with vars: %s"
  328. msgstr[0] "This one is the singular: %s"
  329. msgstr[1] "This one is the plural: %s"
  330. msgstr[2] "And this is the second plural form: %s"
  331. msgctxt "Ctx"
  332. msgid "Some random in a context"
  333. msgstr "Some random Translation in a context"
  334. `
  335. // Create Locales directory on default location
  336. dirname := filepath.Join("/tmp", "en_US")
  337. err := os.MkdirAll(dirname, os.ModePerm)
  338. if err != nil {
  339. t.Fatalf("Can't create test directory: %s", err.Error())
  340. }
  341. // Write PO content to default domain file
  342. filename := filepath.Join("/tmp", GetDomain()+".po")
  343. f, err := os.Create(filename)
  344. if err != nil {
  345. t.Fatalf("Can't create test file: %s", err.Error())
  346. }
  347. defer f.Close()
  348. _, err = f.WriteString(str)
  349. if err != nil {
  350. t.Fatalf("Can't write to test file: %s", err.Error())
  351. }
  352. var wg sync.WaitGroup
  353. for i := 0; i < 1000; i++ {
  354. wg.Add(1)
  355. // Test translations
  356. go func() {
  357. defer wg.Done()
  358. GetLibrary()
  359. SetLibrary(filepath.Join("/tmp", "gotextlib"))
  360. GetDomain()
  361. SetDomain("default")
  362. GetLanguage()
  363. SetLanguage("en_US")
  364. Configure("/tmp", "en_US", "default")
  365. Get("My text")
  366. GetN("One with var: %s", "Several with vars: %s", 0, "test")
  367. GetC("Some random in a context", "Ctx")
  368. }()
  369. }
  370. wg.Wait()
  371. }
  372. func TestPackageArabicTranslation(t *testing.T) {
  373. Configure("fixtures/", "ar", "categories")
  374. // Plurals formula missing + Plural translation string missing
  375. tr := GetD("categories", "Alcohol & Tobacco")
  376. if tr != "الكحول والتبغ" {
  377. t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr)
  378. }
  379. // Plural translation string present without translations, should get the msgid_plural
  380. tr = GetND("categories", "%d selected", "%d selected", 10)
  381. if tr != "%d selected" {
  382. t.Errorf("Expected to get '%%d selected', but got '%s'", tr)
  383. }
  384. //Plurals formula present + Plural translation string present and complete
  385. tr = GetND("categories", "Load %d more document", "Load %d more documents", 0)
  386. if tr != "حمّل %d مستندات إضافيّة" {
  387. t.Errorf("Expected to get 'msgstr[0]', but got '%s'", tr)
  388. }
  389. tr = GetND("categories", "Load %d more document", "Load %d more documents", 1)
  390. if tr != "حمّل مستند واحد إضافي" {
  391. t.Errorf("Expected to get 'msgstr[1]', but got '%s'", tr)
  392. }
  393. tr = GetND("categories", "Load %d more document", "Load %d more documents", 2)
  394. if tr != "حمّل مستندين إضافيين" {
  395. t.Errorf("Expected to get 'msgstr[2]', but got '%s'", tr)
  396. }
  397. tr = GetND("categories", "Load %d more document", "Load %d more documents", 6)
  398. if tr != "حمّل %d مستندات إضافيّة" {
  399. t.Errorf("Expected to get 'msgstr[3]', but got '%s'", tr)
  400. }
  401. tr = GetND("categories", "Load %d more document", "Load %d more documents", 116)
  402. if tr != "حمّل %d مستندا إضافيّا" {
  403. t.Errorf("Expected to get 'msgstr[4]', but got '%s'", tr)
  404. }
  405. tr = GetND("categories", "Load %d more document", "Load %d more documents", 102)
  406. if tr != "حمّل %d مستند إضافي" {
  407. t.Errorf("Expected to get 'msgstr[5]', but got '%s'", tr)
  408. }
  409. }
  410. func TestPackageArabicMissingPluralForm(t *testing.T) {
  411. Configure("fixtures/", "ar", "no_plural_header")
  412. // Get translation
  413. tr := GetD("no_plural_header", "Alcohol & Tobacco")
  414. if tr != "الكحول والتبغ" {
  415. t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr)
  416. }
  417. }