IDxLocalizationService.GetString(String) Method

Returns a translated string by key.

Namespace: DevExpress.Blazor.Localization

Assembly: DevExpress.Blazor.v21.1.dll

Declaration

string GetString(
    string key
)

Parameters

Name Type Description
key String

A string value that specifies a key.

Returns

Type Description
String

The translated string that corresponds to the specified key.

Remarks

Use the GetString(key) method to obtain translated application resources (button captions, menu items, and etc.) to localize you Blazor applications. For more information, refer to Localization.

The code below demonstrates how to 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;
        }
    }
}

[!exampleHow to localize DevExpress Blazor components

See Also