How to use Windows system-provided "OK" string resource as CloseButtonText in ContentDialog

Apptacular Apps 386 Reputation points
2020-07-21T15:38:43.933+00:00

Is there a way to use the 'OK' string resource the Windows operating system provides for dialogs containing an OK button? Using hard coding isn't professional when translating to languages that don't use "OK" as the text for an OK button.

private async void DisplayNoWifiDialog()
{
    ContentDialog noWifiDialog = new ContentDialog
    {
        Title = "No wifi connection",
        Content = "Check your connection and try again.",
        CloseButtonText = "OK"
    };

    ContentDialogResult result = await noWifiDialog.ShowAsync();
}
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-07-22T02:21:41.913+00:00

    Hello,

    Welcome to Microsoft Q&A.

    If you want to use multiple languages (localization) in UWP, you need to create a text resource file (.resw). Currently, there is no API to obtain the built-in text resources of the system by name.

    How to build your multilingual text resource file, please refer to this document: Make your app localizable.

    After creating the text resource file, you can get the text resource currently being used by the application through the following code, and get the corresponding value by name:

    public string GetLocalizationTextFromResource(string key)  
    {  
        var loader = ResourceLoader.GetForCurrentView();  
        var language = loader.GetString(key);  
        return language;  
    }  
    

    Then we can use:

    ContentDialog noWifiDialog = new ContentDialog  
    {  
        Title = "No wifi connection",  
        Content = "Check your connection and try again.",  
        CloseButtonText = GetLocalizationTextFromResource("OK")  
    };  
    

    Thanks.

    1 person found this answer helpful.
    0 comments No comments