Ghost1372

everything can be handy

DynamicLanguage

We moved all namespaces into a single namespace. No matter which (WinUICommunity) library you use, the namespace is always as follows
For use in the Xaml:
xmlns:wuc="using:WinUICommunity"
For use in the Csharp:
using WinUICommunity;

The Localizer helps you to localize your app.

  • Switch languages without restarting the app
  • You (users) can edit localized strings even after deployment
  • You (users) can add new languages even after deployment
  • Use the starndard Resources.resw
  • Support Both Packaged and UnPackaged

Quick Start

Create your resources in this format:

Strings\en-US\Resources.resw

Strings\fa-IR\Resources.resw

Go to App.cs file and Copy InitializeLocalizer method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

private static string StringsFolderPath { get; set; } = string.Empty;

private async Task InitializeLocalizer(params string[] languages)
{
// Initialize a "Strings" folder in the "LocalFolder" for the packaged app.
if (PackageHelper.IsPackaged)
{
// Create string resources file from app resources if doesn't exists.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder stringsFolder = await localFolder.CreateFolderAsync(
"Strings",
CreationCollisionOption.OpenIfExists);
string resourceFileName = "Resources.resw";
foreach (var item in languages)
{
await LocalizerBuilder.CreateStringResourceFileIfNotExists(stringsFolder, item, resourceFileName);
}

StringsFolderPath = stringsFolder.Path;
}
else
{
// Initialize a "Strings" folder in the executables folder.
StringsFolderPath = Path.Combine(AppContext.BaseDirectory, "Strings");
var stringsFolder = await StorageFolder.GetFolderFromPathAsync(StringsFolderPath);
}


ILocalizer localizer = await new LocalizerBuilder()
.AddStringResourcesFolderForLanguageDictionaries(StringsFolderPath)
.SetOptions(options =>
{
options.DefaultLanguage = "en-US";
})
.Build();
}

now we need to call InitializeLocalizer in OnLaunched method:

1
2
3
4
5
6
7
8
9

protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();

await InitializeLocalizer("fa-IR", "en-US");

m_window.Activate();
}

we need to copy our resources to output next to Exe file, so copy this codes and put it in CSProj file:

1
2
3
4
5
6
<!-- Copy the String folder to output directory -->
<ItemGroup>
<Content Include="Strings\**\*.resw">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

now in UIElement you need to set Uid:

1
2
3
4
5
6
<Page
xmlns:wuc="using:WinUICommunity">

<Button wuc:Localizer.Uid="myButtonId"/>

</Page>

Get Localization in Code Behind

1
txt.Text = Localizer.Get().GetLocalizedStrings("myButtonId").FirstOrDefault();

Change Language in Runtime

1
Localizer.Get().SetLanguage("en-US");

Multiple Resources

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.
include /<resources-file-name>/ before the string resource identifier.

1
<TextBlock wuc:Localizer.Uid="/Messages/ButtonFlyoutMessage" />

for more info please see Demo

WinUICommunity

0%