ColorPicker localization in WinUI 3

Jacob Mordon 225 Reputation points
2024-09-02T10:30:17.9433333+00:00

Hi, I would like to know, can I somehow change localization of Microsoft.UI.Xaml.Controls.ColorPicker in Runtime, because I noticed if I restart application, it will automatically localize. I want to change tooltips, which are on AlphaSlider, ValueSlider and on ColorSpectrum, but I don't want to write translation to every possible color.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,822 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,886 questions
{count} votes

Accepted answer
  1. Soumyadip Majumder 75 Reputation points
    2024-09-03T11:44:50.9766667+00:00

    Hey @Jacob Mordon

    I am understand your issue, you can choose this solution for this project work.

    First you can change the localization of **Microsoft.UI.Xaml.Controls.ColorPicker** the UI.Xaml.Cotrols

    this library comes by default with .NET 8.0.0 runtime, if you are using the 'Visual Studio 2022' the purple one. Then go to the tools-->NuGet Package manager--> check your element (ColorPicker). Reinstall it. If it is not solved. Then track the Microsoft.UI.Xaml.Controls.ColorPicker in your laptop or desktop, find them locally and updated the path in the environment variables.

    Another things you can do:
    Step 1: Find and Update TextBlocks

    You need to find the TextBlock elements within the ColorPicker and update their text manually. Here’s a sample code to help you get started:

    **

    public List<TextBlock> TextBlocks { get; set; }

    public MainWindow()

    {

    this.InitializeComponent();
    
    TextBlocks = new List<TextBlock>();
    

    }

    private void UpdateLocalization()

    {

    FindTextChild(TextBlocks, MyColorPicker);
    
    ApplyLocalization(TextBlocks);
    

    }

    public static void FindTextChild(List<TextBlock> textBlocks, DependencyObject parent)

    {

    int count = VisualTreeHelper.GetChildrenCount(parent);
    
    for (int i = 0; i < count; i++)
    
    {
    
        var child = VisualTreeHelper.GetChild(parent, i);
    
        if (child is TextBlock textBlock)
    
        {
    
            textBlocks.Add(textBlock);
    
        }
    
        else
    
        {
    
            FindTextChild(textBlocks, child);
    
        }
    
    }
    

    }

    public void ApplyLocalization(List<TextBlock> textBlocks)

    {

    var localizationStrings = new Dictionary<string, string>
    
    {
    
        { "Alpha", "AlphaText" },
    
        { "Value", "ValueText" },
    
        { "Color", "ColorText" }
    
    };
    
    foreach (var textBlock in textBlocks)
    
    {
    
        if (localizationStrings.ContainsKey(textBlock.Text))
    
        {
    
            textBlock.Text = localizationStrings[textBlock.Text];
    
        }
    
    }
    

    }

    **

    Step 2: Trigger Localization Update

    You can call the UpdateLocalization method whenever you need to change the localization, such as when the user changes the language in your application.

    Step 3: Handle Tooltips

    If you need to update tooltips specifically, you can extend the above approach to find and update ToolTip elements as well.

    Also, you can refer this link: https://video2.skills-academy.com/en-us/windows/apps/winui/winui3/localize-winui3-app

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.