Developer forum

Forum » Development » Accessing Translation api

Accessing Translation api

Dmitrij Jazel
Reply

Hej Guys,

I was wondering if you can access Translations through API, For example:

We have a method in extensebility code, and that method must generate a message - that is already in the Translate.xml

So in order to not hardcode the message, it would be great if one could use phrase from Translate.xml.

 

/Dmitrij


Replies

 
Mikkel Ricky
Reply

Dynamicweb 8.6 will have a public API for getting (and creating) translations. Until then, you can use a function like this to get a translation from a Translation.xml file

string GetTranslation(string key, string culture) {
    var path = System.Web.HttpContext.Current.Server.MapPath("~/Files/Templates/Designs/Translations.xml");
    var doc = new System.Xml.XPath.XPathDocument(path);
    var navigator = doc.CreateNavigator();
    var node = navigator.SelectSingleNode("/translations/key[@name='"+key+"']/translation[@culture='"+culture+"']");
    return node != null ? node.Value : null;
}

In a real application you'll probably want to cache the translations, but the function above shows the general idea.

Best regards,
Mikkel 

 
Dmitrij Jazel
Reply

Hej Mikkel,

Sounds like a nice way of solving it, I will give it a shot, and will let you know how it goes :-)

 

/Dmitrij

 

You must be logged in to post in the forum