Text localization

валера карманов 396 Reputation points
2024-10-03T11:26:40.09+00:00

I want to add the ability to use languages in my application, but from what is written in the doc. it is not clear at all. I am interested in a couple of questions.

  1. In which folder to create resource files. You can use the Resources/Languages folder, and already inside the language resources.
  2. How to access static values in these files in C#, if what is written in the first empty is possible.
  3. The same as point 2 only in XAML

As for the line

xmlns:strings="clr-namespace:LocalizationDemo.Resources.Strings"

replacing the namespace "LocalizationDemo" with your own, I still get an error

Undefined CLR namespace. URI "clr-namespace" refers to the namespace "LocalizationDemo.Resources.Strings", which could not be found.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,587 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,306 Reputation points Microsoft Vendor
    2024-10-04T06:03:57.43+00:00

    Hello,

    There is an official sample, you can refer to it- maui-samples/8.0/Fundamentals/Localization/LocalizationDemo at main · dotnet/maui-samples (github.com)

    In which folder to create resource files.

    If you follow this doc- Localization - .NET MAUI | Microsoft Learn, please create a folder named Strings under Resources folder, then create the resource file there.

    You can use the Resources/Languages folder, and already inside the language resources.

    You name the folder to Languages, it's also allowed.

    How to access static values in these files in C#, if what is written in the first empty is possible.

    You could use it like the following:

    Label notesLabel = new Label(); 
    notesLabel.Text = AppResources.NotesLabel,// find the key in the AppResources.resx
    

    replacing the namespace "LocalizationDemo" with your own, I still get an error

    Because you set the name Languages, please replace the Strings with Languages.

    xmlns:strings="clr-namespace:XXX.Resources.Languages"
    

    Update

    You have created a Languages folder, it's better to move the "AppResources" file there, and the namespace will be changed after moving the file in a folder.

    then in the "Resources/Languages/AppResources.ru.resx" file these values can be translated into the appropriate language?

    Yes, it will. Please follow the doc- Localization - .NET MAUI | Microsoft Learn to specify the app's neutral language and set the language of your machine, then you can see the translated text.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.