code.gitea.io/gitea@v1.22.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  	"html/template"
     8  	"io"
     9  )
    10  
    11  var DefaultLocales = NewLocaleStore()
    12  
    13  type Locale interface {
    14  	// TrString translates a given key and arguments for a language
    15  	TrString(trKey string, trArgs ...any) string
    16  	// TrHTML translates a given key and arguments for a language, string arguments are escaped to HTML
    17  	TrHTML(trKey string, trArgs ...any) template.HTML
    18  	// HasKey reports if a locale has a translation for a given key
    19  	HasKey(trKey string) bool
    20  }
    21  
    22  // LocaleStore provides the functions common to all locale stores
    23  type LocaleStore interface {
    24  	io.Closer
    25  
    26  	// SetDefaultLang sets the default language to fall back to
    27  	SetDefaultLang(lang string)
    28  	// ListLangNameDesc provides paired slices of language names to descriptors
    29  	ListLangNameDesc() (names, desc []string)
    30  	// Locale return the locale for the provided language or the default language if not found
    31  	Locale(langName string) (Locale, bool)
    32  	// HasLang returns whether a given language is present in the store
    33  	HasLang(langName string) bool
    34  	// AddLocaleByIni adds a new language to the store
    35  	AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error
    36  }
    37  
    38  // ResetDefaultLocales resets the current default locales
    39  // NOTE: this is not synchronized
    40  func ResetDefaultLocales() {
    41  	if DefaultLocales != nil {
    42  		_ = DefaultLocales.Close()
    43  	}
    44  	DefaultLocales = NewLocaleStore()
    45  }
    46  
    47  // GetLocale returns the locale from the default locales
    48  func GetLocale(lang string) (Locale, bool) {
    49  	return DefaultLocales.Locale(lang)
    50  }