Skip to content

Commit d861134

Browse files
Merge pull request #28 from AndrewKeepCoding/v2.0.0
v2.0.0
2 parents b2c6f49 + 65821b2 commit d861134

21 files changed

+659
-163
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Add this ItemGroup in the project file (\*.csproj) of your app.
5858
.SetOptions(options =>
5959
{
6060
options.DefaultLanguage = "en-US";
61-
options.UseUidWhenLocalizedStringNotFound = true;
6261
})
6362
.Build();
6463
}
@@ -89,7 +88,6 @@ Add this ItemGroup in the project file (\*.csproj) of your app.
8988
.SetOptions(options =>
9089
{
9190
options.DefaultLanguage = "en-US";
92-
options.UseUidWhenLocalizedStringNotFound = true;
9391
})
9492
.Build();
9593
}
@@ -119,21 +117,28 @@ Add this ItemGroup in the project file (\*.csproj) of your app.
119117

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

122-
First, we need to asign an `Uid` to the `Button`.
120+
First asign an `Uid` to the `Button`, then in each language resources file, add an item that corresponds to the `Uid`.
121+
122+
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.
123+
To just need to include `/<resources-file-name>/` before the string resource identifier.
123124

124125
```xml
125126
<Page
126127
x:Class="WinUI3Localizer.SampleApp.TestPage"
127128
...
128129
xmlns:l="using:WinUI3Localizer">
129130
<StackPanel>
130-
<Button l:Uids.Uid="TestPage_Button" />
131+
<Button l:Uids.Uid="TestPage_Button">
132+
<Button.Flyout>
133+
<Flyout>
134+
<TextBlock l:Uids.Uid="/Messages/ButtonFlyoutMessage" />
135+
</Flyout>
136+
</Button.Flyout>
137+
</Button>
131138
</StackPanel>
132139
</Page>
133140
```
134141

135-
Then in each language resources file, we need to add an item that corresponds to the `Uid`.
136-
137142
- en-US
138143

139144
- Resources.resw
@@ -142,6 +147,12 @@ Then in each language resources file, we need to add an item that corresponds to
142147
| ---- | ----- |
143148
| TestPageButton.Content | Awesome! |
144149

150+
- Messages.resw
151+
152+
| Name | Value |
153+
| ---- | ----- |
154+
| ButtonFlyoutMessage.Text | This is an awesome message! |
155+
145156
- es-ES:
146157

147158
- Resources.resw
@@ -150,6 +161,12 @@ Then in each language resources file, we need to add an item that corresponds to
150161
| ---- | ----- |
151162
| TestPageButton.Content | ¡Increíble! |
152163

164+
- Messages.resw
165+
166+
| Name | Value |
167+
| ---- | ----- |
168+
| ButtonFlyoutMessage.Text | ¡Esto es un mensaje increíble! |
169+
153170
- ja:
154171

155172
- Resources.resw
@@ -158,6 +175,12 @@ Then in each language resources file, we need to add an item that corresponds to
158175
| ---- | ----- |
159176
| TestPageButton.Content | 素晴らしい! |
160177

178+
- Messages.resw
179+
180+
| Name | Value |
181+
| ---- | ----- |
182+
| ButtonFlyoutMessage.Text | これは素晴らしいメッセージです! |
183+
161184
### **Getting localized strings**
162185

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

WinUI3Localizer.SampleApp/App.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
</ResourceDictionary.MergedDictionaries>
1414
<!-- Other app resources here -->
1515
<converters:NullableBooleanToBooleanConverter x:Key="NullableBooleanToBooleanConverter" />
16+
<Style
17+
x:Key="PageStackPanelStyle"
18+
TargetType="StackPanel">
19+
<Setter Property="Spacing" Value="10" />
20+
</Style>
1621
<Style TargetType="local:ExamplePresenter">
17-
<Setter Property="Margin" Value="5" />
1822
<Setter Property="CornerRadius" Value="5" />
1923
<Setter Property="FontSize" Value="12" />
2024
<Setter Property="Background" Value="{ThemeResource CardBackgroundFillColorDefaultBrush}" />

WinUI3Localizer.SampleApp/App.xaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ private static IHost BuildHost()
9898
// .SetOptions(options =>
9999
// {
100100
// options.DefaultLanguage = "ja";
101-
// options.UseUidWhenLocalizedStringNotFound = true;
102101
// })
103102
// .Build()
104103
// .GetAwaiter()
@@ -125,10 +124,12 @@ private async Task InitializeLocalizer()
125124
StringsFolderPath = stringsFolder.Path;
126125

127126
// Create string resources file from app resources if doesn't exists.
128-
string resourceFileName = "Resources.resw";
129-
await MakeSureStringResourceFileExists(stringsFolder, "en-US", resourceFileName);
130-
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", resourceFileName);
131-
await MakeSureStringResourceFileExists(stringsFolder, "ja", resourceFileName);
127+
await MakeSureStringResourceFileExists(stringsFolder, "en-US", "Resources.resw");
128+
await MakeSureStringResourceFileExists(stringsFolder, "en-US", "ErrorMessages.resw");
129+
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", "Resources.resw");
130+
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", "ErrorMessages.resw");
131+
await MakeSureStringResourceFileExists(stringsFolder, "ja", "Resources.resw");
132+
await MakeSureStringResourceFileExists(stringsFolder, "ja", "ErrorMessages.resw");
132133
#endif
133134

134135
ILocalizer localizer = await new LocalizerBuilder()
@@ -139,7 +140,6 @@ private async Task InitializeLocalizer()
139140
.SetOptions(options =>
140141
{
141142
options.DefaultLanguage = "en-US";
142-
options.UseUidWhenLocalizedStringNotFound = true;
143143
})
144144
//.AddLocalizationAction(new LocalizationActionItem(typeof(Hyperlink), arguments =>
145145
//{

WinUI3Localizer.SampleApp/ControlsPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1010
mc:Ignorable="d">
1111
<ScrollViewer>
12-
<StackPanel>
12+
<StackPanel Style="{StaticResource PageStackPanelStyle}">
1313
<!-- TextBlock -->
1414
<local:ExamplePresenter HeaderText="TextBlock">
1515
<TextBlock

WinUI3Localizer.SampleApp/ItemTemplatesPage.xaml

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,56 @@
77
xmlns:local="using:WinUI3Localizer.SampleApp"
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
mc:Ignorable="d">
10-
<Grid>
11-
<local:ExamplePresenter HeaderText="ItemsRepeater">
12-
<ScrollViewer Grid.Row="1">
13-
<ItemsRepeater ItemsSource="{x:Bind People}">
14-
<ItemsRepeater.ItemTemplate>
15-
<DataTemplate x:DataType="local:Person">
16-
<Grid
17-
ColumnDefinitions="*,*,*"
18-
RowDefinitions="Auto,Auto">
19-
<TextBlock
20-
Grid.Row="0"
21-
Grid.Column="0"
22-
l:Uids.Uid="ItemTemplatesPage_ID"
23-
FontSize="10"
24-
Foreground="DimGray"
25-
PointerEntered="LocalizedItem_PointerEntered" />
26-
<TextBlock
27-
Grid.Row="0"
28-
Grid.Column="1"
29-
l:Uids.Uid="ItemTemplatesPage_FirstName"
30-
FontSize="10"
31-
Foreground="DimGray"
32-
PointerEntered="LocalizedItem_PointerEntered" />
33-
<TextBlock
34-
Grid.Row="0"
35-
Grid.Column="2"
36-
l:Uids.Uid="ItemTemplatesPage_LastName"
37-
FontSize="10"
38-
Foreground="DimGray"
39-
PointerEntered="LocalizedItem_PointerEntered" />
40-
<TextBlock
41-
Grid.Row="1"
42-
Grid.Column="0"
43-
Text="{x:Bind ID}" />
44-
<TextBlock
45-
Grid.Row="1"
46-
Grid.Column="1"
47-
Text="{x:Bind FirstName}" />
48-
<TextBlock
49-
Grid.Row="1"
50-
Grid.Column="2"
51-
Text="{x:Bind LastName}" />
52-
</Grid>
53-
</DataTemplate>
54-
</ItemsRepeater.ItemTemplate>
55-
</ItemsRepeater>
56-
</ScrollViewer>
57-
<local:ExamplePresenter.XamlSampleCode>
58-
<x:String xml:space="preserve">&lt;ItemsRepeater ItemsSource="{x:Bind People}"&gt;
10+
<ScrollViewer>
11+
<StackPanel Style="{StaticResource PageStackPanelStyle}">
12+
<local:ExamplePresenter HeaderText="ItemsRepeater">
13+
<ScrollViewer Grid.Row="1">
14+
<ItemsRepeater ItemsSource="{x:Bind People}">
15+
<ItemsRepeater.ItemTemplate>
16+
<DataTemplate x:DataType="local:Person">
17+
<Grid
18+
ColumnDefinitions="*,*,*"
19+
RowDefinitions="Auto,Auto">
20+
<TextBlock
21+
Grid.Row="0"
22+
Grid.Column="0"
23+
l:Uids.Uid="ItemTemplatesPage_ID"
24+
FontSize="10"
25+
Foreground="DimGray"
26+
PointerEntered="LocalizedItem_PointerEntered" />
27+
<TextBlock
28+
Grid.Row="0"
29+
Grid.Column="1"
30+
l:Uids.Uid="ItemTemplatesPage_FirstName"
31+
FontSize="10"
32+
Foreground="DimGray"
33+
PointerEntered="LocalizedItem_PointerEntered" />
34+
<TextBlock
35+
Grid.Row="0"
36+
Grid.Column="2"
37+
l:Uids.Uid="ItemTemplatesPage_LastName"
38+
FontSize="10"
39+
Foreground="DimGray"
40+
PointerEntered="LocalizedItem_PointerEntered" />
41+
<TextBlock
42+
Grid.Row="1"
43+
Grid.Column="0"
44+
Text="{x:Bind ID}" />
45+
<TextBlock
46+
Grid.Row="1"
47+
Grid.Column="1"
48+
Text="{x:Bind FirstName}" />
49+
<TextBlock
50+
Grid.Row="1"
51+
Grid.Column="2"
52+
Text="{x:Bind LastName}" />
53+
</Grid>
54+
</DataTemplate>
55+
</ItemsRepeater.ItemTemplate>
56+
</ItemsRepeater>
57+
</ScrollViewer>
58+
<local:ExamplePresenter.XamlSampleCode>
59+
<x:String xml:space="preserve">&lt;ItemsRepeater ItemsSource="{x:Bind People}"&gt;
5960
&lt;ItemsRepeater.ItemTemplate&gt;
6061
&lt;DataTemplate x:DataTipe="local:Person"&gt;
6162
&lt;Grid ColumnDefinitions="*,*,*" RowDefinitions="Auto,Auto"&gt;
@@ -73,7 +74,8 @@
7374
&lt;/ItemsRepeater.ItemTemplate&gt;
7475
&lt;/ItemsRepeater&gt;
7576
</x:String>
76-
</local:ExamplePresenter.XamlSampleCode>
77-
</local:ExamplePresenter>
78-
</Grid>
77+
</local:ExamplePresenter.XamlSampleCode>
78+
</local:ExamplePresenter>
79+
</StackPanel>
80+
</ScrollViewer>
7981
</Page>

WinUI3Localizer.SampleApp/LocalizationActionsPage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
xmlns:local="using:WinUI3Localizer.SampleApp"
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
mc:Ignorable="d">
10-
<ScrollViewer Margin="5">
11-
<StackPanel>
10+
<ScrollViewer>
11+
<StackPanel Style="{StaticResource PageStackPanelStyle}">
1212
<TextBlock
1313
l:Uids.Uid="LocalizationActionsPage_Description"
1414
TextWrapping="Wrap" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Page
2+
x:Class="WinUI3Localizer.SampleApp.MultipleResourcesPage"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:l="using:WinUI3Localizer"
7+
xmlns:local="using:WinUI3Localizer.SampleApp"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
<ScrollViewer>
11+
<StackPanel Style="{StaticResource PageStackPanelStyle}">
12+
<TextBlock
13+
l:Uids.Uid="MultipleResourcesPage_Description"
14+
TextWrapping="Wrap" />
15+
<!-- ErrorMessages.resw -->
16+
<local:ExamplePresenter HeaderText="ErrorMessages.resw">
17+
<TextBlock
18+
l:Uids.Uid="/ErrorMessages/ErrorMessageExample"
19+
PointerEntered="LocalizedItem_PointerEntered" />
20+
<local:ExamplePresenter.XamlSampleCode>
21+
<x:String xml:space="preserve">&lt;TextBlock l:Uids.Uid="/ErrorMessages/ErrorMessageExample" /&gt;</x:String>
22+
</local:ExamplePresenter.XamlSampleCode>
23+
</local:ExamplePresenter>
24+
</StackPanel>
25+
</ScrollViewer>
26+
</Page>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.UI.Xaml.Controls;
2+
using Microsoft.UI.Xaml.Input;
3+
4+
namespace WinUI3Localizer.SampleApp;
5+
6+
public sealed partial class MultipleResourcesPage : Page, IHasLocalizedItem
7+
{
8+
public MultipleResourcesPage()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
public event PointerEventHandler? LocalizedItemPointerEntered;
14+
15+
private void LocalizedItem_PointerEntered(object sender, PointerRoutedEventArgs e)
16+
{
17+
LocalizedItemPointerEntered?.Invoke(sender, e);
18+
}
19+
}

WinUI3Localizer.SampleApp/SettingsPage.xaml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
xmlns:local="using:WinUI3Localizer.SampleApp"
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
mc:Ignorable="d">
10-
<StackPanel Spacing="20" Margin="5">
11-
<StackPanel>
12-
<TextBlock
13-
l:Uids.Uid="SettingsPage_StringResourcesFolder"
14-
PointerEntered="LocalizedItem_PointerEntered"
15-
Style="{StaticResource SubtitleTextBlockStyle}"
16-
Text="Strings resources folder" />
17-
<TextBlock TextWrapping="Wrap">
18-
<Hyperlink x:Name="StringResourcesFolderHyperLink">None</Hyperlink>
19-
</TextBlock>
10+
<ScrollViewer>
11+
<StackPanel Style="{StaticResource PageStackPanelStyle}">
12+
<StackPanel>
13+
<TextBlock
14+
l:Uids.Uid="SettingsPage_StringResourcesFolder"
15+
PointerEntered="LocalizedItem_PointerEntered"
16+
Style="{StaticResource SubtitleTextBlockStyle}"
17+
Text="Strings resources folder" />
18+
<TextBlock TextWrapping="Wrap">
19+
<Hyperlink x:Name="StringResourcesFolderHyperLink">None</Hyperlink>
20+
</TextBlock>
21+
</StackPanel>
2022
</StackPanel>
21-
</StackPanel>
23+
</ScrollViewer>
2224
</Page>

WinUI3Localizer.SampleApp/ShellPage.xaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
l:Uids.Uid="MainWindow_NavigationView_LocalizationActionsPage"
3434
Icon="PreviewLink"
3535
Tag="LocalizationActionsPage" />
36+
<NavigationViewItem
37+
l:Uids.Uid="MainWindow_NavigationView_MultipleResourcesPage"
38+
Icon="Bullets"
39+
Tag="MultipleResourcesPage" />
3640
</NavigationView.MenuItems>
3741
<NavigationView.PaneCustomContent>
3842
<StackPanel
@@ -60,13 +64,13 @@
6064
<SplitView
6165
DisplayMode="Inline"
6266
IsPaneOpen="{x:Bind ShowStringResourcesToggleSwitch.IsChecked, Mode=OneWay, Converter={StaticResource NullableBooleanToBooleanConverter}}"
63-
OpenPaneLength="600"
67+
OpenPaneLength="500"
6468
PaneBackground="Transparent"
6569
PanePlacement="Right">
6670
<SplitView.Pane>
6771
<Grid
6872
Grid.Column="1"
69-
Margin="10"
73+
Margin="20,10"
7074
RowDefinitions="Auto,*">
7175
<TextBlock
7276
Grid.Row="0"
@@ -98,7 +102,7 @@
98102
<Frame
99103
x:Name="ContentFrame"
100104
Grid.Column="0"
101-
Margin="10" />
105+
Margin="20,10" />
102106
</Grid>
103107
</SplitView>
104108
</NavigationView>

0 commit comments

Comments
 (0)