Skip to content

Commit

Permalink
Merge pull request #45 from AndrewKeepCoding/sample-app-refactoring
Browse files Browse the repository at this point in the history
Sample app refactoring
  • Loading branch information
AndrewKeepCoding authored May 20, 2024
2 parents 8068353 + b3ac875 commit b8fc0a9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
55 changes: 30 additions & 25 deletions WinUI3Localizer.SampleApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public App()

protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
await InitializeLocalizer();
await InitializeWinUI3Localizer();

this.window = Host.Services.GetRequiredService<MainWindow>();
this.window.Activate();
Expand Down Expand Up @@ -107,34 +107,16 @@ private static IHost BuildHost()
.Build();
}

/// <summary>
/// Creates default Resources.resw files for WinUI3Localizer.
/// </summary>
private async Task InitializeLocalizer()
private static async Task InitializeWinUI3Localizer()
{
#if IS_NON_PACKAGED
// Initialize a "Strings" folder in the executables folder.
StringsFolderPath = Path.Combine(AppContext.BaseDirectory, "Strings");
StorageFolder stringsFolder = await StorageFolder.GetFolderFromPathAsync(StringsFolderPath);
PrepareEnvironmentForWinUI3LocalizerOnNonPackagedApp();
#else
// Initialize a "Strings" folder in the "LocalFolder" for the packaged app.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder stringsFolder = await localFolder.CreateFolderAsync("Strings", CreationCollisionOption.OpenIfExists);
StringsFolderPath = stringsFolder.Path;

// Create string resources file from app resources if doesn't exists.
await MakeSureStringResourceFileExists(stringsFolder, "en-US", "Resources.resw");
await MakeSureStringResourceFileExists(stringsFolder, "en-US", "ErrorMessages.resw");
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", "Resources.resw");
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", "ErrorMessages.resw");
await MakeSureStringResourceFileExists(stringsFolder, "ja", "Resources.resw");
await MakeSureStringResourceFileExists(stringsFolder, "ja", "ErrorMessages.resw");
await PrepareEnvironmentForWinUI3LocalizerOnPackagedApp();
#endif

ILocalizer localizer = await new LocalizerBuilder()
.AddPriResourcesForLanguageDictionaries(new[] { "en-US", "es-ES", "ja" } )
.AddPriResourcesForLanguageDictionaries(new[] { "en-US", "es-ES", "ja" }, "ErrorMessages")
//.AddStringResourcesFolderForLanguageDictionaries(StringsFolderPath)
_ = await new LocalizerBuilder()
.AddStringResourcesFolderForLanguageDictionaries(StringsFolderPath)
//.SetLogger(Host.Services
// .GetRequiredService<ILoggerFactory>()
// .CreateLogger<Localizer>())
Expand All @@ -159,4 +141,27 @@ private async Task InitializeLocalizer()
//}))
.Build();
}
}
#if IS_NON_PACKAGED
private static void PrepareEnvironmentForWinUI3LocalizerOnNonPackagedApp()
{
// Initialize a "Strings" folder in the executables folder.
StringsFolderPath = Path.Combine(AppContext.BaseDirectory, "Strings");
}
#else
private static async Task PrepareEnvironmentForWinUI3LocalizerOnPackagedApp()
{
// Initialize a "Strings" folder in the "LocalFolder" for the packaged app.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder stringsFolder = await localFolder.CreateFolderAsync("Strings", CreationCollisionOption.OpenIfExists);
StringsFolderPath = stringsFolder.Path;

// Create string resources file from app resources if doesn't exists.
await MakeSureStringResourceFileExists(stringsFolder, "en-US", "Resources.resw");
await MakeSureStringResourceFileExists(stringsFolder, "en-US", "ErrorMessages.resw");
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", "Resources.resw");
await MakeSureStringResourceFileExists(stringsFolder, "es-ES", "ErrorMessages.resw");
await MakeSureStringResourceFileExists(stringsFolder, "ja", "Resources.resw");
await MakeSureStringResourceFileExists(stringsFolder, "ja", "ErrorMessages.resw");
}
#endif
}
28 changes: 14 additions & 14 deletions WinUI3Localizer.SampleApp/WinUI3Localizer.SampleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>WinUI3Localizer.SampleApp</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
<LangVersion>latest</LangVersion>
Expand Down Expand Up @@ -43,18 +43,18 @@

<ItemGroup>
<PackageReference Include="ColorCode.WinUI" Version="2.0.15" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.DataGrid" Version="7.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230822000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down

0 comments on commit b8fc0a9

Please sign in to comment.