Today I had an issue where I wasn't able to access a Dictionary item that was setup in the Umbraco backoffice.
Before I go in to the details, I will outline that this is an Umbraco 8 project which has multiple languages associated to it. I have dictionaries setup under the Translations tab so that I can CMS values like "Email Subject" in English or Welsh.
To hopefully help explain the issue and also show my understanding, I'm going from an Umbraco page which has a form on it, for taking a donation, that posts a model out to the payment gateway. The payment gateway then posts back to an Umbraco controller and sends an email to the person who has made a payment.
Umbraco -> Gateway -> Umbraco
What I found was that when the gateway posted back to the endpoint, I had lost any reference to the culture of the page. This meant that when I tried to do :
string subject = Umbraco.GetDictionaryValue("Email.Subject");
The subject
would always be empty because Umbraco didn't have any culture set and/or the culture was defaulting to 'en-US' which I don't use on this site.
This is how I got around this, it might not be right but it worked for me and as always with these blogs, I'd appreciate any feedback if there is a better way to do this.
I firstly added a new value to the model that was being used by the donation page and I called it CurrentCulture
- then the model value was set when the form was submitted :
model.CurrentCulture = CurrentPage.GetCultureFromDomains();
Once I've completed a successful payment I'm redirected back to Umbraco and I hit this new endpoint.
private async Task SendDonationEmail(DonationRecordViewModel model, Guid paymentRef)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(model.CurrentCulture);
The key here is to set up the new CultureInfo and using the model to set the culture value. e.g. "en-GB" or "cy" for Welsh.
This now tells my controller what culture to use which now allows the GetDictionaryValue()
to work again.
Published on: 21 February 2023