The LocalizationManager was originally created to handle resources and languages in WPF applications in a clean and MVVM-friendly way.
This README will guide you step by step through the setup and usage of the library 🚀
Open your project terminal and run:
dotnet add package Cacx.LocalizationManager --version latestGo to your Window.xaml and declare the required namespaces:
xmlns:designLoc="clr-namespace:Cacx.LocalizationManager.Core;assembly=Cacx.LocalizationManager"
xmlns:mvvm="clr-namespace:SampleProject.MVVM"
⚠️ Themvvmnamespace depends on your project structure.
You should use MVVM for this library to work optimally.
Add a design-time DataContext so the designer can show preview values:
<d:Window.DataContext>
<designLoc:DesignTimeWindowContext/>
</d:Window.DataContext>Then set the runtime DataContext (optional if done in code):
<Window.DataContext>
<mvvm:MainWindowMVVM/>
</Window.DataContext>Inside your ViewModel, add and initialize the LocalizationProvider:
public LocalizationProvider Loc { get; }
public MainWindowMVVM()
{
// We will explain the constructor parameters in the next step
Loc = new LocalizationProvider(
resourceName: "SampleProject.Resources.MainWindow.MainWindow",
cultureInfo: null
);
}This tutorial assumes the following structure:
MyApp (Project)
└─ Resources
├─ Login
│ ├─ Login.resx
│ └─ Login.de-DE.resx
└─ CreateAccount
├─ CreateAccount.resx
└─ CreateAccount.de-DE.resx
🧠 Important concept:
Each subfolder represents a context (usually a Window or View).
new LocalizationProvider(
resourceName: "SampleProject.Resources.Login.Login",
cultureInfo: null
);Grammar:
{ProjectName}.{ResourcesFolder}.{ContextFolder}.{BaseResxName}
cultureInfonull→ system default language 🌍- or a specific
CultureInfo(e.g.de-DE)
You can now bind localized strings in XAML:
<TextBlock Text="{Binding Loc[WelcomeMessage]}" />
XAML bindings only support strings.
If you need:
- Streams
- Images
- Other objects
➡️ You must retrieve them via code.
Example: Login Context
- Go to
MyApp → Resources - Create a folder called
Login - Create:
Login.resx(base file)Login.de-DE.resx(German)
Add the same keys to all files and translate their values 🌐
Your localization system is now fully working 🚀
The LocalizationProvider offers several useful methods:
void UpdateContext(string resourceName);🔄 Changes the active context (resource file)
void UpdateCulture(CultureInfo culture);🌍 Switches the language (same context)
CultureInfo GetCulture();📌 Returns the current culture
Stream GetStream(string key);📂 Retrieves a stream from the resource file
object GetObject(string key);📦 Retrieves an object from the resource file
⚠️ Objects cannot be added via the RESX GUI.
They must be added via code usingResourceWriter.
A helper method for this will be added in a future release.