Allow to Set and Get global locale storage

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.
This commit is contained in:
Didier Roche 2023-07-18 09:29:02 +02:00
parent db315c3450
commit 0574165f92
No known key found for this signature in database
GPG key ID: 98B24A9CE4AC208E

View file

@ -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,