diff --git a/csharp/Platform.IO.Tests/ConsoleHelpersTests.cs b/csharp/Platform.IO.Tests/ConsoleHelpersTests.cs new file mode 100644 index 0000000..2774a13 --- /dev/null +++ b/csharp/Platform.IO.Tests/ConsoleHelpersTests.cs @@ -0,0 +1,68 @@ +using System; +using System.Globalization; +using System.Threading; +using Xunit; + +namespace Platform.IO.Tests +{ + public class ConsoleHelpersTests + { + [Fact] + public void GetOrReadArgumentUsesLocalizedFormat() + { + var result = ConsoleHelpers.GetOrReadArgument(0, "arg1"); + Assert.Equal("arg1", result); + } + + [Fact] + public void GetOrReadArgumentWithIndexUsesLocalizedFormat() + { + var result = ConsoleHelpers.GetOrReadArgument(0, "arg1"); + Assert.Equal("arg1", result); + } + + [Fact] + public void ResourcesReturnLocalizedStrings() + { + var originalCulture = Thread.CurrentThread.CurrentUICulture; + + try + { + Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); + Assert.Equal("Press any key to continue.", Resources.PressAnyKeyToContinue); + Assert.Equal("{0} argument", Resources.ArgumentPrompt); + Assert.Equal("{0}: ", Resources.InputPrompt); + + Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru"); + Assert.Equal("Нажмите любую клавишу для продолжения.", Resources.PressAnyKeyToContinue); + Assert.Equal("{0} аргумент", Resources.ArgumentPrompt); + Assert.Equal("{0}: ", Resources.InputPrompt); + } + finally + { + Thread.CurrentThread.CurrentUICulture = originalCulture; + } + } + + [Fact] + public void ArgumentPromptFormattingWorks() + { + var originalCulture = Thread.CurrentThread.CurrentUICulture; + + try + { + Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); + var englishPrompt = string.Format(Resources.ArgumentPrompt, 1); + Assert.Equal("1 argument", englishPrompt); + + Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru"); + var russianPrompt = string.Format(Resources.ArgumentPrompt, 1); + Assert.Equal("1 аргумент", russianPrompt); + } + finally + { + Thread.CurrentThread.CurrentUICulture = originalCulture; + } + } + } +} \ No newline at end of file diff --git a/csharp/Platform.IO/ConsoleHelpers.cs b/csharp/Platform.IO/ConsoleHelpers.cs index bb811f2..f03420d 100644 --- a/csharp/Platform.IO/ConsoleHelpers.cs +++ b/csharp/Platform.IO/ConsoleHelpers.cs @@ -19,7 +19,7 @@ public static class ConsoleHelpers [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void PressAnyKeyToContinue() { - Console.WriteLine("Press any key to continue."); + Console.WriteLine(Resources.PressAnyKeyToContinue); Console.ReadKey(); } @@ -40,7 +40,7 @@ public static void PressAnyKeyToContinue() /// Значение с указанным , извлечённое из массива , или введённое пользователем в консоли. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string GetOrReadArgument(int index, params string[] args) => GetOrReadArgument(index, $"{index + 1} argument", args); + public static string GetOrReadArgument(int index, params string[] args) => GetOrReadArgument(index, string.Format(Resources.ArgumentPrompt, index + 1), args); /// /// Gets an argument's value with the specified from the array and if it's absent requests a user to input it in the console. @@ -67,7 +67,7 @@ public static string GetOrReadArgument(int index, string readMessage, params str { if (!args.TryGetElement(index, out string result)) { - Console.Write($"{readMessage}: "); + Console.Write(string.Format(Resources.InputPrompt, readMessage)); result = Console.ReadLine(); } if (string.IsNullOrEmpty(result)) diff --git a/csharp/Platform.IO/Platform.IO.csproj b/csharp/Platform.IO/Platform.IO.csproj index 266f821..7b55fe6 100644 --- a/csharp/Platform.IO/Platform.IO.csproj +++ b/csharp/Platform.IO/Platform.IO.csproj @@ -4,7 +4,7 @@ LinksPlatform's Platform.IO Class Library Konstantin Diachenko Platform.IO - 0.5.1 + 0.6.0 Konstantin Diachenko net8 Platform.IO @@ -23,8 +23,9 @@ true snupkg latest - Update target framework from net7 to net8. + Add internationalization support for ConsoleHelpers messages. enable + false @@ -36,4 +37,20 @@ + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + + + True + True + Resources.resx + + + diff --git a/csharp/Platform.IO/Resources.Designer.cs b/csharp/Platform.IO/Resources.Designer.cs new file mode 100644 index 0000000..770f063 --- /dev/null +++ b/csharp/Platform.IO/Resources.Designer.cs @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Platform.IO { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Platform.IO.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to {0} argument. + /// + public static string ArgumentPrompt { + get { + return ResourceManager.GetString("ArgumentPrompt", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to {0}: . + /// + public static string InputPrompt { + get { + return ResourceManager.GetString("InputPrompt", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Press any key to continue.. + /// + public static string PressAnyKeyToContinue { + get { + return ResourceManager.GetString("PressAnyKeyToContinue", resourceCulture); + } + } + } +} \ No newline at end of file diff --git a/csharp/Platform.IO/Resources.resx b/csharp/Platform.IO/Resources.resx new file mode 100644 index 0000000..e28344b --- /dev/null +++ b/csharp/Platform.IO/Resources.resx @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Press any key to continue. + Message displayed to user asking them to press any key + + + {0} argument + Format string for prompting user for argument number (e.g., "1 argument", "2 argument") + + + {0}: + Format string for input prompt (e.g., "1 argument: ", "filename: ") + + \ No newline at end of file diff --git a/csharp/Platform.IO/Resources.ru.resx b/csharp/Platform.IO/Resources.ru.resx new file mode 100644 index 0000000..52022e3 --- /dev/null +++ b/csharp/Platform.IO/Resources.ru.resx @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Нажмите любую клавишу для продолжения. + Message displayed to user asking them to press any key + + + {0} аргумент + Format string for prompting user for argument number (e.g., "1 аргумент", "2 аргумент") + + + {0}: + Format string for input prompt (e.g., "1 аргумент: ", "имя файла: ") + + \ No newline at end of file