Skip to content

Repository files navigation

🌏WinUI3Localizer

WinUI3Localizer is a NuGet package that helps you localize your WinUI 3 app.

  • Switch languages without app restarting
  • You/users can edit localized strings even after deployment
  • You/users can add new languages even after deployment
  • Use standard Resources.resw (see Microsoft docs)

🙌 Quick Start

Note: This is a quick start guide. Check the sample app for details.

Install WinUI3Localizer

Install WinUI3Localizer from the NuGet Package Manager.

Create localized strings

Create a "Strings" folder in your app project and populate it with your string resources files for each language you need. For example, this is a basic structure for English (en-US), es-ES (Spanish) and Japanese (ja) resources files.

  • Strings
    • en-US
      • Resources.resw
    • es-ES
      • Resources.resw
    • ja
      • Resources.resw

Add this ItemGroup in the project file (*.csproj) of your app.

<!-- Copy all "Resources.resw" files in the "Strings" folder to the output folder. -->
<ItemGroup>
<ContentInclude="Strings\**\*.resw">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

Note: The "Strings" folder can be anywhere as long the app can access it. Usually, aside the app executable for non-packaged apps, or in the "LocalFolder" for packaged-apps.

Build WinUI3Localizer

  • Non-packaged apps:

    In App.xaml.cs, build WinUI3Localizer like this:

    privateasyncTaskInitializeLocalizer(){// Initialize a "Strings" folder in the executables folder.StringsFolderPathStringsFolderPath=Path.Combine(AppContext.BaseDirectory,"Strings");StorageFolderstringsFolder=awaitStorageFolder.GetFolderFromPathAsync(StringsFolderPath);ILocalizerlocalizer=awaitnewLocalizerBuilder().AddStringResourcesFolderForLanguageDictionaries(StringsFolderPath).SetOptions(options =>{options.DefaultLanguage="en-US";}).Build();}
  • Packaged apps:

    In App.xaml.cs, build WinUI3Localizer like this:

    privateasyncTaskInitializeLocalizer(){// Initialize a "Strings" folder in the "LocalFolder" for the packaged app.StorageFolderlocalFolder=ApplicationData.Current.LocalFolder;StorageFolderstringsFolder=awaitlocalFolder.CreateFolderAsync("Strings",CreationCollisionOption.OpenIfExists);// Create string resources file from app resources if doesn't exists.stringresourceFileName="Resources.resw";awaitCreateStringResourceFileIfNotExists(stringsFolder,"en-US",resourceFileName);awaitCreateStringResourceFileIfNotExists(stringsFolder,"es-ES",resourceFileName);awaitCreateStringResourceFileIfNotExists(stringsFolder,"ja",resourceFileName);ILocalizerlocalizer=awaitnewLocalizerBuilder().AddStringResourcesFolderForLanguageDictionaries(stringsFolder.Path).SetOptions(options =>{options.DefaultLanguage="en-US";}).Build();}privatestaticasyncTaskCreateStringResourceFileIfNotExists(StorageFolderstringsFolder,stringlanguage,stringresourceFileName){StorageFolderlanguageFolder=awaitstringsFolder.CreateFolderAsync(language,CreationCollisionOption.OpenIfExists);if(awaitlanguageFolder.TryGetItemAsync(resourceFileName)isnull){stringresourceFilePath=Path.Combine(stringsFolder.Name,language,resourceFileName);StorageFileresourceFile=awaitLoadStringResourcesFileFromAppResource(resourceFilePath);_=awaitresourceFile.CopyAsync(languageFolder);}}privatestaticasyncTask<StorageFile>LoadStringResourcesFileFromAppResource(stringfilePath){UriresourcesFileUri=new($"ms-appx:///{filePath}");returnawaitStorageFile.GetFileFromApplicationUriAsync(resourcesFileUri);}

Localizing controls

This is an example of how to localize the Content of a Button.

First assign an Uid to the Button, then in each language resources file, add an item that corresponds to the Uid.

You can also have multiple string resources files. For example, besides the default Resources.resw file, you can have a Messages.resw for your messages file. To just need to include /<resources-file-name>/ before the string resource identifier.

<Pagex:Class="WinUI3Localizer.SampleApp.TestPage"
...
xmlns:l="using:WinUI3Localizer">
<StackPanel>
<Button l:Uids.Uid="TestPage_Button">
<Button.Flyout>
<Flyout>
<TextBlock l:Uids.Uid="/Messages/ButtonFlyoutMessage" />
</Flyout>
</Button.Flyout>
</Button>
</StackPanel>
</Page>
  • en-US

    • Resources.resw

      NameValue
      TestPageButton.ContentAwesome!
    • Messages.resw

      NameValue
      ButtonFlyoutMessage.TextThis is an awesome message!
  • es-ES:

    • Resources.resw

      NameValue
      TestPageButton.Content¡Increíble!
    • Messages.resw

      NameValue
      ButtonFlyoutMessage.Text¡Esto es un mensaje increíble!
  • ja:

    • Resources.resw

      NameValue
      TestPageButton.Content素晴らしい!
    • Messages.resw

      NameValue
      ButtonFlyoutMessage.Textこれは素晴らしいメッセージです!

Getting localized strings

If we need to localize strings in code-behind or in ViewModels, we can use the GetLocalizedString() method.

List<string>colors=new(){"Red","Green","Blue",};ILocalizerlocalizer=Localizer.Get();List<string>localizedColors=colors.Select(x =>localizer.GetLocalizedString(x)).ToList();

In this case, we just use the Uid as Name.

  • en-US

    • Resources.resw

      NameValue
      RedRed
      GreenGreen
      BlueBlue
  • es-ES:

    • Resources.resw

      NameValue
      RedRojo
      GreenVerde
      BlueAzul
  • ja:

    • Resources.resw

      NameValue
      Red
      Green
      Blue

Minimal example

Refer to TemplateStudioWinUI3LocalizerSampleApp

About

The WinUI3Localizer is a NuGet package that helps you localize your WinUI 3 app.

Topics

Resources

Stars

127 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages