Skip to main content
A newer version of this page is available. .

DxLocalizationService Class

The localization service that allows you to create a custom set of satellite assemblies.

Namespace: DevExpress.Blazor.Localization

Assembly: DevExpress.Blazor.v21.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public class DxLocalizationService :
    IDxLocalizationService

Remarks

DevExpress components for the Blazor Server use the standard localization mechanism from the .NET framework - Satellite Resource Assemblies. For more information, refer to Localization.

Our components ship with NuGet packages with predefined satellite assemblies for the following languages and cultures:

  • German (de)
  • Spanish (es)
  • Japanese (ja)
  • Russian (ru)

However, there are no official nor recommended approaches on how to localize Blazor WebAssembly applications. DevExpress Components for Blazor WebAssembly are localized using the DxLocalizationService class. You can also use this class to create custom localization services for Blazor Server applications.

To return translated strings by key, use the GetString(String) method.

The code below demonstrates how you can implement a custom DxLocalizationService to localize a Blazor WebAssembly application.

namespace BlazorClientApp.Services {
    public class DemoLocalizationService : DxLocalizationService {
        readonly Dictionary<string, Dictionary<string, string>> customLocalizations = new Dictionary<string, Dictionary<string, string>>{
            {"de", new Dictionary<string, string> {
                {"Summary", "Zusammenfassung"},
                {"Temperature", "Temp."},
                {"Date", "Datum"},
                {"SelectYourLanguage", "Wählen Sie Ihre Sprache"},
            }},
            {"en-US", new Dictionary<string, string> {
                {"Summary", "Summary"},
                {"Temperature", "Temp."},
                {"Date", "Date"},
                {"SelectYourLanguage", "Select your language"},
            }},
        };

        protected override string GetString(string key) {
            var culture = CultureInfo.CurrentUICulture.Name;
            if(customLocalizations.TryGetValue(culture, out var localization)) {
                if(localization.TryGetValue(key, out var value)) 
                    return value;
            }
            return LocalizationProvider.GetString(CultureInfo.CurrentUICulture.Name, key) ?? base.GetString(key);
        }
    }
}

namespace DevExpress.Blazor.Localization {

    public static class LocalizationProvider {
        [ThreadStatic]
        static readonly Dictionary<string, Dictionary<string, string>> localizations = new Dictionary<string, Dictionary<string, string>>{
            {"de", new Dictionary<string, string> {
                {"DxBlazorStringId.Calendar_TodayButton", "Heute"},
                {"DxBlazorStringId.Calendar_ClearButton", "Entfernen"},
                {"DxBlazorStringId.DateEdit_ChooseDate", "Ein Datum auswählen"},
                {"DxBlazorStringId.DateEdit_ApplyButton", "Anwenden"},
                {"DxBlazorStringId.DateEdit_CloseButton", "Schließen"},
                {"DxBlazorStringId.Grid_GroupPanel", "Ziehen Sie eine Spaltenüberschrift hierher um nach dieser Spalte zu gruppieren"},
                {"DxBlazorStringId.Grid_UpdateButton", "Speichern"},
                {"DxBlazorStringId.Grid_CancelButton", "Abbrechen"},
                ...
                {"DxBlazorStringId.PivotGrid_TotalFormat", "{0} Gesamt"},
            }},
      };
        public static string GetString(string culture, string key) {
            Dictionary<string, string> localization;
            string value = null;
            if(localizations.TryGetValue(culture, out localization)) {
                localization.TryGetValue(key, out value);
            }
            return value;
        }
    }
}

View Example: How to localize DevExpress Blazor components

Inheritance

Object
DxLocalizationService
See Also