language.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package locale // import "miniflux.app/locale"
  5. import "fmt"
  6. // Language represents a language in the system.
  7. type Language struct {
  8. language string
  9. translations catalogMessages
  10. }
  11. // Get fetch the translation for the given key.
  12. func (l *Language) Get(key string, args ...interface{}) string {
  13. var translation string
  14. str, found := l.translations[key]
  15. if !found {
  16. translation = key
  17. } else {
  18. translation = str.(string)
  19. }
  20. return fmt.Sprintf(translation, args...)
  21. }
  22. // Plural returns the translation of the given key by using the language plural form.
  23. func (l *Language) Plural(key string, n int, args ...interface{}) string {
  24. translation := key
  25. slices, found := l.translations[key]
  26. if found {
  27. pluralForm, found := pluralForms[l.language]
  28. if !found {
  29. pluralForm = pluralForms["default"]
  30. }
  31. index := pluralForm(n)
  32. translations := slices.([]interface{})
  33. translation = key
  34. if len(translations) > index {
  35. translation = translations[index].(string)
  36. }
  37. }
  38. return fmt.Sprintf(translation, args...)
  39. }