-
Notifications
You must be signed in to change notification settings - Fork 12
CatalogManager
CatalogManager is a utility class helping to organize multiple catalogs.
Imagine a scenario with large C# solution, including (shared) libraries and multiple executables. In this case it may be advisable to extract and built multiple catalogs, i.e. on a per-project base. That way translations could be split in multiple smaller chunks, and don't need to built a large (monolithic) catalog.
A challenge though in such scenario is to load and refer to the correct catalog at runtime where you call from an executable to a method on shared library
common shared library project:
public class SharedLibraryClass
{
// Alternative approach - initialize the catalog i.e. in ctor
// private ICatalog catalog;
// public SharedLibraryClass(ICatalog catalog)
// {
// this.catalog = catalog;
// }
public string GetSomeTranslatedString(ICatalog catalog)
{
return catalog.GetString("some string to translate");
}
}
executable A project:
public class ExecutableA
{
static void Main()
{
ICatalog catalog = new Catalog("SharedLibraryDomain");
Console.WriteLine(new SharedLibraryClass(catalog).GetSomeTranslatedString());
}
}
executable B project:
public class ExecutableB
{
static void Main()
{
ICatalog catalog = new Catalog("SharedLibraryDomain");
Console.WriteLine(new SharedLibraryClass(catalog).GetSomeTranslatedString());
}
}
You will need to keep track for all differnt catalog domains to be loaded, passed around if necessary, and set the appriate catalog when calling for translated strings. Or alternatively, provide loader functionality in each library to load and track their own catalog instance.
Thats where CatalogManager comes to the rescue. CatalogManager is a static helper which would track and resolve multiple catalogs. It's using call site resolving to find the correct catalog.
Above scenario would be simplified like this:
common shared library project:
public class SharedLibraryClass
{
public string GetSomeTranslatedString()
{
// CatalogManager will resolve and return the correct catalog out of the current assembly call site.
// To avoid performance penalty when repeatedly calling into CatalogManager.Catalog, it is recommended though
// to cache a reference to ICatalog catalog = CatalogManager.Catalog if possible
return CatalogManager.Catalog.GetString("some string to translate");
}
}
executable A project:
public class ExecutableA
{
static void Main()
{
// Catalog names are derived from (calling) assembly name, and catalogs are to be found in a different directory
CatalogManager.SetCatalogDomainPattern(CatalogDomainPattern.AssemblyName, null, ".\\..\\Locales");
Console.WriteLine(new SharedLibraryClass().GetSomeTranslatedString());
}
}