language.go 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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
  5. import "fmt"
  6. type Language struct {
  7. language string
  8. translations Translation
  9. }
  10. func (l *Language) Get(key string, args ...interface{}) string {
  11. var translation string
  12. str, found := l.translations[key]
  13. if !found {
  14. translation = key
  15. } else {
  16. translation = str.(string)
  17. }
  18. return fmt.Sprintf(translation, args...)
  19. }
  20. func (l *Language) Plural(key string, n int, args ...interface{}) string {
  21. translation := key
  22. slices, found := l.translations[key]
  23. if found {
  24. pluralForm, found := pluralForms[l.language]
  25. if !found {
  26. pluralForm = pluralForms["default"]
  27. }
  28. index := pluralForm(n)
  29. translations := slices.([]interface{})
  30. translation = key
  31. if len(translations) > index {
  32. translation = translations[index].(string)
  33. }
  34. }
  35. return fmt.Sprintf(translation, args...)
  36. }