2016-06-19 22:36:33 +00:00
|
|
|
/*
|
|
|
|
Package gotext implements GNU gettext utilities.
|
2016-06-24 13:54:48 +00:00
|
|
|
|
|
|
|
For quick/simple translations you can use the package level functions directly.
|
|
|
|
|
2024-07-22 06:30:26 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-22 06:33:57 +00:00
|
|
|
"code.rocket9labs.com/tslocum/gotext"
|
2024-07-22 06:30:26 +00:00
|
|
|
)
|
2016-06-24 20:04:46 +00:00
|
|
|
|
2024-07-22 06:30:26 +00:00
|
|
|
func main() {
|
|
|
|
// Configure package
|
|
|
|
gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")
|
2016-06-24 20:04:46 +00:00
|
|
|
|
2024-07-22 06:30:26 +00:00
|
|
|
// Translate text from default domain
|
|
|
|
fmt.Println(gotext.Get("My text on 'domain-name' domain"))
|
2016-06-24 20:04:46 +00:00
|
|
|
|
2024-07-22 06:30:26 +00:00
|
|
|
// Translate text from a different domain without reconfigure
|
|
|
|
fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
|
|
|
|
}
|
2016-06-19 22:36:33 +00:00
|
|
|
*/
|
|
|
|
package gotext
|
|
|
|
|
2018-02-13 20:35:07 +00:00
|
|
|
import (
|
2023-04-17 12:15:55 +00:00
|
|
|
"strings"
|
2018-02-13 20:35:07 +00:00
|
|
|
"sync"
|
|
|
|
)
|
2017-11-02 13:21:20 +00:00
|
|
|
|
2016-06-19 22:36:33 +00:00
|
|
|
// Global environment variables
|
2018-02-13 20:35:07 +00:00
|
|
|
type config struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// Path to library directory where all locale directories and Translation files are.
|
|
|
|
library string
|
|
|
|
|
2016-06-19 22:36:33 +00:00
|
|
|
// Default domain to look at when no domain is specified. Used by package level functions.
|
2018-02-13 20:35:07 +00:00
|
|
|
domain string
|
2016-06-19 22:36:33 +00:00
|
|
|
|
|
|
|
// Language set.
|
2023-04-17 12:15:55 +00:00
|
|
|
languages []string
|
2016-06-19 22:36:33 +00:00
|
|
|
|
|
|
|
// Storage for package level methods
|
2023-04-17 12:15:55 +00:00
|
|
|
locales []*Locale
|
2018-02-13 20:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var globalConfig *config
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
var FallbackLocale = "en_US"
|
|
|
|
|
2018-02-13 20:35:07 +00:00
|
|
|
func init() {
|
2018-09-07 21:14:38 +00:00
|
|
|
// Init default configuration
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig = &config{
|
2023-04-17 12:15:55 +00:00
|
|
|
domain: "default",
|
|
|
|
languages: []string{FallbackLocale},
|
|
|
|
library: "/usr/local/share/locale",
|
|
|
|
locales: nil,
|
2018-02-13 20:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-19 22:36:33 +00:00
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
// loadLocales creates a new Locale object for every language (specified using Configure)
|
|
|
|
// at package level based on the configuration of global configuration .
|
|
|
|
// It is called when trying to use Get or GetD methods.
|
|
|
|
func loadLocales(rebuildCache bool) {
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Lock()
|
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
if globalConfig.locales == nil || rebuildCache {
|
2023-04-17 12:15:55 +00:00
|
|
|
var locales []*Locale
|
|
|
|
for _, language := range globalConfig.languages {
|
|
|
|
locales = append(locales, NewLocale(globalConfig.library, language))
|
|
|
|
}
|
|
|
|
globalConfig.locales = locales
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
for _, locale := range globalConfig.locales {
|
2023-08-30 08:31:23 +00:00
|
|
|
if _, ok := locale.Domains[globalConfig.domain]; !ok || rebuildCache {
|
2023-04-17 12:15:55 +00:00
|
|
|
locale.AddDomain(globalConfig.domain)
|
|
|
|
}
|
|
|
|
locale.SetDomain(globalConfig.domain)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
2018-02-13 20:35:07 +00:00
|
|
|
|
|
|
|
globalConfig.Unlock()
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDomain is the domain getter for the package configuration
|
|
|
|
func GetDomain() string {
|
2018-03-23 20:17:05 +00:00
|
|
|
var dom string
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.RLock()
|
2023-04-17 12:15:55 +00:00
|
|
|
if globalConfig.locales != nil {
|
|
|
|
// All locales have the same domain
|
|
|
|
dom = globalConfig.locales[0].GetDomain()
|
2018-03-23 20:17:05 +00:00
|
|
|
}
|
|
|
|
if dom == "" {
|
|
|
|
dom = globalConfig.domain
|
|
|
|
}
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.RUnlock()
|
|
|
|
|
|
|
|
return dom
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDomain sets the name for the domain to be used at package level.
|
2018-03-23 20:17:05 +00:00
|
|
|
// It reloads the corresponding Translation file.
|
2016-06-19 22:36:33 +00:00
|
|
|
func SetDomain(dom string) {
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Lock()
|
|
|
|
globalConfig.domain = dom
|
2023-04-17 12:15:55 +00:00
|
|
|
if globalConfig.locales != nil {
|
|
|
|
for _, locale := range globalConfig.locales {
|
|
|
|
locale.SetDomain(dom)
|
|
|
|
}
|
2018-03-23 20:17:05 +00:00
|
|
|
}
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Unlock()
|
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
loadLocales(true)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// GetLanguage returns the language gotext will translate into.
|
|
|
|
// If multiple languages have been supplied, the first one will be returned.
|
|
|
|
// If no language has been supplied, the fallback will be returned.
|
2016-06-19 22:36:33 +00:00
|
|
|
func GetLanguage() string {
|
2023-04-17 12:15:55 +00:00
|
|
|
languages := GetLanguages()
|
|
|
|
if len(languages) == 0 {
|
|
|
|
return FallbackLocale
|
|
|
|
}
|
|
|
|
return languages[0]
|
|
|
|
}
|
2023-08-29 16:20:03 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// GetLanguages returns all languages that have been supplied.
|
|
|
|
func GetLanguages() []string {
|
|
|
|
globalConfig.RLock()
|
|
|
|
defer globalConfig.RUnlock()
|
|
|
|
return globalConfig.languages
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// SetLanguage sets the language code (or colon separated language codes) to be used at package level.
|
2018-03-23 20:17:05 +00:00
|
|
|
// It reloads the corresponding Translation file.
|
2016-06-19 22:36:33 +00:00
|
|
|
func SetLanguage(lang string) {
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Lock()
|
2023-04-17 12:15:55 +00:00
|
|
|
var languages []string
|
|
|
|
for _, language := range strings.Split(lang, ":") {
|
|
|
|
languages = append(languages, SimplifiedLocale(language))
|
|
|
|
}
|
|
|
|
globalConfig.languages = languages
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Unlock()
|
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
loadLocales(true)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLibrary is the library getter for the package configuration
|
|
|
|
func GetLibrary() string {
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.RLock()
|
|
|
|
lib := globalConfig.library
|
|
|
|
globalConfig.RUnlock()
|
|
|
|
|
|
|
|
return lib
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 09:56:44 +00:00
|
|
|
// SetLibrary sets the root path for the locale directories and files to be used at package level.
|
2018-03-23 20:17:05 +00:00
|
|
|
// It reloads the corresponding Translation file.
|
2016-06-19 22:36:33 +00:00
|
|
|
func SetLibrary(lib string) {
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Lock()
|
|
|
|
globalConfig.library = lib
|
|
|
|
globalConfig.Unlock()
|
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
loadLocales(true)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
func GetLocales() []*Locale {
|
|
|
|
globalConfig.RLock()
|
|
|
|
defer globalConfig.RUnlock()
|
|
|
|
return globalConfig.locales
|
|
|
|
}
|
|
|
|
|
2023-07-18 07:29:02 +00:00
|
|
|
// GetStorage is the locale storage getter for the package configuration.
|
2023-04-17 12:15:55 +00:00
|
|
|
//
|
2023-08-30 08:31:23 +00:00
|
|
|
// Deprecated: Storage has been renamed to Locale for consistency, use GetLocales instead.
|
2023-07-18 07:29:02 +00:00
|
|
|
func GetStorage() *Locale {
|
2023-04-17 12:15:55 +00:00
|
|
|
locales := GetLocales()
|
|
|
|
if len(locales) > 0 {
|
2023-08-30 08:31:23 +00:00
|
|
|
return locales[0]
|
2023-04-17 12:15:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLocales allows for overriding the global Locale objects with ones built manually with
|
|
|
|
// NewLocale(). This makes it possible to attach custom Domain objects from in-memory po/mo.
|
|
|
|
// The library, language and domain of the first Locale will set the default global configuration.
|
|
|
|
func SetLocales(locales []*Locale) {
|
|
|
|
globalConfig.Lock()
|
|
|
|
defer globalConfig.Unlock()
|
2023-07-18 07:29:02 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
globalConfig.locales = locales
|
|
|
|
globalConfig.library = locales[0].path
|
|
|
|
globalConfig.domain = locales[0].defaultDomain
|
|
|
|
|
|
|
|
var languages []string
|
|
|
|
for _, locale := range locales {
|
|
|
|
languages = append(languages, locale.lang)
|
|
|
|
}
|
|
|
|
globalConfig.languages = languages
|
2023-07-18 07:29:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// SetStorage allows overriding the global Locale object with one built manually with NewLocale().
|
|
|
|
//
|
2023-08-30 08:31:23 +00:00
|
|
|
// Deprecated: Storage has been renamed to Locale for consistency, use SetLocales instead.
|
|
|
|
func SetStorage(locale *Locale) {
|
|
|
|
SetLocales([]*Locale{locale})
|
2023-07-18 07:29:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Configure sets all configuration variables to be used at package level and reloads the corresponding Translation file.
|
2016-06-19 22:36:33 +00:00
|
|
|
// It receives the library path, language code and domain name.
|
|
|
|
// This function is recommended to be used when changing more than one setting,
|
2018-03-23 20:17:05 +00:00
|
|
|
// as using each setter will introduce a I/O overhead because the Translation file will be loaded after each set.
|
2016-06-19 22:36:33 +00:00
|
|
|
func Configure(lib, lang, dom string) {
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.Lock()
|
|
|
|
globalConfig.library = lib
|
2023-04-17 12:15:55 +00:00
|
|
|
var languages []string
|
|
|
|
for _, language := range strings.Split(lang, ":") {
|
|
|
|
languages = append(languages, SimplifiedLocale(language))
|
|
|
|
}
|
|
|
|
globalConfig.languages = languages
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.domain = dom
|
|
|
|
globalConfig.Unlock()
|
2016-06-19 22:36:33 +00:00
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
loadLocales(true)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Get uses the default domain globally set to return the corresponding Translation of a given string.
|
2016-06-23 14:41:38 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
2016-06-19 22:36:33 +00:00
|
|
|
func Get(str string, vars ...interface{}) string {
|
2018-02-13 20:35:07 +00:00
|
|
|
return GetD(GetDomain(), str, vars...)
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetN retrieves the (N)th plural form of Translation for the given string in the default domain.
|
2016-06-23 14:41:38 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
|
|
|
func GetN(str, plural string, n int, vars ...interface{}) string {
|
2018-02-13 20:35:07 +00:00
|
|
|
return GetND(GetDomain(), str, plural, n, vars...)
|
2016-06-23 14:41:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetD returns the corresponding Translation in the given domain for a given string.
|
2016-06-23 14:41:38 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
2016-06-19 22:36:33 +00:00
|
|
|
func GetD(dom, str string, vars ...interface{}) string {
|
2023-08-30 08:31:23 +00:00
|
|
|
// Try to load default package Locales
|
|
|
|
loadLocales(false)
|
2020-04-19 19:50:56 +00:00
|
|
|
|
|
|
|
globalConfig.RLock()
|
2023-04-17 12:15:55 +00:00
|
|
|
defer globalConfig.RUnlock()
|
2020-04-19 19:50:56 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
var tr string
|
|
|
|
for i, locale := range globalConfig.locales {
|
|
|
|
if _, ok := locale.Domains[dom]; !ok {
|
|
|
|
locale.AddDomain(dom)
|
|
|
|
}
|
|
|
|
if !locale.IsTranslatedD(dom, str) && i < (len(globalConfig.locales)-1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tr = locale.GetD(dom, str, vars...)
|
|
|
|
break
|
2020-04-19 19:50:56 +00:00
|
|
|
}
|
|
|
|
return tr
|
2016-06-23 14:41:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetND retrieves the (N)th plural form of Translation in the given domain for a given string.
|
2016-06-23 14:41:38 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
|
|
|
func GetND(dom, str, plural string, n int, vars ...interface{}) string {
|
2023-08-30 08:31:23 +00:00
|
|
|
// Try to load default package Locales
|
|
|
|
loadLocales(false)
|
2016-06-19 22:36:33 +00:00
|
|
|
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.RLock()
|
2023-04-17 12:15:55 +00:00
|
|
|
defer globalConfig.RUnlock()
|
2018-12-07 04:32:39 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
var tr string
|
|
|
|
for i, locale := range globalConfig.locales {
|
|
|
|
if _, ok := locale.Domains[dom]; !ok {
|
|
|
|
locale.AddDomain(dom)
|
|
|
|
}
|
|
|
|
if !locale.IsTranslatedND(dom, str, n) && i < (len(globalConfig.locales)-1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tr = locale.GetND(dom, str, plural, n, vars...)
|
|
|
|
break
|
2018-12-07 04:32:39 +00:00
|
|
|
}
|
2018-02-13 20:35:07 +00:00
|
|
|
return tr
|
2016-06-19 22:36:33 +00:00
|
|
|
}
|
2016-06-26 18:43:54 +00:00
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetC uses the default domain globally set to return the corresponding Translation of the given string in the given context.
|
2016-06-26 18:43:54 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
|
|
|
func GetC(str, ctx string, vars ...interface{}) string {
|
2018-02-13 20:35:07 +00:00
|
|
|
return GetDC(GetDomain(), str, ctx, vars...)
|
2016-06-26 18:43:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetNC retrieves the (N)th plural form of Translation for the given string in the given context in the default domain.
|
2016-06-26 18:43:54 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
|
|
|
func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
|
2018-02-13 20:35:07 +00:00
|
|
|
return GetNDC(GetDomain(), str, plural, n, ctx, vars...)
|
2016-06-26 18:43:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetDC returns the corresponding Translation in the given domain for the given string in the given context.
|
2016-06-26 18:43:54 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
|
|
|
func GetDC(dom, str, ctx string, vars ...interface{}) string {
|
2023-08-30 08:31:23 +00:00
|
|
|
// Try to load default package Locales
|
|
|
|
loadLocales(false)
|
2020-04-19 19:50:56 +00:00
|
|
|
|
|
|
|
globalConfig.RLock()
|
2023-04-17 12:15:55 +00:00
|
|
|
defer globalConfig.RUnlock()
|
2020-04-19 19:50:56 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
var tr string
|
|
|
|
for i, locale := range globalConfig.locales {
|
|
|
|
if !locale.IsTranslatedDC(dom, str, ctx) && i < (len(globalConfig.locales)-1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tr = locale.GetDC(dom, str, ctx, vars...)
|
|
|
|
break
|
|
|
|
}
|
2020-04-19 19:50:56 +00:00
|
|
|
return tr
|
2016-06-26 18:43:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// GetNDC retrieves the (N)th plural form of Translation in the given domain for a given string.
|
2016-06-26 18:43:54 +00:00
|
|
|
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
|
|
|
|
func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string {
|
2023-08-30 08:31:23 +00:00
|
|
|
// Try to load default package Locales
|
|
|
|
loadLocales(false)
|
2016-06-26 18:43:54 +00:00
|
|
|
|
2018-03-23 20:17:05 +00:00
|
|
|
// Return Translation
|
2018-02-13 20:35:07 +00:00
|
|
|
globalConfig.RLock()
|
2023-04-17 12:15:55 +00:00
|
|
|
defer globalConfig.RUnlock()
|
2018-02-13 20:35:07 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
var tr string
|
|
|
|
for i, locale := range globalConfig.locales {
|
|
|
|
if !locale.IsTranslatedNDC(dom, str, n, ctx) && i < (len(globalConfig.locales)-1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tr = locale.GetNDC(dom, str, plural, n, ctx, vars...)
|
|
|
|
break
|
|
|
|
}
|
2018-02-13 20:35:07 +00:00
|
|
|
return tr
|
2016-06-26 18:43:54 +00:00
|
|
|
}
|
2023-04-04 15:22:21 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslated reports whether a string is translated in given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslated(str string, langs ...string) bool {
|
|
|
|
return IsTranslatedND(GetDomain(), str, 0, langs...)
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedN reports whether a plural string is translated in given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedN(str string, n int, langs ...string) bool {
|
|
|
|
return IsTranslatedND(GetDomain(), str, n, langs...)
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedD reports whether a domain string is translated in given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedD(dom, str string, langs ...string) bool {
|
|
|
|
return IsTranslatedND(dom, str, 0, langs...)
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedND reports whether a plural domain string is translated in any of given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedND(dom, str string, n int, langs ...string) bool {
|
|
|
|
if len(langs) == 0 {
|
|
|
|
langs = GetLanguages()
|
|
|
|
}
|
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
loadLocales(false)
|
2023-04-04 15:22:21 +00:00
|
|
|
|
|
|
|
globalConfig.RLock()
|
|
|
|
defer globalConfig.RUnlock()
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
for _, lang := range langs {
|
|
|
|
lang = SimplifiedLocale(lang)
|
2023-04-04 15:22:21 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
for _, supportedLocale := range globalConfig.locales {
|
|
|
|
if lang != supportedLocale.GetActualLanguage(dom) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return supportedLocale.IsTranslatedND(dom, str, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedC reports whether a context string is translated in given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedC(str, ctx string, langs ...string) bool {
|
|
|
|
return IsTranslatedNDC(GetDomain(), str, 0, ctx, langs...)
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedNC reports whether a plural context string is translated in given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedNC(str string, n int, ctx string, langs ...string) bool {
|
|
|
|
return IsTranslatedNDC(GetDomain(), str, n, ctx, langs...)
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedDC reports whether a domain context string is translated in given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedDC(dom, str, ctx string, langs ...string) bool {
|
|
|
|
return IsTranslatedNDC(dom, str, 0, ctx, langs...)
|
2023-08-29 16:20:03 +00:00
|
|
|
}
|
2023-04-17 12:15:55 +00:00
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
// IsTranslatedNDC reports whether a plural domain context string is translated in any of given languages.
|
|
|
|
// When the langs argument is omitted, the output of GetLanguages is used.
|
|
|
|
func IsTranslatedNDC(dom, str string, n int, ctx string, langs ...string) bool {
|
|
|
|
if len(langs) == 0 {
|
|
|
|
langs = GetLanguages()
|
|
|
|
}
|
|
|
|
|
2023-08-30 08:31:23 +00:00
|
|
|
loadLocales(false)
|
2023-04-04 15:22:21 +00:00
|
|
|
|
|
|
|
globalConfig.RLock()
|
|
|
|
defer globalConfig.RUnlock()
|
|
|
|
|
2023-04-17 12:15:55 +00:00
|
|
|
for _, lang := range langs {
|
|
|
|
lang = SimplifiedLocale(lang)
|
|
|
|
|
|
|
|
for _, locale := range globalConfig.locales {
|
|
|
|
if lang != locale.GetActualLanguage(dom) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return locale.IsTranslatedNDC(dom, str, n, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2023-04-04 15:22:21 +00:00
|
|
|
}
|