Skip to content

Commit e295b49

Browse files
authored
[generator] Fix localization error. (#702)
In d664e90 we made `generator` localizable. One of the changes was this pattern: - Report.Warning (0, Report.WarningReturnValue + 0, "Unknown return type {0} {1}.", java_type, context.ContextString); + Report.LogCodedWarning (0, Report.WarningUnknownReturnType, java_type, context.ContextString); in which `Report.WarningUnknownReturnType` is: "Unknown return type '{0}' for member '{1}'." However `context.ContextString` generates a hard to localize string, e.g. in method CreateFromXml in managed type Android.Content.Res.ColorStateList resulting in messages such as: warning BG8800: Unknown return type 'System.Xml.XmlReader' for member 'in method CreateFromXml in managed type Android.Content.Res.ColorStateList'. This message doesn't "look right", nor is it itself localizable. Instead, add a `context.GetContextTypeMember ()` method which formats the member as a type signature, which doesn't need to be separately localized and "reads correctly" in context: warning BG8800: Unknown return type 'System.Xml.XmlReader' for member 'Android.Content.Res.ColorStateList.CreateFromXml(Android.Content.Res.Resources, System.Xml.XmlReader)'.
1 parent 5a0e37e commit e295b49

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

tools/generator/CodeGeneratorContext.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,17 @@ public class CodeGeneratorContext
1616
string ContextMethodString => ContextMethod != null ? "in method " + ContextMethod.Name + " " : null;
1717
string ContextTypeString => ContextType != null ? "in managed type " + ContextType.FullName : null;
1818
public string ContextString => ContextFieldString + ContextMethodString + ContextTypeString;
19+
20+
public string GetContextTypeMember ()
21+
{
22+
var output = ContextType?.FullName ?? string.Empty;
23+
24+
if (ContextMethod != null) {
25+
output += $"{ContextMethod.Name} ({string.Join (", ", ContextMethod?.Parameters.Select (p => p.InternalType).ToArray ())})";
26+
return output;
27+
}
28+
29+
return output + ContextField?.Name;
30+
}
1931
}
2032
}

tools/generator/Java.Interop.Tools.Generator.ObjectModel/Field.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList
3838
Symbol = opt.SymbolTable.Lookup (TypeName, type_params);
3939

4040
if (Symbol == null || !Symbol.Validate (opt, type_params, context)) {
41-
Report.LogCodedWarning (0, Report.WarningUnexpectedFieldType, TypeName, context.ContextString);
41+
Report.LogCodedWarning (0, Report.WarningUnexpectedFieldType, TypeName, context.GetContextTypeMember ());
4242
return false;
4343
}
4444

tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList
3838
foreach (var c in ConstraintExpressions) {
3939
var sym = opt.SymbolTable.Lookup (c, type_params);
4040
if (sym == null) {
41-
Report.LogCodedWarning (0, Report.WarningUnknownGenericConstraint, c, context.ContextString);
41+
Report.LogCodedWarning (0, Report.WarningUnknownGenericConstraint, c, context.GetContextTypeMember ());
4242
validated = true;
4343
return false;
4444
}

tools/generator/Java.Interop.Tools.Generator.ObjectModel/Parameter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public string Type {
224224
get { return managed_type ?? sym.FullName; }
225225
}
226226

227+
public string InternalType => managed_type ?? sym?.FullName ?? type;
228+
227229
public string Annotation { get; internal set; }
228230

229231
public void SetGeneratedEnumType (string enumType)
@@ -260,11 +262,11 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList
260262
{
261263
sym = opt.SymbolTable.Lookup (type, type_params);
262264
if (sym == null) {
263-
Report.LogCodedWarning (0, Report.WarningUnknownParameterType, type, context.ContextString);
265+
Report.LogCodedWarning (0, Report.WarningUnknownParameterType, type, context.GetContextTypeMember ());
264266
return false;
265267
}
266268
if (!sym.Validate (opt, type_params, context)) {
267-
Report.LogCodedWarning (0, Report.WarningInvalidParameterType, type, context.ContextString);
269+
Report.LogCodedWarning (0, Report.WarningInvalidParameterType, type, context.GetContextTypeMember ());
268270
return false;
269271
}
270272
return true;

tools/generator/Java.Interop.Tools.Generator.ObjectModel/ReturnValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList
121121
{
122122
sym = (IsEnumified ? opt.SymbolTable.Lookup (managed_type, type_params) : null) ?? opt.SymbolTable.Lookup (java_type, type_params);
123123
if (sym == null) {
124-
Report.LogCodedWarning (0, Report.WarningUnknownReturnType, java_type, context.ContextString);
124+
Report.LogCodedWarning (0, Report.WarningUnknownReturnType, java_type, context.GetContextTypeMember ());
125125
return false;
126126
}
127127
if (!sym.Validate (opt, type_params, context)) {
128-
Report.LogCodedWarning (0, Report.WarningInvalidReturnType, java_type, context.ContextString);
128+
Report.LogCodedWarning (0, Report.WarningInvalidReturnType, java_type, context.GetContextTypeMember ());
129129
return false;
130130
}
131131
return true;

0 commit comments

Comments
 (0)