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.

i18n.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package i18n
  4. import (
  5. "html/template"
  6. "io"
  7. )
  8. var DefaultLocales = NewLocaleStore()
  9. type Locale interface {
  10. // TrString translates a given key and arguments for a language
  11. TrString(trKey string, trArgs ...any) string
  12. // TrHTML translates a given key and arguments for a language, string arguments are escaped to HTML
  13. TrHTML(trKey string, trArgs ...any) template.HTML
  14. // HasKey reports if a locale has a translation for a given key
  15. HasKey(trKey string) bool
  16. }
  17. // LocaleStore provides the functions common to all locale stores
  18. type LocaleStore interface {
  19. io.Closer
  20. // SetDefaultLang sets the default language to fall back to
  21. SetDefaultLang(lang string)
  22. // ListLangNameDesc provides paired slices of language names to descriptors
  23. ListLangNameDesc() (names, desc []string)
  24. // Locale return the locale for the provided language or the default language if not found
  25. Locale(langName string) (Locale, bool)
  26. // HasLang returns whether a given language is present in the store
  27. HasLang(langName string) bool
  28. // AddLocaleByIni adds a new language to the store
  29. AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error
  30. }
  31. // ResetDefaultLocales resets the current default locales
  32. // NOTE: this is not synchronized
  33. func ResetDefaultLocales() {
  34. if DefaultLocales != nil {
  35. _ = DefaultLocales.Close()
  36. }
  37. DefaultLocales = NewLocaleStore()
  38. }
  39. // GetLocale returns the locale from the default locales
  40. func GetLocale(lang string) (Locale, bool) {
  41. return DefaultLocales.Locale(lang)
  42. }