Skip to content

Commit

Permalink
Merge pull request #80 from didrocks/allow-setting-global-storage
Browse files Browse the repository at this point in the history
Allow setting global storage
  • Loading branch information
leonelquinteros committed Jul 18, 2023
2 parents db315c3 + 6638bd2 commit 1f7d156
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gotext.go
Expand Up @@ -142,6 +142,30 @@ func SetLibrary(lib string) {
loadStorage(true)
}

// GetStorage is the locale storage getter for the package configuration.
func GetStorage() *Locale {
globalConfig.RLock()
storage := globalConfig.storage
globalConfig.RUnlock()

return storage
}

// SetStorage allows overridding 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.
// Any domain not loaded yet will use to the just in time domain loading process.
// Note that any call to gotext.Set* or Configure will invalidate this override.
func SetStorage(storage *Locale) {
globalConfig.Lock()
globalConfig.storage = storage
globalConfig.library = storage.path
globalConfig.language = storage.lang
globalConfig.domain = storage.defaultDomain
globalConfig.Unlock()
}

// Configure sets all configuration variables to be used at package level and reloads the corresponding Translation file.
// It receives the library path, language code and domain name.
// This function is recommended to be used when changing more than one setting,
Expand Down
45 changes: 45 additions & 0 deletions gotext_test.go
Expand Up @@ -29,6 +29,15 @@ func TestGettersSetters(t *testing.T) {
if lang != "es" {
t.Errorf("Expected GetLanguage to return 'es', but got '%s'", lang)
}

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

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

func TestPackageFunctions(t *testing.T) {
Expand Down Expand Up @@ -411,6 +420,42 @@ msgstr[1] "Custom ctx translations"
}
}

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

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

// Create and override with our new locale.
l := NewLocale("fixtures/", "fr")
l.SetDomain("default")
SetStorage(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.
dom := GetDomain()
if dom != "default" {
t.Errorf("Expected GetDomain to return 'default', but got '%s'", dom)
}

lib := GetLibrary()
if lib != "fixtures/" {
t.Errorf("Expected GetLibrary to return 'fixtures/', but got '%s'", lib)
}

lang := GetLanguage()
if lang != "fr" {
t.Errorf("Expected GetLanguage to return 'fr', but got '%s'", lang)
}
}

func TestPackageRace(t *testing.T) {
// Set PO content
str := `# Some comment
Expand Down

0 comments on commit 1f7d156

Please sign in to comment.