Skip to content

Commit 9375ac3

Browse files
author
Kapil Borle
committed
Localize settings related error messages
1 parent fde62eb commit 9375ac3

File tree

3 files changed

+113
-74
lines changed

3 files changed

+113
-74
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,25 +248,37 @@ protected override void BeginProcessing()
248248
case SettingsMode.Auto:
249249
this.WriteVerbose(
250250
String.Format(
251-
"Settings not provided. Will look for settings file in the given path {0}.",
251+
CultureInfo.CurrentCulture,
252+
Strings.SettingsNotProvided,
252253
path));
253254
this.WriteVerbose(
254255
String.Format(
255-
"Found {0}. Will use it to provide settings for this invocation.",
256+
CultureInfo.CurrentCulture,
257+
Strings.SettingsAutoDiscovered,
256258
(string)settingsFound));
257259
break;
258260

259261
case SettingsMode.Preset:
260262
case SettingsMode.File:
261-
this.WriteVerbose(String.Format("Using settings file at {0}", (string)settingsFound));
263+
this.WriteVerbose(
264+
String.Format(
265+
CultureInfo.CurrentCulture,
266+
Strings.SettingsUsingFile,
267+
(string)settingsFound));
262268
break;
263269

264270
case SettingsMode.Hashtable:
265-
this.WriteVerbose(String.Format("Using settings hashtable."));
271+
this.WriteVerbose(
272+
String.Format(
273+
CultureInfo.CurrentCulture,
274+
Strings.SettingsUsingHashtable));
266275
break;
267276

268277
default: // case SettingsMode.None
269-
this.WriteVerbose(String.Format("Cannot find a settings file."));
278+
this.WriteVerbose(
279+
String.Format(
280+
CultureInfo.CurrentCulture,
281+
Strings.SettingsCannotFindFile));
270282
break;
271283
}
272284

@@ -279,7 +291,7 @@ protected override void BeginProcessing()
279291
}
280292
catch
281293
{
282-
this.WriteWarning("Cannot parse settings. Will abort the invocation.");
294+
this.WriteWarning(String.Format(CultureInfo.CurrentCulture, Strings.SettingsNotParsable));
283295
stopProcessing = true;
284296
return;
285297
}
@@ -318,7 +330,8 @@ protected override void ProcessRecord()
318330
ScriptAnalyzer.Instance.ModuleHandler = moduleHandler;
319331
this.WriteVerbose(
320332
String.Format(
321-
"Temporary module location: {0}",
333+
CultureInfo.CurrentCulture,
334+
Strings.ModuleDepHandlerTempLocation,
322335
moduleHandler.TempModulePath));
323336
ProcessInput();
324337
}

Engine/Generic/Settings.cs

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ public Settings(object settings, Func<string, string> presetResolver)
6969
}
7070
else
7171
{
72-
throw new ArgumentException(String.Format("File does not exist: {0}", settingsFilePath));
72+
throw new ArgumentException(
73+
String.Format(
74+
CultureInfo.CurrentCulture,
75+
Strings.InvalidPath,
76+
settingsFilePath));
7377
}
7478
}
7579
else
@@ -81,7 +85,7 @@ public Settings(object settings, Func<string, string> presetResolver)
8185
}
8286
else
8387
{
84-
throw new ArgumentException("Input object should either be a string or a hashtable");
88+
throw new ArgumentException(Strings.SettingsInvalidType);
8589
}
8690
}
8791
}
@@ -168,29 +172,24 @@ private Dictionary<string, object> GetDictionaryFromHashtable(Hashtable hashtabl
168172
string key = obj as string;
169173
if (key == null)
170174
{
171-
// TODO localize
172-
throw new InvalidDataException("key not string");
173-
// writer.WriteError(
174-
// new ErrorRecord(
175-
// new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.KeyNotString, key)),
176-
// Strings.ConfigurationKeyNotAString,
177-
// ErrorCategory.InvalidData,
178-
// hashtable));
179-
// hasError = true;
175+
throw new InvalidDataException(
176+
string.Format(
177+
CultureInfo.CurrentCulture,
178+
Strings.KeyNotString,
179+
key));
180180
}
181+
181182
var valueHashtableObj = hashtable[obj];
182183
if (valueHashtableObj == null)
183184
{
184-
throw new InvalidDataException("wrong hash table value");
185-
// writer.WriteError(
186-
// new ErrorRecord(
187-
// new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueHashTable, valueHashtableObj, key)),
188-
// Strings.WrongConfigurationKey,
189-
// ErrorCategory.InvalidData,
190-
// hashtable));
191-
// hasError = true;
192-
// return null;
185+
throw new InvalidDataException(
186+
string.Format(
187+
CultureInfo.CurrentCulture,
188+
Strings.WrongValueHashTable,
189+
"",
190+
key));
193191
}
192+
194193
var valueHashtable = valueHashtableObj as Hashtable;
195194
if (valueHashtable == null)
196195
{
@@ -221,17 +220,11 @@ private List<string> GetData(object val, string key)
221220
if (val == null)
222221
{
223222
throw new InvalidDataException(
224-
String.Format(
225-
"value should be a string or string array for {0} key",
223+
string.Format(
224+
CultureInfo.CurrentCulture,
225+
Strings.WrongValueHashTable,
226+
"",
226227
key));
227-
// writer.WriteError(
228-
// new ErrorRecord(
229-
// new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueHashTable, value, key)),
230-
// Strings.WrongConfigurationKey,
231-
// ErrorCategory.InvalidData,
232-
// profile));
233-
// hasError = true;
234-
// break;
235228
}
236229

237230
List<string> values = new List<string>();
@@ -260,21 +253,23 @@ private List<string> GetData(object val, string key)
260253
}
261254
else
262255
{
263-
throw new InvalidDataException("array items should be of string type");
264-
// writer.WriteError(
265-
// new ErrorRecord(
266-
// new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueHashTable, val, key)),
267-
// Strings.WrongConfigurationKey,
268-
// ErrorCategory.InvalidData,
269-
// profile));
270-
// hasError = true;
271-
// break;
256+
throw new InvalidDataException(
257+
string.Format(
258+
CultureInfo.CurrentCulture,
259+
Strings.WrongValueHashTable,
260+
val,
261+
key));
272262
}
273263
}
274264
}
275265
else
276266
{
277-
throw new InvalidDataException("array items should be of string type");
267+
throw new InvalidDataException(
268+
string.Format(
269+
CultureInfo.CurrentCulture,
270+
Strings.WrongValueHashTable,
271+
val,
272+
key));
278273
}
279274
}
280275

@@ -285,21 +280,17 @@ private List<string> GetData(object val, string key)
285280
/// Sets the arguments for consumption by rules
286281
/// </summary>
287282
/// <param name="ruleArgs">A hashtable with rule names as keys</param>
288-
private Dictionary<string, Dictionary<string, object>> ConvertToRuleArgumentType(object ruleArguments)
283+
private Dictionary<string, Dictionary<string, object>> ConvertToRuleArgumentType(object ruleArguments)
289284
{
290285
var ruleArgs = ruleArguments as Dictionary<string, object>;
291286
if (ruleArgs == null)
292287
{
293-
throw new ArgumentException(
294-
"input should be a dictionary",
295-
"ruleArguments");
288+
throw new ArgumentException(Strings.SettingsInputShouldBeDictionary, nameof(ruleArguments));
296289
}
297290

298291
if (ruleArgs.Comparer != StringComparer.OrdinalIgnoreCase)
299292
{
300-
throw new ArgumentException(
301-
"Input dictionary should have OrdinalIgnoreCase comparer.",
302-
"ruleArguments");
293+
throw new ArgumentException(Strings.SettingsDictionaryShouldBeCaseInsesitive, nameof(ruleArguments));
303294
}
304295

305296
var ruleArgsDict = new Dictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
@@ -308,9 +299,7 @@ private Dictionary<string, Dictionary<string, object>> ConvertToRuleArgumentTyp
308299
var argsDict = ruleArgs[rule] as Dictionary<string, object>;
309300
if (argsDict == null)
310301
{
311-
throw new ArgumentException(
312-
"input should be a dictionary",
313-
"ruleArguments");
302+
throw new InvalidDataException(Strings.SettingsInputShouldBeDictionary);
314303
}
315304
ruleArgsDict[rule] = argsDict;
316305
}
@@ -341,19 +330,25 @@ private void parseSettingsHashtable(Hashtable settingsHashtable)
341330
break;
342331

343332
case "rules":
344-
ruleArguments = ConvertToRuleArgumentType(val);
333+
try
334+
{
335+
ruleArguments = ConvertToRuleArgumentType(val);
336+
}
337+
catch (ArgumentException argumentException)
338+
{
339+
throw new InvalidDataException(
340+
string.Format(CultureInfo.CurrentCulture, Strings.WrongValueHashTable, "", key),
341+
argumentException);
342+
}
343+
345344
break;
346345

347346
default:
348-
throw new InvalidDataException(String.Format("Invalid key: {0}", key));
349-
// writer.WriteError(
350-
// new ErrorRecord(
351-
// new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongKeyHashTable, key)),
352-
// Strings.WrongConfigurationKey,
353-
// ErrorCategory.InvalidData,
354-
// profile));
355-
// hasError = true;
356-
// break;
347+
throw new InvalidDataException(
348+
string.Format(
349+
CultureInfo.CurrentCulture,
350+
Strings.WrongKeyHashTable,
351+
key));
357352
}
358353
}
359354
}
@@ -368,10 +363,7 @@ private void parseSettingsFile(string settingsFilePath)
368363
// no hashtable, raise warning
369364
if (hashTableAsts.Count() == 0)
370365
{
371-
throw new ArgumentException("Given file does not contain a hashtable");
372-
// writer.WriteError(new ErrorRecord(new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, profile)),
373-
// Strings.ConfigurationFileHasNoHashTable, ErrorCategory.ResourceUnavailable, profile));
374-
// hasError = true;
366+
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, settingsFilePath));
375367
}
376368

377369
HashtableAst hashTableAst = hashTableAsts.First() as HashtableAst;
@@ -384,12 +376,16 @@ private void parseSettingsFile(string settingsFilePath)
384376
}
385377
catch (InvalidOperationException e)
386378
{
387-
throw new ArgumentException("input file has invalid hashtable", e);
379+
throw new ArgumentException(Strings.InvalidProfile, e);
388380
}
389381

390382
if (hashtable == null)
391383
{
392-
throw new ArgumentException("input file has invalid hashtable");
384+
throw new ArgumentException(
385+
String.Format(
386+
CultureInfo.CurrentCulture,
387+
Strings.InvalidProfile,
388+
settingsFilePath));
393389
}
394390

395391
parseSettingsHashtable(hashtable);

Engine/Strings.resx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,37 @@
244244
<value>{0} is not a valid key in the settings hashtable. Valid keys are ExcludeRules, IncludeRules and Severity.</value>
245245
</data>
246246
<data name="WrongValueHashTable" xml:space="preserve">
247-
<value>Value {0} for key {1} has the wrong data type. Value in the settings hashtable should be a string or an array of strings.</value>
247+
<value>Value {0} for key {1} has the wrong data type.</value>
248+
</data>
249+
<data name="SettingsInputShouldBeDictionary" xml:space="preserve">
250+
<value>Input should be a dictionary type.</value>
251+
</data>
252+
<data name="SettingsDictionaryShouldBeCaseInsesitive" xml:space="preserve">
253+
<value>Dictionary should be indexable in a case-insensitive manner.</value>
254+
</data>
255+
<data name="SettingsInvalidType" xml:space="preserve">
256+
<value>Settings should be either a file path, built-in preset or a hashtable.</value>
257+
</data>
258+
<data name="SettingsNotProvided" xml:space="preserve">
259+
<value>Settings not provided. Will look for settings file in the given path {0}.</value>
260+
</data>
261+
<data name="SettingsAutoDiscovered" xml:space="preserve">
262+
<value>Found {0}. Will use it to provide settings for this invocation.</value>
263+
</data>
264+
<data name="SettingsUsingFile" xml:space="preserve">
265+
<value>Using settings file at {0}.</value>
266+
</data>
267+
<data name="SettingsUsingHashtable" xml:space="preserve">
268+
<value>Using settings hashtable.</value>
269+
</data>
270+
<data name="SettingsCannotFindFile" xml:space="preserve">
271+
<value>Cannot find a settings file.</value>
272+
</data>
273+
<data name="SettingsNotParsable" xml:space="preserve">
274+
<value>Cannot parse settings. Will abort the invocation.</value>
275+
</data>
276+
<data name="ModuleDepHandlerTempLocation" xml:space="preserve">
277+
<value>Temporary module location: {0}.</value>
248278
</data>
249279
<data name="DigraphVertexAlreadyExists" xml:space="preserve">
250280
<value>Vertex {0} already exists! Cannot add it to the digraph.</value>

0 commit comments

Comments
 (0)