From 0574165f922c546830fd9faf4dfd0d835d3e329c Mon Sep 17 00:00:00 2001 From: Didier Roche Date: Tue, 18 Jul 2023 09:29:02 +0200 Subject: [PATCH 1/2] Allow to Set and Get global locale storage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By setting globalConfig.storage object, it's up to the loading wrapper to set Locale.Domains map to desired manually loaded Translators like embedding po as []bytes or so, using dedicated files… Then, the project can use gotext.Get() or any root level methods transparently. This is very useful if you want to test your code from your own repo (using embedded po files if you detect the root of your projects), while still being able to build your project and deploy to an installation, which will load from /usr/share/… Fixes: #52. --- gotext.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gotext.go b/gotext.go index 95c7b11..a999261 100644 --- a/gotext.go +++ b/gotext.go @@ -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, From 6638bd22d11de28fd5837cb5c3863526cdce739e Mon Sep 17 00:00:00 2001 From: Didier Roche Date: Tue, 18 Jul 2023 09:32:27 +0200 Subject: [PATCH 2/2] Tests for setting and getting locale as global configuration. Ensure we override and set expected properties on the default config object. --- gotext_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/gotext_test.go b/gotext_test.go index 37d8c7a..561ea69 100644 --- a/gotext_test.go +++ b/gotext_test.go @@ -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) { @@ -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