Skip to content

Commit c9e4eec

Browse files
authored
Support localization for parser messages (#457)
1 parent 7d0f945 commit c9e4eec

File tree

4 files changed

+417
-9
lines changed

4 files changed

+417
-9
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
namespace Microsoft.WingetCreateCLI
5+
{
6+
/* This implementation is taken from the CommandLineParser reference examples.
7+
* https://github.com/commandlineparser/commandline/tree/master/demo/ReadText.LocalizedDemo
8+
*/
9+
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using CommandLine;
14+
using CommandLine.Text;
15+
16+
/// <inheritdoc/>
17+
public class LocalizableSentenceBuilder : SentenceBuilder
18+
{
19+
/// <inheritdoc/>
20+
public override Func<string> RequiredWord
21+
{
22+
get { return () => Properties.Resources.SentenceRequiredWord; }
23+
}
24+
25+
/// <inheritdoc/>
26+
public override Func<string> ErrorsHeadingText
27+
{
28+
// Cannot be pluralized
29+
get { return () => Properties.Resources.SentenceErrorsHeadingText; }
30+
}
31+
32+
/// <inheritdoc/>
33+
public override Func<string> UsageHeadingText
34+
{
35+
get { return () => Properties.Resources.SentenceUsageHeadingText; }
36+
}
37+
38+
/// <inheritdoc/>
39+
public override Func<string> OptionGroupWord
40+
{
41+
get { return () => Properties.Resources.SentenceOptionGroupWord; }
42+
}
43+
44+
/// <inheritdoc/>
45+
public override Func<bool, string> HelpCommandText
46+
{
47+
get
48+
{
49+
return isOption => isOption
50+
? Properties.Resources.SentenceHelpCommandTextOption
51+
: Properties.Resources.SentenceHelpCommandTextVerb;
52+
}
53+
}
54+
55+
/// <inheritdoc/>
56+
public override Func<bool, string> VersionCommandText
57+
{
58+
get { return _ => Properties.Resources.SentenceVersionCommandText; }
59+
}
60+
61+
/// <inheritdoc/>
62+
public override Func<Error, string> FormatError
63+
{
64+
get
65+
{
66+
return error =>
67+
{
68+
switch (error.Tag)
69+
{
70+
case ErrorType.BadFormatTokenError:
71+
return string.Format(Properties.Resources.SentenceBadFormatTokenError, ((BadFormatTokenError)error).Token);
72+
73+
case ErrorType.MissingValueOptionError:
74+
return string.Format(Properties.Resources.SentenceMissingValueOptionError, ((MissingValueOptionError)error).NameInfo.NameText);
75+
76+
case ErrorType.UnknownOptionError:
77+
return string.Format(Properties.Resources.SentenceUnknownOptionError, ((UnknownOptionError)error).Token);
78+
79+
case ErrorType.MissingRequiredOptionError:
80+
var errMisssing = (MissingRequiredOptionError)error;
81+
return errMisssing.NameInfo.Equals(NameInfo.EmptyName) ? Properties.Resources.SentenceMissingRequiredValueError
82+
: string.Format(Properties.Resources.SentenceMissingRequiredOptionError, errMisssing.NameInfo.NameText);
83+
84+
case ErrorType.BadFormatConversionError:
85+
var badFormat = (BadFormatConversionError)error;
86+
return badFormat.NameInfo.Equals(NameInfo.EmptyName) ? Properties.Resources.SentenceBadFormatConversionErrorValue
87+
: string.Format(Properties.Resources.SentenceBadFormatConversionErrorOption, badFormat.NameInfo.NameText);
88+
89+
case ErrorType.SequenceOutOfRangeError:
90+
var seqOutRange = (SequenceOutOfRangeError)error;
91+
return seqOutRange.NameInfo.Equals(NameInfo.EmptyName) ? Properties.Resources.SentenceSequenceOutOfRangeErrorValue
92+
: string.Format(Properties.Resources.SentenceSequenceOutOfRangeErrorOption, seqOutRange.NameInfo.NameText);
93+
94+
case ErrorType.BadVerbSelectedError:
95+
return string.Format(Properties.Resources.SentenceBadVerbSelectedError, ((BadVerbSelectedError)error).Token);
96+
97+
case ErrorType.NoVerbSelectedError:
98+
return Properties.Resources.SentenceNoVerbSelectedError;
99+
100+
case ErrorType.RepeatedOptionError:
101+
return string.Format(Properties.Resources.SentenceRepeatedOptionError, ((RepeatedOptionError)error).NameInfo.NameText);
102+
103+
case ErrorType.SetValueExceptionError:
104+
var setValueError = (SetValueExceptionError)error;
105+
return string.Format(Properties.Resources.SentenceSetValueExceptionError, setValueError.NameInfo.NameText, setValueError.Exception.Message);
106+
}
107+
108+
throw new InvalidOperationException();
109+
};
110+
}
111+
}
112+
113+
/// <inheritdoc/>
114+
public override Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors
115+
{
116+
get
117+
{
118+
return errors =>
119+
{
120+
var bySet = from e in errors
121+
group e by e.SetName into g
122+
select new { SetName = g.Key, Errors = g.ToList() };
123+
124+
var msgs = bySet.Select(
125+
set =>
126+
{
127+
var names = string.Join(
128+
string.Empty,
129+
(from e in set.Errors select string.Format("'{0}', ", e.NameInfo.NameText)).ToArray());
130+
var namesCount = set.Errors.Count();
131+
132+
var incompat = string.Join(
133+
string.Empty,
134+
(from x in
135+
(from s in bySet where !s.SetName.Equals(set.SetName) from e in s.Errors select e)
136+
.Distinct()
137+
select string.Format("'{0}', ", x.NameInfo.NameText)).ToArray());
138+
139+
// TODO: Pluralize by namesCount
140+
return string.Format(
141+
Properties.Resources.SentenceMutuallyExclusiveSetErrors,
142+
names.Substring(0, names.Length - 2),
143+
incompat.Substring(0, incompat.Length - 2));
144+
}).ToArray();
145+
return string.Join(Environment.NewLine, msgs);
146+
};
147+
}
148+
}
149+
}
150+
}

src/WingetCreateCLI/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private static async Task<int> Main(string[] args)
2727
Logger.Initialize();
2828
UserSettings.FirstRunTelemetryConsent();
2929
TelemetryEventListener.EventListener.IsTelemetryEnabled();
30+
SentenceBuilder.Factory = () => new LocalizableSentenceBuilder();
3031

3132
string arguments = string.Join(' ', Environment.GetCommandLineArgs());
3233
Logger.Trace($"Command line args: {arguments}");

src/WingetCreateCLI/Properties/Resources.Designer.cs

Lines changed: 189 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)