code.gitea.io/gitea@v1.19.3/modules/translation/i18n/i18n.go (about)

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