Skip to content

A Swiss Army knife for developers.

Added ReswPlus

Etienne Baudoux95f6a86a8ec6
diff --git a/src/app/dev/DevToys.UI.EntryPoint/App.xaml.cs b/src/app/dev/DevToys.UI.EntryPoint/App.xaml.cs
index be89ae1cb..e8a041c88 100644
--- a/src/app/dev/DevToys.UI.EntryPoint/App.xaml.cs
+++ b/src/app/dev/DevToys.UI.EntryPoint/App.xaml.cs
@@ -1,4 +1,5 @@
-using DevToys.UI.Views;
+using DevToys.UI;
+using DevToys.UI.Views;
using Microsoft.Extensions.Logging;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
@@ -34,6 +35,10 @@ public sealed partial class App : Application
{
InitializeLogging();
+ // Set the language of the app for startup. By default, it's the same than Windows, or english.
+ // The language defined by the user will be applied later, once MEF is loaded, but before the UI shows up.
+ LanguageManager.Instance.SetCurrentCulture(LanguageManager.Instance.AvailableLanguages[0]);
+
this.InitializeComponent();
#if HAS_UNO || NETFX_CORE
diff --git a/src/app/dev/DevToys.UI.Framework/MarkupExtensions/StringResourceExtension.cs b/src/app/dev/DevToys.UI.Framework/MarkupExtensions/StringResourceExtension.cs
deleted file mode 100644
index 539dc84b3..000000000
--- a/src/app/dev/DevToys.UI.Framework/MarkupExtensions/StringResourceExtension.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-#if WINDOWS_UWP
-#nullable enable
-using Windows.UI.Xaml.Markup;
-#else
-using Microsoft.UI.Xaml.Markup;
-#endif
-
-using Windows.ApplicationModel.Resources;
-
-namespace DevToys.UI.Framework.MarkupExtensions;
-
-[MarkupExtensionReturnType(ReturnType = typeof(string))]
-public sealed class StringResource : MarkupExtension
-{
- private static readonly ResourceLoader resourceLoader = new("DevToys.UI.Framework/Strings");
-
- public StringResource() { }
-
- public string Key { get; set; } = "";
-
- protected override object ProvideValue()
- {
- return resourceLoader.GetString(Key);
- }
-}
diff --git a/src/app/dev/DevToys.UI.Framework/Themes/Generic.xaml b/src/app/dev/DevToys.UI.Framework/Themes/Generic.xaml
index 0c595d3a1..be7ae2212 100644
--- a/src/app/dev/DevToys.UI.Framework/Themes/Generic.xaml
+++ b/src/app/dev/DevToys.UI.Framework/Themes/Generic.xaml
@@ -1,7 +1,7 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:ext="using:DevToys.UI.Framework.MarkupExtensions">
+ xmlns:str="using:DevToys.UI.Framework.Strings">
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
@@ -47,8 +47,8 @@
</Style>
<Style TargetType="ToggleSwitch">
- <Setter Property="OnContent" Value="{ext:StringResource Key=ToggleSwitchOn}"/>
- <Setter Property="OffContent" Value="{ext:StringResource Key=ToggleSwitchOff}"/>
+ <Setter Property="OnContent" Value="{str:StringsExtension Key=ToggleSwitchOn}"/>
+ <Setter Property="OffContent" Value="{str:StringsExtension Key=ToggleSwitchOff}"/>
</Style>
<!-- From https://github.com/morning4coffe-dev/storylines -->
@@ -64,8 +64,8 @@
<Setter Property="MinWidth" Value="0" />
<Setter Property="FocusVisualMargin" Value="-7,-3,-7,-3" />
<Setter Property="CornerRadius" Value="{StaticResource ControlCornerRadius}" />
- <Setter Property="OnContent" Value="{ext:StringResource Key=ToggleSwitchOn}"/>
- <Setter Property="OffContent" Value="{ext:StringResource Key=ToggleSwitchOff}"/>
+ <Setter Property="OnContent" Value="{str:StringsExtension Key=ToggleSwitchOn}"/>
+ <Setter Property="OffContent" Value="{str:StringsExtension Key=ToggleSwitchOff}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleSwitch">
diff --git a/src/app/dev/DevToys.UI/DevToys.UI.csproj b/src/app/dev/DevToys.UI/DevToys.UI.csproj
index 349d62e07..bf67ccfaf 100644
--- a/src/app/dev/DevToys.UI/DevToys.UI.csproj
+++ b/src/app/dev/DevToys.UI/DevToys.UI.csproj
@@ -91,6 +91,7 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Common" />
<PackageReference Include="CommunityToolkit.Diagnostics" />
+ <PackageReference Include="CommunityToolkit.Mvvm" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" />
<PackageReference Include="System.ComponentModel.Composition" />
diff --git a/src/app/dev/DevToys.UI/LanguageDefinition.cs b/src/app/dev/DevToys.UI/LanguageDefinition.cs
new file mode 100644
index 000000000..f64a484d0
--- /dev/null
+++ b/src/app/dev/DevToys.UI/LanguageDefinition.cs
@@ -0,0 +1,56 @@
+using System.Globalization;
+
+namespace DevToys.UI;
+
+/// <summary>
+/// Represents un language supported by the app.
+/// </summary>
+public class LanguageDefinition
+{
+ /// <summary>
+ /// Unique internal name.
+ /// </summary>
+ public string InternalName { get; }
+
+ /// <summary>
+ /// Unique internal ID used to identify the language.
+ /// </summary>
+ public string Identifier { get; }
+
+ /// <summary>
+ /// The name of the language displayed to the user.
+ /// </summary>
+ public string DisplayName { get; }
+
+ /// <summary>
+ /// The culture to apply.
+ /// </summary>
+ public CultureInfo Culture { get; }
+
+ public LanguageDefinition()
+ : this(null)
+ {
+ }
+
+ public LanguageDefinition(string? identifier)
+ {
+ if (string.IsNullOrEmpty(identifier))
+ {
+#if HAS_UNO_WASM
+ Culture = CultureInfo.CurrentUICulture;
+#else
+ Culture = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);
+#endif
+ DisplayName = Strings.Languages.DefaultLanguage;
+ InternalName = "default";
+ }
+ else
+ {
+ Culture = new CultureInfo(identifier!);
+ DisplayName = Culture.NativeName;
+ InternalName = Culture.Name;
+ }
+
+ Identifier = Culture.Name;
+ }
+}
diff --git a/src/app/dev/DevToys.UI/LanguageManager.cs b/src/app/dev/DevToys.UI/LanguageManager.cs
new file mode 100644
index 000000000..32ec4498a
--- /dev/null
+++ b/src/app/dev/DevToys.UI/LanguageManager.cs
@@ -0,0 +1,62 @@
+using System.Globalization;
+using Windows.Globalization;
+
+#if WINDOWS_UWP
+using Windows.UI.Xaml;
+#else
+using Microsoft.UI.Xaml;
+#endif
+
+namespace DevToys.UI;
+
+public sealed class LanguageManager
+{
+ private static LanguageManager? languageManager;
+
+ /// <summary>
+ /// Gets an instance of <see cref="LanguageManager"/>.
+ /// </summary>
+ public static LanguageManager Instance => languageManager ??= new LanguageManager();
+
+ /// <summary>
+ /// Gets if the text must be written from left to right or from right to left.
+ /// </summary>
+ public FlowDirection FlowDirection { get; private set; }
+
+ /// <summary>
+ /// Gets the list of available languages in the app.
+ /// </summary>
+ public List<LanguageDefinition> AvailableLanguages { get; }
+
+ public LanguageManager()
+ {
+ AvailableLanguages = new List<LanguageDefinition>
+ {
+ new LanguageDefinition() // default language
+ };
+ IReadOnlyList<string> supportedLanguageIdentifiers = ApplicationLanguages.ManifestLanguages;
+ for (int i = 0; i < supportedLanguageIdentifiers.Count; i++)
+ {
+ AvailableLanguages.Add(new LanguageDefinition(supportedLanguageIdentifiers[i]));
+ }
+ }
+
+ /// <summary>
+ /// Change the current culture of the application
+ /// </summary>
+ public void SetCurrentCulture(LanguageDefinition language)
+ {
+ CultureInfo.DefaultThreadCurrentCulture = language.Culture;
+ CultureInfo.DefaultThreadCurrentUICulture = language.Culture;
+ ApplicationLanguages.PrimaryLanguageOverride = language.Identifier;
+
+ if (language.Culture.TextInfo.IsRightToLeft)
+ {
+ FlowDirection = FlowDirection.RightToLeft;
+ }
+ else
+ {
+ FlowDirection = FlowDirection.LeftToRight;
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.UI/Strings/en-US/Resources.resw b/src/app/dev/DevToys.UI/Strings/en-US/Languages.resw
similarity index 98%
rename from src/app/dev/DevToys.UI/Strings/en-US/Resources.resw
rename to src/app/dev/DevToys.UI/Strings/en-US/Languages.resw
index ab12f3980..d8ec3b95e 100644
--- a/src/app/dev/DevToys.UI/Strings/en-US/Resources.resw
+++ b/src/app/dev/DevToys.UI/Strings/en-US/Languages.resw
@@ -117,7 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
- <data name="ApplicationName" xml:space="preserve">
- <value>DevToys</value>
+ <data name="DefaultLanguage" xml:space="preserve">
+ <value>Same as Microsoft Windows</value>
</data>
</root>
\ No newline at end of file
diff --git a/src/app/dev/Directory.Build.props b/src/app/dev/Directory.Build.props
index 635f096c1..f7a07b570 100644
--- a/src/app/dev/Directory.Build.props
+++ b/src/app/dev/Directory.Build.props
@@ -1,5 +1,6 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
+ <Import Project="$(RepoRoot)\tools\ReswPlus.0.1.5-alpha\ReswPlus.props" />
<PropertyGroup>
<!-- Define .NET and C# configuration -->
@@ -24,4 +25,8 @@
<Compile Include="$(RepoRoot)\src\app\dev\shared\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
<Compile Include="$(RepoRoot)\src\app\dev\shared\SharedAssemblyVersion.cs" Link="Properties\SharedAssemblyVersion.cs" />
</ItemGroup>
+
+ <ItemGroup>
+ <Analyzer Include="$(RepoRoot)\tools\ReswPlus.0.1.5-alpha\ReswPlusSourceGenerator.dll" />
+ </ItemGroup>
</Project>
\ No newline at end of file
diff --git a/src/app/dev/platforms/desktop/windows/DevToys.UWP.Core/DevToys.UWP.Core.csproj b/src/app/dev/platforms/desktop/windows/DevToys.UWP.Core/DevToys.UWP.Core.csproj
index 52a75e5e5..99e48333d 100644
--- a/src/app/dev/platforms/desktop/windows/DevToys.UWP.Core/DevToys.UWP.Core.csproj
+++ b/src/app/dev/platforms/desktop/windows/DevToys.UWP.Core/DevToys.UWP.Core.csproj
@@ -86,6 +86,7 @@
<!-- Explicitely set Version because Central Package Management isn't supported by UWP projects -->
<PackageReference Include="CommunityToolkit.Common" Version="$(CommunityToolkitVersion)" />
<PackageReference Include="CommunityToolkit.Diagnostics" Version="$(CommunityToolkitVersion)" />
+ <PackageReference Include="CommunityToolkit.Mvvm" Version="$(CommunityToolkitVersion)" />
<PackageReference Include="System.ComponentModel.Composition" Version="$(DotNetVersion)" />
<PackageReference Include="Microsoft.Web.WebView2" Version="$(WebView2Version)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(DotNetVersion)" />
diff --git a/tools/ReswPlus.0.1.5-alpha/README.md b/tools/ReswPlus.0.1.5-alpha/README.md
new file mode 100644
index 000000000..c6d922702
--- /dev/null
+++ b/tools/ReswPlus.0.1.5-alpha/README.md
@@ -0,0 +1,18 @@
+C# Source generator built from https://github.com/DotNetPlus/ReswPlus/tree/sourceGenerator
+This binary contains a tiny change to the usings generated [here](https://github.com/DotNetPlus/ReswPlus/blob/c94e47bbc1deff1569345c680e1017984120da3c/src/ReswPlus.Shared/CodeGenerators/CsharpCodeGenerator.cs#L49-L52) that set the proper namespace for non-UWP apps.
+
+In the end, it generates the following:
+
+```
+// File generated automatically by ReswPlus. https://github.com/DotNetPlus/ReswPlus
+// The NuGet package ReswPlusLib is necessary to support Pluralization.
+using System;
+using Windows.ApplicationModel.Resources;
+#if WINDOWS_UWP
+using Windows.UI.Xaml.Markup;
+using Windows.UI.Xaml.Data;
+#else
+using Microsoft.UI.Xaml.Markup;
+using Microsoft.UI.Xaml.Data;
+#endif
+```
\ No newline at end of file
diff --git a/tools/ReswPlus.0.1.5-alpha/ReswPlus.props b/tools/ReswPlus.0.1.5-alpha/ReswPlus.props
new file mode 100644
index 000000000..3c29de223
--- /dev/null
+++ b/tools/ReswPlus.0.1.5-alpha/ReswPlus.props
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <CompilerVisibleProperty Include="MSBuildProjectFullPath" />
+ <CompilerVisibleProperty Include="RootNamespace" />
+ <CompilerVisibleProperty Include="DefaultLanguage" />
+ <CompilerVisibleProperty Include="OutputType" />
+ </ItemGroup>
+ <PropertyGroup>
+ <AdditionalFileItemNames>$(AdditionalFileItemNames);PRIResource</AdditionalFileItemNames>
+ </PropertyGroup>
+</Project>
\ No newline at end of file
diff --git a/tools/ReswPlus.0.1.5-alpha/ReswPlusSourceGenerator.dll b/tools/ReswPlus.0.1.5-alpha/ReswPlusSourceGenerator.dll
new file mode 100644
index 000000000..152964205
Binary files /dev/null and b/tools/ReswPlus.0.1.5-alpha/ReswPlusSourceGenerator.dll differ