Skip to content

Commit

Permalink
Rename 'storage' to 'locale'
Browse files Browse the repository at this point in the history
As the package matured, it makes sense to use unified names everywhere.
Since the config holds Locale objects, the variable is called 'locales'.

This patch finishes the work and updates the 'loadStorage' function
to be called 'localLocales' with argument 'rebuildCache' instead of
'force'.
  • Loading branch information
m-horky committed Dec 19, 2023
1 parent f7ff9bf commit c8e8221
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
50 changes: 25 additions & 25 deletions gotext.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func init() {
gob.Register(TranslatorEncoding{})
}

// loadStorage creates a new Locale object at package level based on the Global variables settings
// for every language specified using Configure.
// It's called automatically when trying to use Get or GetD methods.
func loadStorage(force bool) {
// 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) {
globalConfig.Lock()

if globalConfig.locales == nil || force {
if globalConfig.locales == nil || rebuildCache {
var locales []*Locale
for _, language := range globalConfig.languages {
locales = append(locales, NewLocale(globalConfig.library, language))
Expand All @@ -77,7 +77,7 @@ func loadStorage(force bool) {
}

for _, locale := range globalConfig.locales {
if _, ok := locale.Domains[globalConfig.domain]; !ok || force {
if _, ok := locale.Domains[globalConfig.domain]; !ok || rebuildCache {
locale.AddDomain(globalConfig.domain)
}
locale.SetDomain(globalConfig.domain)
Expand Down Expand Up @@ -114,7 +114,7 @@ func SetDomain(dom string) {
}
globalConfig.Unlock()

loadStorage(true)
loadLocales(true)
}

// GetLanguage returns the language gotext will translate into.
Expand Down Expand Up @@ -146,7 +146,7 @@ func SetLanguage(lang string) {
globalConfig.languages = languages
globalConfig.Unlock()

loadStorage(true)
loadLocales(true)
}

// GetLibrary is the library getter for the package configuration
Expand All @@ -165,7 +165,7 @@ func SetLibrary(lib string) {
globalConfig.library = lib
globalConfig.Unlock()

loadStorage(true)
loadLocales(true)
}

func GetLocales() []*Locale {
Expand All @@ -174,11 +174,11 @@ func GetLocales() []*Locale {
return globalConfig.locales
}

// GetStorage is the locale storage getter for the package configuration.
// GetLocale is the locale storage getter for the package configuration.
//
// It returns the first locale returned by GetLocales.
// This function exists for backwards compatibility, prefer using GetLocales directly.
func GetStorage() *Locale {
func GetLocale() *Locale {
locales := GetLocales()
if len(locales) > 0 {
return GetLocales()[0]
Expand All @@ -204,7 +204,7 @@ func SetLocales(locales []*Locale) {
globalConfig.languages = languages
}

// SetStorage allows overriding the global Locale object with one built manually with NewLocale().
// SetLocale allows overriding the global Locale object with one built manually with NewLocale().
// This allows then to attach to the locale Domains object in memory po or mo files (embedded or in any directory),
// for each domain.
// Locale library, language and domain properties will apply on default global configuration.
Expand All @@ -213,8 +213,8 @@ func SetLocales(locales []*Locale) {
//
// This works by calling SetLocales with just one Locale object.
// This function exists for backwards compatibility, prefer using SetLocales directly.
func SetStorage(storage *Locale) {
SetLocales([]*Locale{storage})
func SetLocale(locale *Locale) {
SetLocales([]*Locale{locale})
}

// Configure sets all configuration variables to be used at package level and reloads the corresponding Translation file.
Expand All @@ -232,7 +232,7 @@ func Configure(lib, lang, dom string) {
globalConfig.domain = dom
globalConfig.Unlock()

loadStorage(true)
loadLocales(true)
}

// Get uses the default domain globally set to return the corresponding Translation of a given string.
Expand All @@ -250,8 +250,8 @@ func GetN(str, plural string, n int, vars ...interface{}) string {
// GetD returns the corresponding Translation in the given domain for a given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func GetD(dom, str string, vars ...interface{}) string {
// Try to load default package Locale storage
loadStorage(false)
// Try to load default package Locales
loadLocales(false)

globalConfig.RLock()
defer globalConfig.RUnlock()
Expand All @@ -273,8 +273,8 @@ func GetD(dom, str string, vars ...interface{}) string {
// GetND retrieves the (N)th plural form of Translation in the given domain for a given string.
// 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 {
// Try to load default package Locale storage
loadStorage(false)
// Try to load default package Locales
loadLocales(false)

globalConfig.RLock()
defer globalConfig.RUnlock()
Expand Down Expand Up @@ -308,8 +308,8 @@ func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
// GetDC returns the corresponding Translation in the given domain for the given string in the given context.
// 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 {
// Try to load default package Locale storage
loadStorage(false)
// Try to load default package Locales
loadLocales(false)

globalConfig.RLock()
defer globalConfig.RUnlock()
Expand All @@ -328,8 +328,8 @@ func GetDC(dom, str, ctx string, vars ...interface{}) string {
// GetNDC retrieves the (N)th plural form of Translation in the given domain for a given string.
// 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 {
// Try to load default package Locale storage
loadStorage(false)
// Try to load default package Locales
loadLocales(false)

// Return Translation
globalConfig.RLock()
Expand Down Expand Up @@ -371,7 +371,7 @@ func IsTranslatedND(dom, str string, n int, langs ...string) bool {
langs = GetLanguages()
}

loadStorage(false)
loadLocales(false)

globalConfig.RLock()
defer globalConfig.RUnlock()
Expand Down Expand Up @@ -414,7 +414,7 @@ func IsTranslatedNDC(dom, str string, n int, ctx string, langs ...string) bool {
langs = GetLanguages()
}

loadStorage(false)
loadLocales(false)

globalConfig.RLock()
defer globalConfig.RUnlock()
Expand Down
14 changes: 7 additions & 7 deletions gotext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func TestGettersSetters(t *testing.T) {

// Create Locale with full language code
l := NewLocale("fixtures/", "fr_FR")
SetStorage(l)
storage := GetStorage()
SetLocale(l)
locale := GetLocale()

if storage != l {
t.Errorf("Expected GetStorage to return provided locale storage %v, but got '%v'", storage, l)
if locale != l {
t.Errorf("Expected GetLocale to return provided locale locale %v, but got '%v'", locale, l)
}
}

Expand Down Expand Up @@ -436,7 +436,7 @@ msgstr[1] "Custom ctx translations"
}
}

func TestOverrideStorage(t *testing.T) {
func TestOverrideLocale(t *testing.T) {
// Configure global translation
Configure("fixtures/", "de_DE", "default")

Expand All @@ -448,14 +448,14 @@ func TestOverrideStorage(t *testing.T) {
// Create and override with our new locale.
l := NewLocale("fixtures/", "fr")
l.SetDomain("default")
SetStorage(l)
SetLocale(l)

language = Get("language")
if language != "fr" {
t.Errorf("Expected default configuration to be overriden by locale 'fr' but got '%s'", language)
}

// Ensure properties were applied on globale configuration when Set.
// Ensure properties were applied on global configuration when Set.
dom := GetDomain()
if dom != "default" {
t.Errorf("Expected GetDomain to return 'default', but got '%s'", dom)
Expand Down

0 comments on commit c8e8221

Please sign in to comment.