adams85 / po

A library for reading and writing Gettext PO files.
MIT License
61 stars 16 forks source link

System.NullReferenceException for trivial input #5

Closed robert-j-engdahl closed 5 years ago

robert-j-engdahl commented 5 years ago

The code

POCatalog catalog = new POCatalog(new List<IPOEntry> { 
    new POSingularEntry(new POKey("MSGID", contextId: "MSGCTX")) });

var generator = new POGenerator(new POGeneratorSettings
{
    IgnoreEncoding = true,
});

TextWriter writer = new StringWriter(new StringBuilder());

generator.Generate(writer, catalog);

throws an exception with the following stacktrace:

System.NullReferenceException: Objektreferencen er ikke indstillet til en forekomst af et objekt.
   ved Karambolo.PO.POGenerator.BuildString(String value)
   ved Karambolo.PO.POGenerator.WriteEntry(IPOEntry entry)
   ved Karambolo.PO.POGenerator.Generate(TextWriter writer, POCatalog catalog)
   ved Tests.Internationalization.PotFileGenerationTest.FactMethodName() i 

and even if I find out how to solve this soon, it is still to be preferred with an informative exception about what I did wrong.

robert-j-engdahl commented 5 years ago

The problem appears to be that I am creating a .pot file and so I don't give a Translation. Also the Translation property on POSingularEntry wasn't in the constructor, so I think it should be expected to have a valid default value or at least cause a descriptive Exception.

Here is the change I needed to make:

POCatalog catalog = new POCatalog(new List<IPOEntry> { 
    new POSingularEntry(new POKey("MSGID", pluralId: "MSGIDPL", contextId: "MSGCTX")) 
    { 
        Translation = "" ,
    }, 
});
adams85 commented 5 years ago

Translation is a read-write property, hence not included in the ctor params.

I came to the decision not to throw an exception but to treat unset (null reference string) translations as empty strings when generating PO files. Unfortunately, unset translations can be added to POPluralEntry objects, too. Changing this behavior would be a BC I don't want to introduce.