In this lesson, you will learn how to optimize your Blazor WebAssembly performance by loading localization resources on-demand instead of bundling every language into your initial download.
The Problem with Static Localization
By default, most localization setups include all your Resources.resx files in the main assembly. As your application grows and supports more languages, your initial bundle size inflates, leading to longer load times for users. For a global application, a user in France shouldn't have to download strings for Japanese or Arabic.
Loading Resources On-Demand
Instead of bundling everything, we can treat translation files as external assets—like JSON files or satellite assemblies—that are fetched only when the user changes their culture settings.
When your application boots, you detect the user's CultureInfo. Rather than having a monolithic resource set, you implement a custom IStringLocalizer that fetches the specific JSON or RESX file for the active culture via HttpClient only when needed.
Here is a simplified approach to how you might fetch a dictionary dynamically:
public async Task<IDictionary<string, string>> LoadTranslations(string culture)
{
// Fetch only the relevant JSON file from the wwwroot/locales folder
var response = await Http.GetFromJsonAsync<Dictionary<string, string>>($"locales/{culture}.json");
return response ?? new Dictionary<string, string>();
}
Managing State
Once you fetch the dictionary, you cache it in a service that implements IStringLocalizer. When the user switches languages (e.g., via a dropdown menu), you trigger a state update. The UI components, which are bound to your IStringLocalizer, will then re-render using the newly loaded keys.
By offloading the localization data to the file system (or an external CDN), the browser only downloads the specific language pack required for the current session. This keeps your blazor.boot.json lightweight and ensures the initial "Time to Interactive" remains fast, regardless of how many languages you support.
Key Takeaway: Dynamic resource localization reduces your initial application payload by fetching translation files over the network only when the user's culture context changes.