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.

79 lines
1.7KB

  1. /*
  2. * Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
  3. * Licensed under the MIT License. See LICENSE file in the project root for full license information.
  4. */
  5. package gotext
  6. // Translation is the struct for the Translations parsed via Po or Mo files and all coming parsers
  7. type Translation struct {
  8. ID string
  9. PluralID string
  10. Trs map[int]string
  11. }
  12. // NewTranslation returns the Translation object and initialized it.
  13. func NewTranslation() *Translation {
  14. tr := new(Translation)
  15. tr.Trs = make(map[int]string)
  16. return tr
  17. }
  18. // Get returns the string of the translation
  19. func (t *Translation) Get() string {
  20. // Look for Translation index 0
  21. if _, ok := t.Trs[0]; ok {
  22. if t.Trs[0] != "" {
  23. return t.Trs[0]
  24. }
  25. }
  26. // Return untranslated id by default
  27. return t.ID
  28. }
  29. // GetN returns the string of the plural translation
  30. func (t *Translation) GetN(n int) string {
  31. // Look for Translation index
  32. if _, ok := t.Trs[n]; ok {
  33. if t.Trs[n] != "" {
  34. return t.Trs[n]
  35. }
  36. }
  37. // Return untranslated singular if corresponding
  38. if n == 0 {
  39. return t.ID
  40. }
  41. // Return untranslated plural by default
  42. return t.PluralID
  43. }
  44. // GetE returns the string of the translation. The second return value is true
  45. // iff the string was found.
  46. func (t *Translation) GetE() (string, bool) {
  47. // Look for Translation index 0
  48. if _, ok := t.Trs[0]; ok {
  49. if t.Trs[0] != "" {
  50. return t.Trs[0], true
  51. }
  52. }
  53. return "", false
  54. }
  55. // GetNE returns the string of the plural translation. The second return value
  56. // is true iff the string was found.
  57. func (t *Translation) GetNE(n int) (string, bool) {
  58. // Look for Translation index
  59. if _, ok := t.Trs[n]; ok {
  60. if t.Trs[n] != "" {
  61. return t.Trs[n], true
  62. }
  63. }
  64. return "", false
  65. }