diff --git a/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs b/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs index 0ac371c5..b22349e5 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs @@ -4,6 +4,7 @@ // using System; +using System.Globalization; namespace Microsoft.SqlTools.ServiceLayer.Utility { @@ -18,6 +19,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility public CommandOptions(string[] args) { ErrorMessage = string.Empty; + Locale = string.Empty; try { @@ -26,18 +28,31 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility string arg = args[i]; if (arg.StartsWith("--") || arg.StartsWith("-")) { + // Extracting arguments and properties arg = arg.Substring(1).ToLowerInvariant(); - switch (arg) + string argName = arg; + string argProperty = ""; + int splitIndex = arg.IndexOf(' '); + if (splitIndex > 0) + { + argName = arg.Substring(0, splitIndex); + argProperty = arg.Substring(splitIndex + 1); + } + + switch (argName) { case "-enable-logging": EnableLogging = true; break; + case "-locale": + setLocale(argProperty); + break; case "h": case "-help": ShouldExit = true; return; default: - ErrorMessage += String.Format("Unknown argument \"{0}\"" + Environment.NewLine, arg); + ErrorMessage += String.Format("Unknown argument \"{0}\" with property \"{1}\"" + Environment.NewLine, argName, argProperty); break; } } @@ -71,6 +86,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility /// public bool ShouldExit { get; private set; } + /// + /// The locale our we should instantiate this service in + /// + public string Locale { get; private set; } + /// /// Get the usage string describing command-line arguments for the program /// @@ -82,10 +102,32 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility "Microsoft.SqlTools.ServiceLayer.exe " + Environment.NewLine + " Options:" + Environment.NewLine + " [--enable-logging]" + Environment.NewLine + - " [--help]" + Environment.NewLine, + " [--help]" + Environment.NewLine + + " [--locale **] (default: 'en')" + Environment.NewLine, ErrorMessage); return str; } } + + private void setLocale(string locale){ + try + { + // Creating cultureInfo from our given locale + CultureInfo language = new CultureInfo(locale); + Locale = locale; + + // Setting our language globally + CultureInfo.CurrentCulture = language; + CultureInfo.CurrentUICulture = language; + + // Setting our internal SR culture to our global culture + SR.Culture = CultureInfo.CurrentCulture; + } + catch(Exception ex) + { + // Warn user of invalid locale, and fall back to english + Console.WriteLine(ex); + } + } } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/sr.cs b/src/Microsoft.SqlTools.ServiceLayer/sr.cs index f45c58a1..2168d490 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/sr.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/sr.cs @@ -421,6 +421,14 @@ namespace Microsoft.SqlTools.ServiceLayer } } + public static string TestLocalizationConstant + { + get + { + return Keys.GetString(Keys.TestLocalizationConstant); + } + } + public static string ConnectionServiceListDbErrorNotConnected(string uri) { return Keys.GetString(Keys.ConnectionServiceListDbErrorNotConnected, uri); @@ -672,6 +680,9 @@ namespace Microsoft.SqlTools.ServiceLayer public const string WorkspaceServiceBufferPositionOutOfOrder = "WorkspaceServiceBufferPositionOutOfOrder"; + public const string TestLocalizationConstant = "TestLocalizationConstant"; + + private Keys() { } diff --git a/src/Microsoft.SqlTools.ServiceLayer/sr.es.resx b/src/Microsoft.SqlTools.ServiceLayer/sr.es.resx new file mode 100644 index 00000000..b1657f70 --- /dev/null +++ b/src/Microsoft.SqlTools.ServiceLayer/sr.es.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + ES_LOCALIZATION + + \ No newline at end of file diff --git a/src/Microsoft.SqlTools.ServiceLayer/sr.resx b/src/Microsoft.SqlTools.ServiceLayer/sr.resx index f50b8318..3c065e9c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/sr.resx +++ b/src/Microsoft.SqlTools.ServiceLayer/sr.resx @@ -373,4 +373,8 @@ . Parameters: 0 - sLine (int), 1 - sCol (int), 2 - eLine (int), 3 - eCol (int) + + EN_LOCALIZATION + + diff --git a/src/Microsoft.SqlTools.ServiceLayer/sr.strings b/src/Microsoft.SqlTools.ServiceLayer/sr.strings index 13a58333..14ab937e 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/sr.strings +++ b/src/Microsoft.SqlTools.ServiceLayer/sr.strings @@ -175,4 +175,10 @@ WorkspaceServicePositionLineOutOfRange = Position is outside of file line range WorkspaceServicePositionColumnOutOfRange(int line) = Position is outside of column range for line {0} -WorkspaceServiceBufferPositionOutOfOrder(int sLine, int sCol, int eLine, int eCol) = Start position ({0}, {1}) must come before or be equal to the end position ({2}, {3}) \ No newline at end of file +WorkspaceServiceBufferPositionOutOfOrder(int sLine, int sCol, int eLine, int eCol) = Start position ({0}, {1}) must come before or be equal to the end position ({2}, {3}) + + +############################################################################ +# Workspace Service + +TestLocalizationConstant = EN_LOCALIZATION \ No newline at end of file diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/SrTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/SrTests.cs index 38f46228..15fff2af 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/SrTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/ServiceHost/SrTests.cs @@ -2,6 +2,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using Microsoft.SqlTools.ServiceLayer.Utility; using Xunit; namespace Microsoft.SqlTools.ServiceLayer.Test.ServiceHost @@ -47,5 +48,44 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.ServiceHost var queryServiceQueryFailed = SR.QueryServiceQueryFailed(".."); var workspaceServiceBufferPositionOutOfOrder = SR.WorkspaceServiceBufferPositionOutOfOrder(1, 2, 3, 4); } + + [Fact] + public void SrStringsTestWithEnLocalization() + { + string locale = "en"; + var args = new string[] { "--locale " + locale }; + CommandOptions options = new CommandOptions(args); + Assert.Equal(SR.Culture.Name, options.Locale); + Assert.Equal(options.Locale, locale); + + var TestLocalizationConstant = SR.TestLocalizationConstant; + Assert.Equal(TestLocalizationConstant, "EN_LOCALIZATION"); + } + + [Fact] + public void SrStringsTestWithEsLocalization() + { + string locale = "es"; + var args = new string[] { "--locale " + locale }; + CommandOptions options = new CommandOptions(args); + Assert.Equal(SR.Culture.Name, options.Locale); + Assert.Equal(options.Locale, locale); + + var TestLocalizationConstant = SR.TestLocalizationConstant; + Assert.Equal(TestLocalizationConstant, "ES_LOCALIZATION"); + } + + [Fact] + public void SrStringsTestWithNullLocalization() + { + SR.Culture = null; + var args = new string[] { "" }; + CommandOptions options = new CommandOptions(args); + Assert.Null(SR.Culture); + Assert.Equal(options.Locale, ""); + + var TestLocalizationConstant = SR.TestLocalizationConstant; + Assert.Equal(TestLocalizationConstant, "EN_LOCALIZATION"); + } } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs index 0e2cdeec..1d239552 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs @@ -22,6 +22,7 @@ namespace Microsoft.SqlTools.Test.Utility Assert.True(options.EnableLogging); Assert.False(options.ShouldExit); + Assert.Equal(options.Locale, string.Empty); } [Fact] @@ -33,6 +34,7 @@ namespace Microsoft.SqlTools.Test.Utility Assert.False(options.EnableLogging); Assert.False(options.ShouldExit); + Assert.Equal(options.Locale, string.Empty); } [Fact] @@ -43,6 +45,7 @@ namespace Microsoft.SqlTools.Test.Utility Assert.NotNull(options); Assert.True(options.ShouldExit); + Assert.Equal(options.Locale, string.Empty); } [Fact] @@ -53,6 +56,7 @@ namespace Microsoft.SqlTools.Test.Utility Assert.NotNull(options); Assert.True(options.ShouldExit); + Assert.Equal(options.Locale, string.Empty); } [Fact] @@ -61,9 +65,49 @@ namespace Microsoft.SqlTools.Test.Utility var args = new string[] {}; CommandOptions options = new CommandOptions(args); Assert.NotNull(options); - + Assert.False(options.EnableLogging); Assert.False(options.ShouldExit); + Assert.Equal(options.Locale, string.Empty); + } + + [Theory] + [InlineData("en")] + [InlineData("es")] + public void LocaleSetWhenProvided(string locale) + { + var args = new string[] {"--locale " + locale}; + CommandOptions options = new CommandOptions(args); + + // Asserting all options were properly set + Assert.NotNull(options); + Assert.False(options.ShouldExit); + Assert.Equal(options.Locale, locale); + } + + [Fact] + public void ShouldExitSetWhenInvalidLocale() + { + string locale = "invalid"; + var args = new string[] { "--locale " + locale }; + CommandOptions options = new CommandOptions(args); + + // Asserting all options were properly set + Assert.NotNull(options); + Assert.False(options.ShouldExit); + } + + [Fact] + public void LocaleNotSetWhenNotProvided() + { + var args = new string[] {}; + CommandOptions options = new CommandOptions(args); + + // Asserting all options were properly set + Assert.NotNull(options); + Assert.False(options.EnableLogging); + Assert.False(options.ShouldExit); + Assert.Equal(options.Locale, string.Empty); } } }