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.

66 lines
2.2KB

  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. import "net/textproto"
  7. // Translator interface is used by Locale and Po objects.Translator
  8. // It contains all methods needed to parse translation sources and obtain corresponding translations.
  9. // Also implements gob.GobEncoder/gob.DobDecoder interfaces to allow serialization of Locale objects.
  10. type Translator interface {
  11. ParseFile(f string)
  12. Parse(buf []byte)
  13. Get(str string, vars ...interface{}) string
  14. GetN(str, plural string, n int, vars ...interface{}) string
  15. GetC(str, ctx string, vars ...interface{}) string
  16. GetNC(str, plural string, n int, ctx string, vars ...interface{}) string
  17. GetE(str string, vars ...interface{}) (string, bool)
  18. GetNE(str, plural string, n int, vars ...interface{}) (string, bool)
  19. GetCE(str, ctx string, vars ...interface{}) (string, bool)
  20. GetNCE(str, plural string, n int, ctx string, vars ...interface{}) (string, bool)
  21. MarshalBinary() ([]byte, error)
  22. UnmarshalBinary([]byte) error
  23. }
  24. // TranslatorEncoding is used as intermediary storage to encode Translator objects to Gob.
  25. type TranslatorEncoding struct {
  26. // Headers storage
  27. Headers textproto.MIMEHeader
  28. // Language header
  29. Language string
  30. // Plural-Forms header
  31. PluralForms string
  32. // Parsed Plural-Forms header values
  33. Nplurals int
  34. Plural string
  35. // Storage
  36. Translations map[string]*Translation
  37. Contexts map[string]map[string]*Translation
  38. }
  39. // GetTranslator is used to recover a Translator object after unmarshaling the TranslatorEncoding object.
  40. // Internally uses a Po object as it should be switcheable with Mo objects without problem.
  41. // External Translator implementations should be able to serialize into a TranslatorEncoding object in order to unserialize into a Po-compatible object.
  42. func (te *TranslatorEncoding) GetTranslator() Translator {
  43. po := new(Po)
  44. po.Headers = te.Headers
  45. po.Language = te.Language
  46. po.PluralForms = te.PluralForms
  47. po.nplurals = te.Nplurals
  48. po.plural = te.Plural
  49. po.translations = te.Translations
  50. po.contexts = te.Contexts
  51. return po
  52. }