Developer forum

Forum » Templates » Getting translation from other language in Razor template

Getting translation from other language in Razor template

Kasper Vesth
Reply

Hi guys

We are having an issue where we want to be able to swap some parts of the website from Danish to English for instance. We are not able to simply use different Areas as it has to be that specific content connected with the Danish Area, just with text translated in English if the users wishes.

The question is then: Is it possible at all to be at an Area using the Danish language (LANG1) and get a translation from another language (for instance LANG5)?

If not it would also be appreciated if someone could mention another way to get around this issue :)

The use case is an internal webshop where people from Germany gets hired in Denmark and needs to order office supplies from the Danish shop (but preferably in English).

Regards

Kasper Vesth


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Kasper

If you have products in the catalog in 2 languages, and publishes the catalog on 1 website, you can do this simply.

Just add ?languageid=LANG5 to the URL and it changes the users context language.

Nicolai

 
Kasper Vesth
Reply

Thank you, that solves it for the product information, but we also want to be able to change the texts in the checkout flow and the "AddToCart"-button. Generally speaking everywhere you would use translation tags in your template it would be nice if we could swap the language they used somehow. Without switching out the Area.

 
Nicolai Høeg Pedersen
Reply

Translations, dateformats etc. are controlled by the regional settings on the website.

They cannot be overwridden using url paramters.

You can do it custom though. When the pageview is loaded (PageView.Load), it sets the culture. You can make a notification subscriber listening to the Page Loaded notifcation:

Notifications.Standard.Page.Loaded

and when that fires, set the culture to any valid Globalization.CultureInfo string. I.e.:

Dim ci As New System.Globalization.CultureInfo("en-US")
System.Threading.Thread.CurrentThread.CurrentCulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture = ci

Instead of hard coding the culture info string, get it from querystring and store it on the sesssion for further requests from that user...

 
Kasper Vesth
Reply

I can't seem to get it to work. I have tried both with the

Notifications.Standard.Page.Loaded

and with

Notifications.Standard.Application.BeginRequest

to see if I needed to put if earlier in the pipeline. No matter which one I use I can't get the Razor template to output something different when using the tag

Translate("addToCartButton")

I can see that it has switched the culture, but it doesn't affect the translations it fetches. The translation file looks like this:

  <key name="addToCartButton">
    <translation culture="da-DK"><![CDATA[LÆG I KURV]]></translation>
    <translation culture="nb-NO"><![CDATA[]]></translation>
    <translation culture="et-EE"><![CDATA[]]></translation>
    <translation culture="fi-FI"><![CDATA[]]></translation>
    <translation culture="lt-LT"><![CDATA[]]></translation>
    <translation culture="lv-LV"><![CDATA[]]></translation>
    <translation culture="en-GB"><![CDATA[ADD TO CART]]></translation>
    <translation culture="nn-NO"><![CDATA[]]></translation>
    <translation culture="sv-SE"><![CDATA[]]></translation>
  </key>

and I try to get the value for "en-GB".

[Subscribe(Dynamicweb.Notifications.Standard.Page.Loaded)]
    public class ChangeLanguageNotificationSubscriber : NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["lang"]))
            {
                CultureInfo ci = new System.Globalization.CultureInfo("en-GB");
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            }
        }
    }

It still prints the danish version though.

 
Kasper Vesth
Reply

It runs on a Professional version 8.5.1.1 btw

 
Kasper Vesth
Reply

Anyone got any insight on this now? :) If it's not possible we will ofc just do something else. It would make sense if it wasn't doable, but would be nice if there was a solution I could get to work.

 
Nicolai Høeg Pedersen
Reply

It is currently not possible. We would need to make a change to PageView that gives you the possibility to set the culture into the user context and have Dynamicweb load that instead of the website culture.

We can make that change for you - but it will not make it until sometimes next week at the earliest. We have a TC coming up!

 
Kasper Vesth
Reply

Sure, I can easily wait a bit :) It would be a pretty powerful change to Dynamicweb so it would be beautiful if you could make it work. And have fun with your conference. I am unfortunately not going.

 
Nicolai Høeg Pedersen
Reply

You are the first ever to ask for it, so I don't know how powerful it would be for others... smiley.

Why translate the templates when the rest of the content is not translated?

Anyways, I've looked into the matter. The template object makes translation using the context of the area on the Pageview. And changing that behavior would be out of scope right now since we would need to change som internal logic.

But I've added a Translate method on RazorTemplateBase which is your context in Razor template.

Then you can use Translate(key, defaultText, cultureName):

var addtoCarttext = Translate("addtocarttext", "Add product to cart", "lt-LT")
<button>@addtoCarttext</button>

Coming in 8.6.1

Then you have to keep track of the chosen culture your self, or use the ecommerce context culture to get the translations.

Hope this a useful solution for you.

BR Nicolai

 
Kasper Vesth
Reply

That is pretty much exactly what I need :) The important parts for the customers to get translated are the checkout flow which mostly consists of translate tags. It's a pretty small internal site with no real text pages, but used by many people of varying nationalities, so that is why we have the weird request. Looking forward to seeing it in action. Thank you.

/Kasper

 

You must be logged in to post in the forum