I have a class that handles the Localization of my application. My goal is that the class is usable in the entire application so I made it static. This allows me to call the code below anywhere in my application.
Localizer.GetString(/* Key to localize */)
The method uses several fields in the Localizer class. To make sure these fields are set, a Init
method was added to initialize the Localizer. If the user of my class forgets to call Init
at for example the start-up of the application, exceptions will occur because the fields are not set.
One solution I'm thinking about is making the Localizer class not static, add a constructor that sets the fields and initialize the class in a global static class
in my application, something like this:
public static class Globals
{
public static Localizer Localize = new Localizer(/* Field arguments here */);
}
Now I'm not sure what the best approach is. Either
- Static Localizer but user has to make sure
Init
is called before using the class. - Instantiated Localizer in a global static class in the application.
One more note: The user has no access to the source of the class.