Developer forum

Forum » Dynamicweb 9.0 Upgrade issues » Can't use Translate in class

Can't use Translate in class

Lars Larsen
Lars Larsen
Reply

Hi

I am upgrading a solution from DW9.3 to 9.6.5. Some custom code contains a custom product model. In this Translation() is used like shown below. But the code fails on v9.6.5 with this error:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Dynamicweb.Rendering.TemplateBase`1.get_Template()
   at Dynamicweb.Rendering.TemplateBase`1.Translate(String text, String defaultValue)

How can I fix it?

Capture.PNG

Replies

 
Nicolai Pedersen
Reply

HI Lars

I think it is because of a missing culture. are you in the context of a pageview? Would like to see your custom code... 

There is an overload to the translate that also takes a culture - try to use that.

BR Nicolai

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

I thought the same regarding the culture. Therefore I have already tried Translate(text, defaultvalue, culture) and culture set to "da-DK". But it fails with the same error. I am using the code from my first screenshot above in a Razor page template like this:

 

Capture.PNG
 
Nicolai Pedersen
Reply

Hm , why have a viewmodel and call that inside another viewmodel? Why is it not just a class?

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

Yeah I know. But this is old custom code made by another person, and I rather don't wont to change it to much. But it worked on v9.3.11

 
Nicolai Pedersen
Reply

Hi Lars

ok, but you cannot. You need to call the overload with 3 options - or change the product description viewmodel to not inherit viewmodel - as it is lacking context and we fixed a bug related to translations in viewmodel based templates that requires a template instance...

BR Nicolai

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

How can I call/use Translate() if I don't inherit from RazorTemplateBase<RazorTemplateModel<Template>> ?

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Why do you need ProductDescriptionViewModel? Why don't you just call the Translate method directly?

The ProductDescriptionViewModel in your example is not a view model. It inherits from RazorTemplateBase, so it represents a razor template instance.
You should avoid instantiating razor templates inside another razor template like this. Razor templates are instantiated by Dynamicweb when needed.

Anyway...

To make your code work you can change ProductDescriptionViewModel to a simple class and add a constructor which takes an instance of a RazorTemplateBase.
ProductDescriptionViewModel can then call the Translate method on that RazorTemplateBase...

public class ProductDescriptionViewModel
{
 
    public ProductDescriptionViewModel(RazorTemplateBase<RazorTemplateModel<Template>> templateBase)
    {
        this.TemplateBase = templateBase;
    }
 
    private RazorTemplateBase<RazorTemplateModel<Template>> TemplateBase { get; }
 
    public string TranslateProductDescription {
        get {
            return TemplateBase.Translate("(product) Description""Product Description");
        }
    }
}

Use it in your razor template like this...

@{
    var productDescriptionFooter = new ProductDescriptionViewModel(this);
}
 
<text>@productDescriptionFooter.TranslateProductDescription</text>

ProductDescriptionViewModel is still not a view model, but more like a helper class that doesn't help that much :)

Best regards,
Morten

 
Nicolai Pedersen
Reply

@ Lars: Just call the translate API directly:

https://doc.dynamicweb.com/api/html/edb97f59-4918-1edd-0f9b-70ca99d0c55d.htm

BR Nicolai

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai and Morten

I went  with Nicolais suggestion:

Dynamicweb.Rendering.Translation.Translation.GetTranslation("(product) Description", Thread.CurrentThread.CurrentCulture.Name, new Design(new DirectoryInfo("/Files/Template/Designs/[name]")))

Thank you both smiley

 

You must be logged in to post in the forum